🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Yet another question:

Started by
4 comments, last by Jonny Go 24 years, 3 months ago
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.
Advertisement
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
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:

 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?
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
Its all fun and games till someone gets pregnant.
I''m sorry for confusing you.

That function, ProcessAge() was just a placeholder. Basically, I was saying to do whatever processing on the iAge variable that you want to do.

Josh
http://www.jh-software.com
Joshhttp://www.jh-software.com

This topic is closed to new replies.

Advertisement