# Help Writing Simple Program

• 09-12-2007
wco5002
Help Writing Simple Program
Hi,

I'm taking an entry level C++ class to satisfy my computer engineering major. I had homework last night, which consisted of writing several programs, and I stumbled upon one, which after several hours, I cannot figure out. Here is the question.

"Write a program that reads in an integer that is greater than 1,000 and less than 1,000,000. Recall that the number must be entered without a comma. Display the number with a comma inserted where it belongs. (Hint: use the % operator.)

I can't figure out what variables I will need, and what values to assign them to. This one is really annoying me, so anyone that can offer any help will be appreciated. Thanks.
• 09-12-2007
dwks
Well, here's basically what the code must do at the beginning.
• Read in a number
• Make sure the number is > 1000 and < 1000000. If not, perhaps read in another number.

Likely you got that far and then got stuck.

So how do you print a number with commas? This problem simplifies down to, how can I get the rightmost three digits of a number, and then the next rightmost three digits etc, which you can then print separated with commas.

The &#37; (modulus) operator will work here. The modulus operator returns the remainder of a number divided by another number. For example, 8%3 is 2, because 8/3 is 2 with a remainder of 2.

If you use num%10, you'll get the rightmost digit of a number. Think about it. Any number divided by ten will have a remainder between 0 and 9, which will be equal to the last digit.

So, to get the three rightmost digits of a number, use num%1000. To get the next three rightmost digits, you could use (num/1000)%1000. Or you could just go num/=1000 and do num%1000 once again.

One disadvantage of this is that you'll need to store the digits and print them in a different order than you got them. So perhaps you'll need to generate a string, and then print that, or use a different algorithm which gives you the leftmost three digits first. This is more complicated, though, because you have to count the number of digits before you know whether the first digits number one, two, or three.
• 09-12-2007
robwhit
you need an int that hold the number. then read from the user a number, check whether or not ((num> 1000) && (num < 1000000)) then output the number one section at a time.

http://www.cprogramming.com/tutorial/modulus.html

edit: beaten
• 09-12-2007
wco5002
thanks dwks, i think that helped a lot. now to just test the program.
• 09-12-2007
hk_mp5kpdw
Not that I'd recommend it for this program you're working on, but the language itself supports certain features that takes care of this (placing the commas separators in a number) for you. It looks like a lot of work just to get those stupid commas but it's there for consideration (for future reference):
Code:

```#include <iostream> #include <locale> #include <string> #include <iterator> int main() {     std::locale loc(std::locale("American_USA.1252"),                     new std::num_put<char,std::back_insert_iterator<std::string> >);     std::basic_ios<char> str(0);     str.imbue(loc);     std::string s = "";     std::use_facet<std::num_put<char,std::back_insert_iterator<std::string> > >(loc)         .put(std::back_inserter(s),str,' ',static_cast<unsigned long>(12345678L));     std::cout << s << std::endl;     return 0; }```
Output:
Code:

`12,345,678`
Tested under MS Visual Studio C++ 2005. I don't understand 100&#37; of what's going on there so don't ask.