Getting a infinite loop when error checking
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Getting a infinite loop when error checking

  1. #1
    Join Date
    Nov 2003
    Posts
    31

    Getting a infinite loop when error checking

    Why does this create a infinite loop if I enter a char?



    //////////////////////////////////////////////////////////////////////
    //CREATE ITEM
    //////////////////////////////////////////////////////////////////////
    void World::CreateItem()
    {
    int answer = 0;
    do
    {
    cout << endl << "Item Level: ";
    cin >> answer;

    }while(!GoodChoice(0, answer, 50));
    FirstItem->GenerateItem(this, answer);
    }

    //////////////////////////////////////////////////////////////////////
    //GOOD CHOICE
    //////////////////////////////////////////////////////////////////////
    int World::GoodChoice(int lowest, int Num, int highest)
    {

    if ((Num <= lowest) || (Num > highest))
    {
    //A bad choice so print a message and return false
    cout << endl << "I don't understand that choice." << endl;
    return 0;
    }

    //Since we haven't returned, the choice is good
    return 1;
    }

  2. #2
    Join Date
    Nov 2002
    Posts
    49
    What does your output look like?

  3. #3
    Join Date
    Nov 2003
    Posts
    31
    I don't understand that choice.

    Item Level:

    I don't understand that choice.

    Item Level:

    I don't understand that choice.

    Item Level:

    Over and over and over, I need to ctrl c out of it to stop it

  4. #4
    Join Date
    Nov 2003
    Posts
    31
    it works if I enter a number but it's when I enter a char like an 'a'

  5. #5
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,471
    But you are asking for an integer. 'a' is not an integer, so answer gets the value 0, which is a bad choice according to your code. If you want a character, why aren't you asking for a character?
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  6. #6
    Join Date
    Nov 2003
    Posts
    31
    thats why I have the error checking in place, if someone types something other than what I want I want it to display the message "I don't understand that choice." then go back to
    Item Level: and wait for another entry.

  7. #7
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,564
    When you enter an "a", the read fails, which sets the
    failbit for the stream (cin in your case) AND does not
    move the position in the stream (still points to the "a").

    If an error occurs, you need to clear the state of the stream
    and throw out the rest of the stream buffer :

    Code:
    cin >> answer;
    
    if (cin.fail())
    {
         cin.clear();
         cin.ignore(100,'\n');
    }

  8. #8
    Join Date
    Nov 2003
    Posts
    31
    Thanks a lot that did it..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center