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;
}
}
}
} 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; }
}
}
}
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.
<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
* 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?
got it. thx all. I guess exceptions are not appropriate for this type
of situation.
> 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
"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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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,...
|
by: Arjen |
last post by:
Hi,
I'm doing this:
try {
try {
}
catch(Exception ex){
throw;
|
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...
|
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...
|
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...
|
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()
|
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...
|
by: dick |
last post by:
in the "try{throw}catch" structure, how the C++ code return the "type"
thrown by a function?
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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,...
| | |