int help!!

Jul 21, 2012 at 10:41pm
I'm trying to make a program that tells the user to enter a population number and estimate what nation has the population number. Anyways, when I entered an integer,it pops up nothing. I entered "400,000,000 in but still nothing. I did the same thing that is under 400,000,000 like 200,000,000. Here's the code I'm working on. Ik there's only one but I'm trying to figure it out till I put more in.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*Enter a population number and estimate what nation has that population number*/
#include <iostream>

using namespace std;

int main()
{
int pop;

cout << "Enter a population number!\n";
cin >> pop;

if ( pop >= 313,955,00 )
    cout << "United States";

return 0;
}
Last edited on Jul 21, 2012 at 10:41pm
Jul 21, 2012 at 10:56pm
Look in a book on C/C++ how integer constants are represented.
Jul 21, 2012 at 11:01pm
Integers in C++ do not include commas. Tale out the commas and it should work.

If you want to allow the user to enter the commas you will have to read the population in as a string and so a bit of work.

Edit - 313,955,00 is not even a valid number regardless. lol
Last edited on Jul 21, 2012 at 11:02pm
Jul 21, 2012 at 11:04pm
lol now I see my mistakes. Thanks. :P
Jul 21, 2012 at 11:18pm
Now how can I make this more accurate? lol Don't give me the codes. Tell me and i'll do them by myself! Here's the improved code I did:

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;
}
Jul 21, 2012 at 11:54pm
What do you mean more accurate?
Jul 22, 2012 at 12:03am
I'm not sure what you mean by more accurate but I can help you a bit. As your code is, if the user enters 1347350000(China), the program outputs the US. This is because you have the US first and 1347350000 > 313955000. You can fix this by either adding an upper limit to your if statements or rearrange the order so the countries with the highest population are at the top.
Jul 22, 2012 at 2:53am
Sorry, I meant like improved it :D or no need......

@light: oh ok.
Jul 22, 2012 at 3:07am
Well, you could put an "else" in there among the "ifs" and "else ifs," just in case someone decides to put in a population of -1 for some reason (or something similarly ridiculous).

Also, what would happen if someone decided to put commas in the input? (I really don't know).

And maybe in the output, you can put in the last census-based population for the country.

And of course, more countries!
Last edited on Jul 22, 2012 at 3:10am
Jul 22, 2012 at 3:43am
OH SNAP! I SHOULD ADD THEM IN! I'm too lazy to put in more countries lol. However, i'll add comma's in and last cenus-based population!
Topic archived. No new replies allowed.