: Write a program that reads a number greater than or equal to 1000 from the user and prints it out with a comma seperating the thousands. Here is a sample dialog; the user input in color:: : Please enter an integer >= 1000 : 23456: : 23,456 : : Can anyone figure this out?

Well, you already have the number in string form (because you read it from the console). Now all you have to do is count off characters from the right and insert a comma after every three digits. You can insert the commas while copying the digits to a new string, or you can insert them in place. This will give you practice on looping and string handling at the character level. It's not going to be easy, but you can always post here for help.

Sometimes it is just easier to use a little figuring out beforehand and then bruteforce the result.

Think about this, find out how many digits would be at the very left of your number, e.g.:

1,234 has one12,345 has two123,456 has three

Okay, now you have that. Now, say for example you have two digits on the end.

Write out the first two digits of your number and then add a comma. Something like,

cout << Number.substr (0,2);

Then output a comma,

cout << ",";

and then output three more digits and a comma, and continue doing that until you reach the end of the string. You don't even need to know how many digits are actually in your string to begin with, only how many are on the very left.

: : Write a program that reads a number greater than or equal to 1000 from the user and prints it out with a comma seperating the thousands. Here is a sample dialog; the user input in color:: : : : Please enter an integer >= 1000 : 23456: : : : 23,456 : : : : Can anyone figure this out?: : Well, you already have the number in string form (because you read it from the console). Now all you have to do is count off characters from the right and insert a comma after every three digits. You can insert the commas while copying the digits to a new string, or you can insert them in place. This will give you practice on looping and string handling at the character level. It's not going to be easy, but you can always post here for help.: : Cheers,: Eric: : I dont know how to do the looping thing...im getting very different answer freom people and im getting even more confused... can u do one example?

: : : Write a program that reads a number greater than or equal to 1000 from the user and prints it out with a comma seperating the thousands. Here is a sample dialog; the user input in color:: : : : : : Please enter an integer >= 1000 : 23456: : : : : : 23,456 : : : : : : Can anyone figure this out?: : : : Well, you already have the number in string form (because you read it from the console). Now all you have to do is count off characters from the right and insert a comma after every three digits. You can insert the commas while copying the digits to a new string, or you can insert them in place. This will give you practice on looping and string handling at the character level. It's not going to be easy, but you can always post here for help.: : : : Cheers,: : Eric: : : : : I dont know how to do the looping thing...im getting very different answer freom people and im getting even more confused... can u do one example?

Don't get too stressed, it's not as easy as it might first appear. The easiest way to do it is to output each character one at a time, inserting commas as needed. Since you're using std::string, to access individual characters you can use operator[], or using string::iterator. As in:

[code]for (int = 0; i < s.length(); ++i) putchar (s[i]);[/code]or[code]for (string::iterator it = s.begin(); it != s.end(); ++it) putchar (*it);[/code]or you could use pointers, but basic_string::iterator really is just char* anyway, and the iterator is cleaner.

Anyway, the real trick is knowing when to insert a comma. We need to know when we're at the end of a "three digit group". Well we know we need to insert commas after the digits 3,6,9,12,etc. Multiples of three, right? How do we find out in code if a number is a multiple of three? Just divide by 3; if there's no remainder, it's an even multiple. Of course, anytime we need the remainder of an integer division, we know we're going to be using the mod operator (%).

So now we have mathematical way of determining if we're at a digit that needs a comma.

Small problem. In our 'legend' above we number the digits [N..1] (with the 'first' digit on the right). However, in C++ you index digits in a string [0..N-1] (with the 'first' digit on the left). So you'll have to reconcile that in your program. The easiest way is to loop from N to 1, as in "for (int i = length; i >= 1; --i)", or something like that. If (i % 3 == 0), we know we need to insert a comma.

: i did what you said but it wont show the rest of the #'s.: ex: user inputs '1000': then the screen shows '10,': : im really lost here.i need for the program to place the comma where is needed. 1,000 or 100,000: : ill really appreciate it if you can teach me this one thing...im stuck!

Have you determined how many characters on the left you need?

You've need to have your program count the number of digits there are in the number, and then perform a modulo (%) of three on that number to get how many you need to print before simply printing three at a time.

This is what i got so far...but im getting confused with the numbers...your telling me to read from right to left and xotor is telling me from left to right. I dont know!I'm i even going in the right direction. I think i got some of it.

: This is what i got so far...but im getting confused with the numbers...your telling me to read from right to left and xotor is telling me from left to right. I dont know!I'm i even going in the right direction. I think i got some of it.

Well, Xotor gave you a better "road map" than I did. My code was probably a bit more theoretical I guess, trying to get you to understand how the mod operator would work for this situation, it was not really meant as example code. You don't want to address each digit individually like you're doing, you need to do this in a loop so that your program can handle number that are arbitrarily long.

Xotor has already given you pseudocode. He's some psuedocode for another approach: