Ask your own question, for FREE!
Computer Science 8 Online
sonnguyen0604:

Hello everyone, I have a problem with my homework. I more fix my homework, but my teacher wants me, watch this video https://greg.jamison.cc/Web%20Pages/CSCI40/videos/ass2-commas.mp4 . You scroll video 4:38 and add code. Write a program that accepts a 7-9 digit integer and echoes the number with commas between every three digits from the right c++. NOTE: Do not use arrays, for loops, functions, pointers, if statement, or while loops. Only use . I have a problem when I input number 10000000, output 10,0,0. I want to 100,000,00 but I do not how to code. And my teacher requires to use this code. I am so tired. Can you help me? Thank you so much #include using namespace std; int main() { int leadingDigits, middleDigits, lastDigits; int tempValue, original; cout<<"Please enter 7- to 9-digit number.\n"; cin>>original; tempValue = original / 1000; lastDigits = original % 1000; //add code leadingDigits = tempValue / 1000; middleDigits = tempValue % 1000; //add code cout<<"The number with commas is "<. 26 minutes ago #include #include // setfill, setw using namespace std; int main() { int original, millions, thousands, ones; cout<<"Please enter 7- to 9-digit number.\n"; cin>>original; //Formula for number millions = original / 1000000; thousands = original / 1000 % 1000; ones = original % 1000; // Create if - else if condition to produce result #define THREE<<','<

Vocaloid:

@SmokeyBrown

SmokeyBrown:

Based on the video, it seems like your instructor wants you to apply the code for listing individual digits, which you can use to make sure none of the digits of the input number are excluded in the output. The example in the video is around 3:27. I think the idea is that by keeping track of the individual digits from each 3-digit section, you can make sure that the output matches the input. I think it is also true that the only time digits will be lost is in the case that the remainder of the 1000-division is less than 3 digits, in which case you will only ever need to add left-side zeroes in order to make the output exactly 3 digits, as we mentioned the last time we looked at this problem. However, it seems like that is not the implementation that your instructor wants you to take. I would suggest looking at the code from part 3:27 in the video and see how you can apply it to the code you have in order to make it return the proper output. Consider how you can get the individual digits (even when they are 0 digits) and use that to return the appropriate digits in your program.

sonnguyen0604:

Yes, I do it. Thank you so much. ^^ #include <iostream> using namespace std; int main() { int leadingDigits, middleDigits, lastDigits; int tempValue, original; cout<<"Please enter 7- to 9-digit number.\n"; cin>>original; tempValue = original / 1000; lastDigits = original % 1000; // Add the lastDigits with trailing zeroes. string lastdigits = string(3 - to_string(lastDigits).length(), '0').append(to_string(lastDigits)); leadingDigits = tempValue / 1000; middleDigits = tempValue % 1000; // Add the middleDigits with trailing zeroes. string middledigits = string(3 - to_string(middleDigits).length(), '0').append(to_string(middleDigits)); cout<<"The number with commas is "<<leadingDigits; cout<<","<<middledigits<<","<<lastdigits<<endl; return 0; }

SmokeyBrown:

No problem! Glad you could figure it out :)

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!
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!