user input

This is a discussion on user input within the C++ Programming forums, part of the General Programming Boards category; Hey guys, if I want to get user input for example: Code: cout<<"press Y or X"<<endl; cin>>var Is there anyway ...

  1. #1
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133

    user input

    Hey guys, if I want to get user input for example:

    Code:
    cout<<"press Y or X"<<endl;
    cin>>var
    Is there anyway i can use a function like the getch() in this case so if the users enters something other than y or x it will ask them for the input again and not crash the program?

    Thanks.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    You could either make var a string and convert it to whatever it should be, or you could just let your stream fail, clear it, empty it, and ask again. It's up to you.

    Code:
    int foo;
    cout << "Enter an integer: ";
    while (!(cin >> foo)) {
       cin.clear();
       cin.ignore(100,'\n'); // In reality it should be the max buffer size
       cout << "Invalid input, enter again: ";
    }
    Last edited by SlyMaelstrom; 02-15-2006 at 07:51 PM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Note that SlyMaelstrom's code doesn't actually check for Y or X for you. You would want to add that check to the while code: while (!(cin >> foo) && foo != 'Y' && foo != 'X')

    When reading in a char, only the check against 'Y' and 'X' is necessary, but when you switch your input to read in numbers the extra check of cin >> read will be very helpful.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Yes, I probably should have used an integer in my example. As, well... anything is a character, so cin is unlikely to fail. I actually changed it in the text editor I wrote it in but forgot to change it in this post after I pasted but before I clicked post. Good catch, Daved.
    Sent from my iPadŽ

  5. #5
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Thanks for the replies, as its only one character being used is both cin.clear() and cin.ignore both required?

    Also how do you loop back so the user can enter their option again once the error message has been displayed? Thanks.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    They are not required, but they can help (if the user types in Hello, without the ignore it will display the error once for each letter).

    The while loop loops back automatically. After the error message you can add a re-prompt if you want.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Quote Originally Posted by 182
    Also how do you loop back so the user can enter their option again once the error message has been displayed? Thanks.
    Incase you don't fully understand, since the input is in the loops condition, it's part of the loop and therefor will request input everytime it checks the condition. That is so long as cin has been cleared, otherwise the failed stream will cause an infinite loop.
    Sent from my iPadŽ

  8. #8
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    I thought it was supposed to do that but when I use it, it doesn't seem to work at all:

    Code:
     while (!(cin >> hold) && hold != 'Y' && hold != 'y' && hold != 'X' && hold != 'x')
       cin.clear();
       cin.ignore(); 
    cout<<"Please try again"<<endl;

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    That cin.ignore isn't in the loop. Which means invalid input will kill the buffer, you'll unkill it, and the same input will kill it again. That will happen forever. You need braces.
    Sent from my iPadŽ

  10. #10
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    It still doesn't appear to work with the braces.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Sorry, my fault. It should be:
    Code:
    while (!(cin >> hold) || (hold != 'Y' && hold != 'y' && hold != 'X' && hold != 'x'))
    {
       cin.clear();
       cin.ignore(1000, '\n'); 
       cout<<"Please try again"<<endl;
    }
    You loop while the read fails or the character is not one of the correct inputs.

    Also, the ignore() will only ignore a single character, so if the user types a sentence it will not help unless you change it to the way SlyMaelstrom had it (or how I have it above).

  12. #12
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    It works now thanks

    I understand most of it except the part I have posted below what does this mean?

    while (!(cin >> hold)
    Last edited by 182; 02-16-2006 at 07:27 AM.

  13. #13
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Quote Originally Posted by Daved
    Sorry, my fault. It should be:
    Code:
    while (!(cin >> hold) || (hold != 'Y' && hold != 'y' && hold != 'X' && hold != 'x'))
    {
       cin.clear();
       cin.ignore(1000, '\n'); 
       cout<<"Please try again"<<endl;
    }
    You loop while the read fails or the character is not one of the correct inputs.

    Also, the ignore() will only ignore a single character, so if the user types a sentence it will not help unless you change it to the way SlyMaelstrom had it (or how I have it above).
    Hi, the program seems to go into a loop if the user enters a sentence even with that code I guess its because its a char variable I have to store it in.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> while (!(cin >> hold)

    The operator>> returns the stream which evaluates to true if the stream is in a good state and false if it is in a bad state. So if the read fails, it will evaluate to false. If the read succeeds, it will evaluate to true and the loop won't run any more (unless the other conditions after the or are met). This rarely matters if hold is a char because anything typed by the user is a character and so it shouldn't fail very often. If you were reading into an int or double, and the user typed a charcter like 'a', then the read would fail and that code would trigger the loop to run and display the error message.

    >> Hi, the program seems to go into a loop if the user enters a sentence even with that code I guess its because its a char variable I have to store it in.

    I don't understand this, maybe you could post your latest code and the example input.

  15. #15
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Below is the code I have used and thanks for the explaning the code.

    Code:
    while (!(cin >> hold) ||(hold != 'Y' && hold != 'y' && hold != 'X'&& hold != 'x'))
    		{
    			cin.clear();
    			cin.ignore(1000,'\n');
    			system("cls");
    			cout<<"error try again"<<endl;
    
    		}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  2. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 02:53 PM
  3. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 09:53 AM
  4. Getting user input for filename loop
    By jpchand in forum C++ Programming
    Replies: 1
    Last Post: 09-16-2003, 06:37 AM
  5. comparing user input
    By lambs4 in forum C Programming
    Replies: 5
    Last Post: 12-15-2002, 09:28 AM

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