Re: Problem in C++ program
<bazymew@gmail.com> wrote in message
news:1183750331.003373.250260@c77g2000hse.googlegroups.com...
I'm new to C++(I've only started learning about a month ago), so
please don't call me stupid for asking a dumb question.
You see I was experimenting with Exception operators, trying to
make a program not lock up or quit right away when you enter a non
integer value. Now, even if you enter an integer value, it says to
enter an integer value. I hadf it set up so that the second time you
enter a non integer value, it quits. Now, the second time you enter
any value, it quits. Here's a section of the source code:
cin >> integer;
try
{
throw integer;
No matter what happened in the cin >> integer; statement, integer WILL
contain an integer. It's the only thing it can contain. What you are
trying to do here will not work. Also, if cin >> integer fails then cin
will be put into a bad state and your next cin >> integer will also fail
without even looking at the input until you reset cin.
Take a look at the FAQ:
Read from 15.2
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.2
down to 15.6 or so which should answer most of your questions on cin input
errors and how to deal with them.
Trying to throw an integer and seeing if an integer was thrown is not the
way to deal with them.
you basically want your cin >> input
in an if statement to see if it was successful.
if ( cin >> input )
// Yeehaw! Got a number
else
// Doh! Bad input. Reset cin and maybe try again
}
catch (int ncn)
{
name = new char[ncn];
}
catch (...)
{
cout << "Please enter an integer value.\n" << "If you enter a non
integer value a second time, the program will end.";
cin >> integer;
try
{
throw integer;
}
catch (int ncn)
{
name = new char[ncn];
}
catch (...)
{
return (0);
}
}