How do I add commas?

lol this sounds silly but how do I add commas like when the program is excuting the user have to enter something in there with a comma.

EDIT: *SIGH*...... IS THERE ANY WAY TO ADD COMMA'S INSIDE THE NUMBERS AFTER I HIT ENTER TO EXECUTE IT! Here for more info:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

using namespace std;

int main()
{
int pop;

cout << "Enter a population number! Must be between 8 to 9 digits.\n";
cin >> pop;

if ( pop >= 313955000 )
    cout << "United States\n";
else
    if ( pop >= 1347350000 )
        cout << "China\n";
else
    if ( pop >= 1210193422 )
        cout << "India\n";
else
    if ( pop >= 127530000 )
        cout << "Japan\n";
else
    if ( pop >= 87840000 )
        cout << "Vietnam\n";
else
    if ( pop >= 112336538 )
        cout << "Mexico";

return 0;
}
Last edited on
Can u plz further elaborate ..
There's more info!
closed account (o3hC5Di1)
Hi there,

If it's std::cin you're using I would use a dummychar:

1
2
3
4
5
char dummychar;
int x,y,z;

std::cout << "Please enter x,y,z";
std::cin >> x >> dummychar >> y >> dummychar >> z;


Perhaps someone else would have a more elegant solution.
There's also scanf(), but that's not really standard c++ so it is advised against.

Edit: Hmm, i didn't see your added program before - do you mean they would enter comma's within the numbers? Because then it gets complicated, as the two values separated by a comma are in fact one and the same number, but c++ won't recognise it as such as a comma is not a numeric character.
Also, please don't start using caps because someone asks you a bit more information - in this case I may have given you wrong information because your brief problem description was inaccurate, so don't blame others for that.

Hope that helps.

All the best,
NwN
Last edited on
You must make pop String .
I haven't reach to the "char" part yet lol. So far I learned "int" and "double". However ,little on "char".. not really in depth with that stuff yet.
Last edited on
So you are trying to allow the user enter something like
123,456,789
and treat that as number 123456789 ?

If this is so, use a std::string for reading, get rid of commas using std:;string methods and a std::stringstream to convert to a number.
Only using "int" and "double", you cannot do what you want to do coz , is not nemeric character.
closed account (o3hC5Di1)
To add on Bibek Subedi's post:

This article: http://www.cplusplus.com/articles/D9j2Nwbp/ shows how to convert a string containing comma's to an int. That would require some knowledge of std::string however : http://www.cplusplus.com/doc/tutorial/variables/

All the best,
NwN
Damn..... I haven't learn about "String". Maybe this is a great time to learn it!
Topic archived. No new replies allowed.