problem with input

This is a discussion on problem with input within the C++ Programming forums, part of the General Programming Boards category; In the code below I want to only accept input of 1, 2, 3, 4, 5 or 6. This works ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    96

    problem with input

    In the code below I want to only accept input of 1, 2, 3, 4, 5 or 6.

    This works in that it will accept only one of those numbers and return it to my main.

    If I enter a number such as '22' say, then it reprompts for a correct number; but if I input 'a' it locks the program up.

    Is there a better way of asking for my input to stop this happening?

    Code:
    int menu ()
    {
        int choice=0;
    
        cout<<endl<<"JayCee's Player Information Storage, Recall, and Amendment Facility"<<endl<<endl<<endl;
        cout<<"Please choose from :"<<endl<<endl<<endl;
    
        cout<<"       1  -  Ceate a new data file"<<endl<<endl;
        cout<<"       2  -  Add a record to an existing file"<<endl<<endl;
        cout<<"       3  -  Delete a record from a file"<<endl<<endl;
        cout<<"       4  -  Read a single record from a file"<<endl<<endl;
        cout<<"       5  -  Read whole file into an array"<<endl<<endl;
        cout<<"       6  -  End the program"<<endl<<endl<<endl;
    
        cout<<"                                 Choice  -  _\b";
    
        while  (choice<1 || choice>6)
            {
            cin>>choice;
            }
    
        return choice;
    }

  2. #2
    Registered User Sid_TheBeginner's Avatar
    Join Date
    Jun 2012
    Location
    India
    Posts
    19
    try using a simple if else looping. The Final else must say please enter a valid value. switch is also one better way.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Welcome to the intricacies of I/O in C/C++. Your input loop is designed to accept integer input only and when it comes across something that does not fit into its expectations, "a" for example, then odd things happen. What is occurring here is that the cin stream goes into an error state where further operations on that stream are ineffective. The bad data that caused the stream to go into its errored state remains in the input stream and will continue to cause problems until it is cleared out.

    The way to deal with such an occurrence is to first clear the error state on the stream by calling... you guessed it, the stream's "clear" member function. Next you want to wipe out the bad data in the stream that caused the problem in the first place and you do so by calling the "ignore" member function on the stream.

    In practice, your loop might look more like this:
    Code:
    #include <limits>
    
    ...
    
    while  (choice<1 || choice>6)
    {
        cin>>choice;
        if( !cin )
        {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
    }
    There are other ways of writing that loop but the basics are in place: check if the stream is in an errored/bad state and clear the error and ignore the bad data if it is. The limits header is needed for the numeric_limits part. Most people would also reprompt the user to enter good data.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    96
    Thanks very much for that suggestion, I have changed it to :
    Code:
            if ((!cin) || (choice<1 || choice>6))
                {
                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(), '\n');
                cout<<"            Please enter a valid choice  -  _\b";
                }
    and it gives a nice clean prompt if a number outside 1-6 or a character is input.

    There is only one thing I would like to change now and that is each new prompt is on a new line and I can't find an escape code to allow me to do it so they appear over the top of each other ie in the same place each time, maybe that isn't really necessary but I thought it would look better. If I am asking something I should be able to look up please do tell me but I have tried a \t and \v but neither seems to do as I want.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    You can try \r.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    96
    Thanks for that Elysia but unfortunately that appears to do nothing.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Well, it's platform dependent, so if it doesn't work, you have to resort to OS-specific APIs.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with input
    By Mr.Lnx in forum C Programming
    Replies: 0
    Last Post: 01-29-2012, 09:51 AM
  2. input problem
    By joybanerjee39 in forum C Programming
    Replies: 9
    Last Post: 10-15-2011, 12:18 PM
  3. Input Problem
    By slapgun87 in forum C Programming
    Replies: 4
    Last Post: 07-26-2010, 10:40 AM
  4. geting input in C, problem in getting input
    By BujarM in forum C Programming
    Replies: 3
    Last Post: 04-17-2009, 09:38 PM
  5. input problem
    By lukestanton07 in forum C Programming
    Replies: 13
    Last Post: 10-14-2008, 04:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21