TakShing
TakShing

Reputation: 145

While loops in relation to alphabet characters

I am currently trying to understand why this problem is occurring with my code and I am not sure how to fix this..

int main()
{
    char answer;
    std::cin >> answer; // valid input is a, b, c or d
    while(answer > 'd') // doesn't enter no matter what letter I input
    {
        retry(); // function call to print something long..
        std::cin >> answer;
    }
    //rest of code not shown since everything is fine after this problem;
}

My goal of this program is to have the user input letters a, b, c, or d and after that it will trigger a "switch' function. So I figured that I can just have a while loop that keeps asking the user to enter a valid letter if they entered one not allowed. However, it doesn't seem to work?

Problem: I want while loop to activate when a letter greater than 'd' is entered, but the loop never happens no matter what letter I input.

Upvotes: 0

Views: 1369

Answers (1)

user2249683
user2249683

Reputation:

Some common mistakes regarding stream IO are:

  1. Testing for eof
  2. Ignoring the state of the stream after an extraction
  3. Similar to (2), checking the result (which might not have been
    altered)

Your fail regarding 2 and 3.

Upvotes: 1

Related Questions