Archived

This topic is now archived and is closed to further replies.

Jammer211

Can someone sample this?

Recommended Posts

I''m new to C++ and I''m writing small little programs to get better. This one calculates someones age. #include int main() { cout << "\n*****WARNING***** Please Enter Only Numbers *****WARNING*****\n"; begin: cout << "\nWhat year is it?"; int WhatYear; //A big occurs in each of these statements. If a non-numerical character is enetered, // the program will loop until it is manually shut down. Poo cin >> WhatYear; cout <<"\nHow old are you?"; int YourAge; cin >> YourAge; cout << "\nIn what year do you wish to know your age?"; int NextYear; cin >> NextYear; if (NextYear < WhatYear) { cout << "\nPlease Enter a year past the current year"; cout << "\n"; goto begin ; } else; int NextAge; NextAge= (NextYear-WhatYear)+YourAge; cout << "\nIn " << NextYear << " you will be " << NextAge << "\n"; int end; cout << "\nDo you want to find another age? Hit 1 to continue or 2 to stop: "; cin >> end; if (end==1) { cout << "\n"; goto begin; } else; return 0; } Can someone tell me if this is good? Also, there is a bug in it. If you enter a non-numerical character, it loops indefinently. Can this be fixed? Thanks. Just my 2 cents.. oh damn! I was gonna spend it on something...

Share this post


Link to post
Share on other sites
try this:

After you input it, call the _itoa command, it converts an int to a string. Like this:

int YourAge;
char junkstr[10];
bool isOK = false;

while(!isOK)
{
cout <<"\nHow old are you?";
cin >> YourAge;
_itoa(YourAge, junkstr, 10);
if (junkstr[0] >= ''0'' && junkstr[0] <= ''9'') //it is a #
isOK = true;
}

I just made this up, I''m pretty sure it''ll work tho.

Share this post


Link to post
Share on other sites