Try, Catch, And Throw

A

A_StClaire_

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;
}

}

}

}
 
Z

Zara

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;
}
 
A

A_StClaire_

Zara said:
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.
 
J

John Carson

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().
 
A

Alf P. Steinbach

* (e-mail address 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.
 
A

A_StClaire_

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

Matteo

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
 
J

John Carson

Matteo said:
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.
 
M

Matteo

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,838
Latest member
KandiceChi

Latest Threads

Top