Ask your own question, for FREE!
Computer Science 88 Online
OpenStudy (anonymous):

Write a program that accepts a 7-9 digit integer and echoes the number with commas between every three digits from the right. Test it with the numbers 1234567, 987654321, 20300045, & 10000000. i'm having trouble with the output here is the code i have so far

OpenStudy (anonymous):

#include<iostream> using namespace std; int main() { int first3, middle3, last3; int original, temp, first, second,third, temp1, temp2, temp3, first2, temp4, second2, third2; cout<<"enter a 7-9 digit number"<<endl; cin>>original; temp=original/1000; last3=original%1000; temp1=original/10; first=original%10; temp2=temp1/10; second=temp2%10; third=temp2/10; first3=temp/1000; middle3=temp%1000; temp3=original/10; first2=original%10; temp4=temp1/10; second2=temp4%10; third2=temp4/10; cout<<"the number with a comma is "<<endl; cout<<first3<<","<<first2<<second2<<third2<<","<<first<<second<<third<<endl; return 0; }

OpenStudy (anonymous):

The way you're coding is very messy. You're creating so many variables. If you keep them in an array, you can loop over them and make both input and output much easier. To be honest, I'd suggest a complete re-write. Without looking at what you have. It's harder to fix code with many unknown bugs than to write new code. The code I wrote to solve this problem uses 2 while loops, an array of ints, an int for iterating, and an int to store the input in.

OpenStudy (anonymous):

@bdean20 my teacher doesnt not want while loops and arrays as this was our 2nd assignment and hadnt learned the while loop or array

OpenStudy (anonymous):

I've just rewritten my code to work without loops or arrays. You only really need to repeat the process of %10, /10 to get each new digit (from the back). Don't use temp variables if you don't need them. They add to the confusion. Here's my code for the first digit (from the back): one = original % 10; original /= 10;

OpenStudy (anonymous):

you can take a number like int a = 1234567 String b =""+a; b.trim(); while(b.length() < 7 && b.length >9) { // re enter the value } char c [] = b.toCharArray(); now you can print it with comma s in print line function!

OpenStudy (anonymous):

@xaadi1993 can't use a while loop teacher said so and i think the problem is the last cout statement to display the numbers with commas but i cant seem to find it

OpenStudy (anonymous):

cout<<"the number with a comma is "<<endl; cout<<first3<<","<<first2<<second2<<third2<<","<<first<<second<<third<<endl;

OpenStudy (anonymous):

The code before that has bugs. I've written out descriptions of what each of your variables contains at each step (above the assignment statement): // everything but the last 3 temp=original/1000; //the last 3 last3=original%1000; //everything but the last temp1=original/10; //the last 1 first=original%10; //everything but the last 2 temp2=temp1/10; //the last 1 from everything but the last 2 //aka the 3rd last second=temp2%10; //everything but the last 3 third=temp2/10; //everything before the last 6 first3=temp/1000; //the 3 digits in the middle middle3=temp%1000; //everything but the last 1 temp3=original/10; //the last 1 (of the whole number!) first2=original%10; //everything but the last 2 temp4=temp1/10; //the 3rd last (of the whole number!) second2=temp4%10; //everything but the last 3 third2=temp4/10;

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Latest Questions
luhbabyliyahh: Is my daughter cute her name is zalaria (PFP)
19 seconds ago 0 Replies 0 Medals
luhbabyliyahh: If my daughter cute her name is zalaria (PFP)
2 minutes ago 0 Replies 0 Medals
Gucchi: leadership help
56 seconds ago 14 Replies 2 Medals
Arizona: HELLLp science ASAP
53 minutes ago 7 Replies 0 Medals
Gucchi: leadership help
1 hour ago 10 Replies 2 Medals
Limitless2: what is circumference of a circle
2 hours ago 12 Replies 3 Medals
kenziegray7965liner: pls help
1 hour ago 8 Replies 2 Medals
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!