Yet another question:
Yes, I know, im a pest. But I have to learn somehow.
Whenever letters are entered into a cin meant for integers, it messes the whole program up
Heres what I got
age:
cout << endl << "Whats your age?: ";
cin >> age;
if (age < 0)
goto age;
else if (age > 1000)
goto age;
else
So if I were to accidentally enter a letter, everything is screwed......
What would I do to overcome this?
Thanks
Its all fun and games till someone gets pregnant.
This kind of error checking is really tedious to do. The reason characters will screw up a cin waiting for an integer is because you''re only giving the cin one byte, when it''s expecting 2 or 4.
Basically, the only safe way to handle input via cin is to read input a string at a time, and then parse out the information you''re looking for. That will keep cin from gettin'' the funk. Reading integers from strings can be a pain in the behind though.
Jonathan
Basically, the only safe way to handle input via cin is to read input a string at a time, and then parse out the information you''re looking for. That will keep cin from gettin'' the funk. Reading integers from strings can be a pain in the behind though.
Jonathan
You''ll want to do what Jonathan suggested. Instead of reading a number, read in a string and then convert it to an integer. Here''s a small code snippet:
Josh
http://www.jh-software.com
int iAge; char szAge[ 256 ]; // hopefully nobody will type more than 255 characters... cout << "What''s your age? "; cin.getline( szAge, 255 ); // reads until ENTER is pressed iAge = atoi( szAge ); // atoi - alphanumeric to integer. ProcessAge( iAge ); // don''t use gotos unless you *HAVE* to.
Josh
http://www.jh-software.com
Joshhttp://www.jh-software.com
Ok..
I tried that out but it gave me this
"[C++ Error] test.cpp(66): E2268 Call to undefined function ''ProcessiAge''."
Does it require a header file?
I tried that out but it gave me this
"[C++ Error] test.cpp(66): E2268 Call to undefined function ''ProcessiAge''."
Does it require a header file?
Its all fun and games till someone gets pregnant.
Whoops, I mean:
"[C++ Error] test.cpp(66): E2268 Call to undefined function ''ProcessAge''."
I had ProcessiAge in the previous post if you hadnt noticed
"[C++ Error] test.cpp(66): E2268 Call to undefined function ''ProcessAge''."
I had ProcessiAge in the previous post if you hadnt noticed
Its all fun and games till someone gets pregnant.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement