Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Try, Catch, And Throw

Reply
Thread Tools

Try, Catch, And Throw

 
 
A_StClaire_@hotmail.com
Guest
Posts: n/a
 
      12-03-2005
it's been frustrating rearranging these things in the code below and
never getting the result I want.

I'm looking for the system to cout a single "Invalid choice. Try
again:" in response to every incorrect entry the user gives, regardless
of whether the invalid entry is a single character or 20 characters.

can anyone help?

void Game::RunSinglePlayerGame()
{
bool quit = false;

while(quit == false)
{
RunSinglePlayerSequence();
Interface::Instance().ClearScreen();

int choice;
bool validChoice = false;

cout << "\n\n Play again?\n\n 1. Yes\n 2. No\n\n ";

while(validChoice == false)
{
cin >> choice;

switch(choice)
{
case 1:
validChoice = true;
break;
case 2:
validChoice = true;
quit = true;
cout << "\n\n\n\n Thanks for playing!\n\n ";
break;
default:
cout << "\n Invalid choice. Try again: ";
break;
}

}

}

}

 
Reply With Quote
 
 
 
 
Zara
Guest
Posts: n/a
 
      12-03-2005
On 3 Dec 2005 01:48:00 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

>it's been frustrating rearranging these things in the code below and
>never getting the result I want.
>
>I'm looking for the system to cout a single "Invalid choice. Try
>again:" in response to every incorrect entry the user gives, regardless
>of whether the invalid entry is a single character or 20 characters.
>
>can anyone help?
>
>void Game::RunSinglePlayerGame()
>{
> bool quit = false;
>
> while(quit == false)
> {
> RunSinglePlayerSequence();
> Interface::Instance().ClearScreen();
>
> int choice;
> bool validChoice = false;
>
> cout << "\n\n Play again?\n\n 1. Yes\n 2. No\n\n ";
>
> while(validChoice == false)
> {
> cin >> choice;



You must detect when the input is not a valid number
if (cin.fail())
{
choice=0;
}

>
> switch(choice)
> {
> case 1:
> validChoice = true;
> break;
> case 2:
> validChoice = true;
> quit = true;
> cout << "\n\n\n\n Thanks for playing!\n\n ";
> break;
> default:
> cout << "\n Invalid choice. Try again: ";
> break;
> }
>
> }
>
> }
>
>}

 
Reply With Quote
 
 
 
 
A_StClaire_@hotmail.com
Guest
Posts: n/a
 
      12-03-2005

Zara wrote:
>
> You must detect when the input is not a valid number
> if (cin.fail())
> {
> choice=0;
> }



added your code after cin >> choice but it does not appear to have any
impact.

 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      12-03-2005
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com
> Zara wrote:
>>
>> You must detect when the input is not a valid number
>> if (cin.fail())
>> {
>> choice=0;
>> }

>
>
> added your code after cin >> choice but it does not appear to have any
> impact.


You need to reset cin to a valid state and clear its buffer. After

cin>>choice;

add

if (cin.fail())
{
choice=0;
cin.clear(cin.rdstate()&~std::ios::failbit); // set to valid state
cin.ignore(numeric_limits<int>::max(), '\n'); // empty buffer
}

You will need to

#include <limits>

to get numeric_limits<int>::max().


--
John Carson


 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      12-03-2005
* (E-Mail Removed):
>
> it's been frustrating rearranging these things in the code below and
> never getting the result I want.
>
> I'm looking for the system to cout a single "Invalid choice. Try
> again:" in response to every incorrect entry the user gives, regardless
> of whether the invalid entry is a single character or 20 characters.
>
> can anyone help?


Does the title of this posting, "Try, Catch, And Throw", mean that you
have enabled exceptions in the input stream?


> void Game::RunSinglePlayerGame()
> {
> bool quit = false;
>
> while(quit == false)
> {
> RunSinglePlayerSequence();
> Interface::Instance().ClearScreen();
>
> int choice;


Make choice a std::string.


> bool validChoice = false;
>
> cout << "\n\n Play again?\n\n 1. Yes\n 2. No\n\n ";
>
> while(validChoice == false)
> {
> cin >> choice;


cin.clear();
std::getline( cin, choice );

>
> switch(choice)


Replace 'switch' with if-else construction.


> {
> case 1:
> validChoice = true;
> break;
> case 2:
> validChoice = true;
> quit = true;
> cout << "\n\n\n\n Thanks for playing!\n\n ";
> break;
> default:
> cout << "\n Invalid choice. Try again: ";
> break;
> }
>
> }
>
> }
>
> }
>


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
A_StClaire_@hotmail.com
Guest
Posts: n/a
 
      12-03-2005

got it. thx all. I guess exceptions are not appropriate for this type
of situation.

 
Reply With Quote
 
Matteo
Guest
Posts: n/a
 
      12-05-2005
> switch(choice)
> {
> case 1:
> validChoice = true;


I think you want
case '1':
here instead of
case 1:

What you have refers to (probably) ASCII code 1, which can't really be
typed in. Use single quotes when refering to character constants.

-matt

 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      12-05-2005
"Matteo" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com
>> switch(choice)
>> {
>> case 1:
>> validChoice = true;

>
> I think you want
> case '1':
> here instead of
> case 1:
>
> What you have refers to (probably) ASCII code 1, which can't really be
> typed in. Use single quotes when refering to character constants.


Not true. The variable choice is defined to be an integer. Thus when 1 is
typed in

cin >> choice;

assigns the value 1 to choice.

--
John Carson


 
Reply With Quote
 
Matteo
Guest
Posts: n/a
 
      12-05-2005
Whoops! I don't know why I thought he was reading into a char. That'll
teach me to read the OP's code more careflly next time.

Sorry,
-matt

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To throw or to throw not? Emanuele D'Arrigo Python 6 11-15-2008 04:12 PM
exception throw and handle and resume Rahul C++ 4 12-08-2007 08:23 PM
JNI's throw new does not throw an exception yarona@m-sys.com Java 15 09-08-2005 08:36 AM
is catch and re-throw a good idea? Andy Fish Java 17 04-19-2004 09:07 PM
Throw Exception Vs Throw New Exception Kerri ASP .Net 2 10-27-2003 02:13 PM



Advertisments