Try, Catch, And Throw

Discussion in 'C++' started by A_StClaire_@hotmail.com, Dec 3, 2005.

  1. Guest

    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, 2005
    #1
    1. Advertising

  2. Zara Guest

    On 3 Dec 2005 01:48:00 -0800, 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, Dec 3, 2005
    #2
    1. Advertising

  3. Guest

    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, 2005
    #3
  4. John Carson Guest

    <> wrote in message
    news:
    > 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
     
    John Carson, Dec 3, 2005
    #4
  5. * :
    >
    > 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?
     
    Alf P. Steinbach, Dec 3, 2005
    #5
  6. Guest

    got it. thx all. I guess exceptions are not appropriate for this type
    of situation.
     
    , Dec 3, 2005
    #6
  7. Matteo Guest

    > 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, Dec 5, 2005
    #7
  8. John Carson Guest

    "Matteo" <> wrote in message
    news:
    >> 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
     
    John Carson, Dec 5, 2005
    #8
  9. Matteo Guest

    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
     
    Matteo, Dec 5, 2005
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon Maz
    Replies:
    7
    Views:
    4,299
    Jon Maz
    Oct 25, 2004
  2. Matt
    Replies:
    1
    Views:
    414
    Michael Rauscher
    Jun 12, 2004
  3. Chris Riesbeck

    try-catch-throw-finally

    Chris Riesbeck, Jan 19, 2007, in forum: Java
    Replies:
    3
    Views:
    1,044
    Chris Riesbeck
    Jan 22, 2007
  4. dick

    "try{throw}catch"

    dick, Nov 15, 2006, in forum: C++
    Replies:
    7
    Views:
    415
    Noah Roberts
    Nov 15, 2006
  5. fishfry

    Error.pm and try/catch/throw

    fishfry, Jul 21, 2005, in forum: Perl Misc
    Replies:
    6
    Views:
    111
    Ron Savage
    Jul 24, 2005
Loading...

Share This Page