-2

I'm having problems checking for similar string input from the user and i'm pretty sure i'm not getting my concepts wrong. I'm afraid i might be missing out something.

I'm trying to ensure that the user input only grade A-F therefore i tried doing the input check like shown below but it doesn't seem to be working correctly.

string grade;
bool input=false;

while (!input)
{
    cout << "Please enter grade : ";
    getline(cin, grade);

    if (grade == "Grade A" || grade == "Grade B" || grade == "Grade C" || grade == "Grade D" || grade == "Grade E" || grade == "Grade F")
    {
        input = true;
        cout << "Successful input " << endl;
    }
    else
    {
        cout << "Please enter the correct input" << endl << endl;
        input = false;
    }       

}

For example if i type Grade A, it's suppose to return successful input while exiting out of the loop however it doesn't seem to work. I'm currently using Visual Studio 2015 to program this application.

  • '\n' doesn't work and i do know what getline is suppose to do. – Astral Zhang Oct 16 '17 at 21:12
  • 2
    Your cin.ignore is the problem, just remove it. You remove the 100 first typed char before the first '\n', so i assume you hit enter 2 times to get your string – grifos Oct 16 '17 at 21:12
  • 2
    Forcing people to type in, literally, "Grade A", which is case sensitive, is asking a lot. Why not just "A", case insensitive? – tadman Oct 16 '17 at 21:13
  • @grifos i removed the ignore to solve the double enter issue – Astral Zhang Oct 16 '17 at 21:14
  • @tadman my application doesn't simply check for grade. i'm just replicating an easier example of my problem as my actual program requires me to check for exact input. – Astral Zhang Oct 16 '17 at 21:14
  • @Astral And you still got the problem? Does the input contain some special chars? – grifos Oct 16 '17 at 21:15
  • @grifos no it doesn't. it just requires simple input for example like `Type B` or `Grade B` with the exact same case sensitive which should not be an issue for string in C++ – Astral Zhang Oct 16 '17 at 21:17
  • @Astral i'm perplex, it should work. If you are using VS2015, just put a breakpoint after the getline to see what is extracted, maybe it'll help you. Because so far the code you posted works just fine without the cin.ignore – grifos Oct 16 '17 at 21:18
  • @Astral I don't know what happened before your ask for a new input, but maybe you need to clear the cin buffer cf: https://stackoverflow.com/questions/257091/how-do-i-flush-the-cin-buffer – grifos Oct 16 '17 at 21:23
  • you're right. it seems that i need to clear the cin. apparently, i can't check for a string input after entering an int. the weird part is, when i put a breakpoint, it returns me the correct string but it doesn't seem to check properly – Astral Zhang Oct 16 '17 at 21:30

1 Answers1

0

The best way to go about solving this problem is using the debugger or cout to understand where things are going wrong. My first step would be to find out what the value of grade is after using getline(). If grade does not equal what you typed in, something is obviously wrong.

Remember there is always an alternative to getline(). Instead, you could use:

 cin << grade;
Devin Haslam
  • 747
  • 2
  • 12
  • 34