Unexpected result when entering wrong data type

Discussion in 'C++' started by japh, Jan 8, 2005.

  1. japh

    japh Guest

    I am on chapter 7 of my C++ text (sigh). Could you help me understand
    this? If the user inputs an int, the script works as it should. If
    they enter a char instead, it goes into an endless loop. I'm coming
    from un-typed languages like PERL and PHP so this makes no sense to me:
    (PS - is there a way to preserve indentation in this bulletin board ?)

    #include <iostream>
    using namespace std;

    int main()
    {
    while(true)
    {
    int choice;
    cout << endl << "Enter a number between 1 - 5: ";
    cin >> choice;

    if (choice < 1 || choice > 5)
    {
    cout << endl << "Invalid choice" << endl;
    continue;
    }
    else
    {
    cout << "You chose " << choice << endl;
    }
    }// end while
    } // end main
     
    japh, Jan 8, 2005
    #1
    1. Advertising

  2. "japh" <> wrote...
    >I am on chapter 7 of my C++ text (sigh). Could you help me understand
    > this? If the user inputs an int, the script works as it should. If
    > they enter a char instead, it goes into an endless loop.


    This is actually a FAQ. Visit http://www.parashift.com/c -faq-lite/
    Read it all when you have time just to get an idea what has already
    been answered, so you don't ask about it again (unless it's unclear
    or you think it's incorrectly answered there). For now, you need the
    section 15.

    > I'm coming
    > from un-typed languages like PERL and PHP so this makes no sense to me:
    > (PS - is there a way to preserve indentation in this bulletin board ?)


    Not if you're using Google's delapidated posting service. Get a real ISP
    and connect to their news server using a real news reader.

    > [...]


    V
     
    Victor Bazarov, Jan 8, 2005
    #2
    1. Advertising

  3. japh

    David Hilsee Guest

    "japh" <> wrote in message
    news:...
    > I am on chapter 7 of my C++ text (sigh). Could you help me understand
    > this? If the user inputs an int, the script works as it should. If
    > they enter a char instead, it goes into an endless loop. I'm coming
    > from un-typed languages like PERL and PHP so this makes no sense to me:
    > (PS - is there a way to preserve indentation in this bulletin board ?)


    The FAQ (http://www.parashift.com/c -faq-lite/) answers this. See section
    15 ("Input/output via <iostream> and <cstdio>"), question 2 ("Why does my
    program go into an infinite loop when someone enters an invalid input
    character?"). The other answers in that section are a good read as well.

    --
    David Hilsee
     
    David Hilsee, Jan 8, 2005
    #3
  4. japh

    japh Guest

    OK - thanks... will do
     
    japh, Jan 8, 2005
    #4
  5. japh

    japh Guest

    thanks
     
    japh, Jan 8, 2005
    #5
    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. Jeff
    Replies:
    3
    Views:
    568
    Karsten Baumgarten
    Feb 5, 2005
  2. Replies:
    0
    Views:
    2,592
  3. Nathan Sokalski

    Entering an enumeration type as a property

    Nathan Sokalski, Nov 4, 2009, in forum: ASP .Net
    Replies:
    6
    Views:
    431
    Herfried K. Wagner [MVP]
    Nov 7, 2009
  4. Michael Tan
    Replies:
    32
    Views:
    991
    Ara.T.Howard
    Jul 21, 2005
  5. Andy Tolle
    Replies:
    7
    Views:
    232
    Andy Tolle
    Nov 15, 2010
Loading...

Share This Page