Thread: Simple program, stopping invalid character infinite loop

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    128

    Simple program, stopping invalid character infinite loop

    Hi everyone,

    I've been super busy over the last couple of months and not had a chance to carry on with the jumping into c++ pdf. However, now I'm back and ready, and stuck, already!

    Here is my program, I thought I'd refresh my memory on how loops work:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main ()
    
    {
        int userSelection;
        string file = "File";
        string edit = "Edit";
        string view = "View";
        string navigate = "Navigate";
        
        cout << "1 - " << file << "\n";
        cout << "2 - " << edit << "\n";
        cout << "3 - " << view << "\n";
        cout << "4 - " << navigate <<  "\n\n";
        
        cout << "Please select a number from the list: ";
        cin >> userSelection;
        
        while ( userSelection < 1 || userSelection > 4 )
        {
            cout << "Please try again: ";
            cin >> userSelection;
        }
        
        if ( userSelection == 1 )
        {
            cout << "You chose " << file;
        }
        else if ( userSelection == 2 )
        {
            cout << "You chose " << edit;
        }
        else if ( userSelection == 3 )
        {
            cout << "You chose " << view;
        }
        else
        {
            cout << "You chose " << navigate;
        }
        
        
        return 0;
        
    }
    So it all works ok, entering any number other than 1,2,3 or 4 results in a prompt to "Please try again:".

    However, if entering anything other than a number, i.e. a letter, t, r, a, whatever, the cin fails and goes into an infinite loop.

    I've been reading about this happening in other people's programs but haven't been able to apply any solution to my own. Looking for some kind of 'not a number' function or something. If the program only allows 1,2,3 or 4 in order to continue, I would have logically thought any other input would be bad, hence show the prompt. However, I don't understand how c++ works so my own logic probably goes out the window!

    Any help is much appreciated, thanks.

    Sam.
    Last edited by samwillc; 01-02-2013 at 03:09 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My program's in an infinite loop!
    By Peach in forum C Programming
    Replies: 3
    Last Post: 02-24-2008, 01:58 PM
  2. Stopping an Infinite Loop
    By linuxpyro in forum C Programming
    Replies: 4
    Last Post: 11-30-2006, 12:21 PM
  3. Infinite Loop when entering invalid input
    By acwheat in forum C Programming
    Replies: 5
    Last Post: 04-18-2006, 04:17 PM
  4. Program gets stuck in infinite loop
    By Xanth in forum C++ Programming
    Replies: 10
    Last Post: 02-08-2005, 12:51 AM
  5. detect infinite loop in c program
    By abhivyakat in forum C Programming
    Replies: 19
    Last Post: 10-01-2003, 06:55 AM