473,405 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Try, Catch, And Throw

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

}

}

}

Dec 3 '05 #1
8 1739
On 3 Dec 2005 01:48:00 -0800, A_*********@hotmail.com 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;
}

}

}

}

Dec 3 '05 #2

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.

Dec 3 '05 #3
<A_*********@hotmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr 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
Dec 3 '05 #4
* A_*********@hotmail.com:

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?
Dec 3 '05 #5

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

Dec 3 '05 #6
> 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

Dec 5 '05 #7
"Matteo" <ma****@ncsa.uiuc.edu> wrote in message
news:11**********************@g47g2000cwa.googlegr 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
Dec 5 '05 #8
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

Dec 5 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Gary.Hu | last post by:
I was trying to catch the Arithmetic exception, unsuccessfully. try{ int a = 0, b = 9; b = b / a; }catch(...){ cout << "arithmetic exception was catched!" << endl; } After ran the program,...
7
by: Arjen | last post by:
Hi, I'm doing this: try { try { } catch(Exception ex){ throw;
12
by: Andrew Schepler | last post by:
When compiled with Visual C++ .NET 2003 (only), the program below aborts as though no matching catch clause is present. If the copy constructor of A is made public, it successfully catches the...
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
3
by: will | last post by:
Hi all. I've got an question about how to catch an exception. In Page_Load, I place a DataGrid, dg1, into edit mode. This will call the method called GenericGridEvent. GenericGridEvent will call...
26
by: Grim Reaper | last post by:
Just a quick and probably daft question... Isn't the code; Try DoSomething() Catch e As Exception HandleError(e) Finally DoThisAtTheEnd()
9
by: Mr Flibble | last post by:
Hi all, happy Friday! (certainly Friday is a day worth celebrating). I have a question on try/catch design (an exciting Friday topic for sure): I can either put a try/catch block in every...
7
by: dick | last post by:
in the "try{throw}catch" structure, how the C++ code return the "type" thrown by a function?
4
by: Fred | last post by:
Is it possible to use throw in javascript without try..catch? As far as I know, you must call it from within a try..catch block, or the function that calls throw must itself be called from within...
4
by: Elmo Watson | last post by:
I've got one main method, with a Try Catch block. In that, I catch the error and put it on a label. However, inside the Try portion, I also run another method. I have a Try/Catch block there,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.