int userChoice(){
   int choice;
    cin>> choice;
    while (choice!=1 && choice!=2){
          cout<<"that is not a valid choice please choose again ";
          cin>>choice;}
    return choice;
}

the whole point of this is that if a user doesnt enter 1 or 2 then it tells them to enter 1 or 2 and wont exit until they do, it works if they enter the wrong number but if they enter a letter it just loops infinitly not giving them a chance to enter a 1 or 2 what am i doing wrong? thanks!

Recommended Answers

All 9 Replies

Here's whats happening, you trying to store integer values in the variable choice everything is fine until the user enters a letter cin object goes into an error state and nothing gets stored in the variable choice.

the problem is that if you try to enter a letter into a integer it will fail and cause cin to enter an error state and it will cause your program to loop forever. if you want to make it bulletproof you will have to get the input as a string and then test it to see if it is what you want.

[EDIT]
Crutoy beet me.
[/EDIT]

i dont understand any of that

thelamb gave you a link to show you how to clear out any extraneous characters out of the input be it a number or letter. When the user enters a number everything is fine because it is an integer, once you enter something other than an integer the program ceases to work properly. A quick solution would be say using char data type and checking for the values enclosed in ''.

Edit2: http://www.daniweb.com/forums/thread349026.html Two questions of the same exact thing by two different people. Fun stuff.

i dont understand any of that

Check out the isdigi() and isalpha() functions please.. To understand the code below.. The guys above are absolutely correct but clearing the input buffer will not help here.. instead a work around is to declare choice as a char and then to call isdigit() or isalpha() to check.. if it is a digit, run your while loop.. if it is a letter, re-run function userChoice(); and return choice;

Note: since it choice is of type char, you must use '1' (single quotes!) instead of.. 1 or "1"..

#include <cctype>   //This must be used as isdigit() and isalpha() is defined here.

int userChoice()  //Function userChoice();
{
    char choice;   //Declare choice as type char.
    cin>>choice;    //char choice accepts user input.
    if(isdigit(choice))   //check if the input in char choice is a digit.
    {
        while((choice != '1') && (choice != '2'))    //while choice is a digit and !=1 & !=2, run the code between {...} Note: '1' != "1"
        {
          cout<<"that is not a valid choice please choose again ";
          cin>>choice;      // Get the choice if it is digit, !=2 & !=1..
        }
    }
    else //Else if char choice is not a digit, run code between {...}
    {
        cout<<"Choice Is not a digit! ";
        userChoice();   //Since user entered an unacceptable choice (letter), rerun function userChoice();
    }
    return choice;
}

I added comments for extra explanation incase you dont read up on isdigit() and isalpha(), though I ask that u do. Also You must now use the #include <cctype> at the top of your program.

if(isdigit(choice)

this is not gonna work , assume the input has already been corrupted it will hold a garbage value. Instead , he should probably check to see if input has been successful by using cin.good() and then loop though asking user to re-enter the value.

if(isdigit(choice)

this is not gonna work , assume the input has already been corrupted it will hold a garbage value. Instead , he should probably check to see if input has been successful by using cin.good() and then loop though asking user to re-enter the value.

Uh might wanna check that again... it works :S I just compiled it.. as such:

[Edit]: Just read that you said "assume the input is already corrupted".. Uh I cant do that because the user has choice declared in the userChoice() function.. It if was declared outside the function or in main(), then I suppose you can assume that..

#include <iostream>
#include <windows.h>
#include <cctype>

using namespace std;


int userChoice()
{
    char choice;
    cin>>choice;
    if(isdigit(choice))
    {
        while((choice != '1') && (choice != '2'))
        {
          cout<<"that is not a valid choice please choose again ";
          cin>>choice;
        }
    }
    else
    {
        cout<<"Choice Is not a digit! ";
        userChoice();
    }
    return choice;
}

int main()
{
 userChoice();
}

i see, you declared it as character, never mind, i was gonna point out that op had it as integer and now you placing restriction on his input.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.