0

I'm asking the user for an input, but I want the question to stay on screen until the input meets one of the allowed inputs. Here's my code

string input = "";
string departure = "";

cout << "Please enter an airport code: ";

do 
{
    getline(cin,input);
    stringstream(input) >> departure;

} while(departure.compare("MAN") != 0 || departure.compare("EMA") != 0 || departure.compare("LHR") != 0 );
}

I want it to loop until the user enters MAN or EMA or LHR; also if they are lowercase I would like for it to be accepted aswell.

Every time I run this, even if I enter a correct input, it just keeps taking words in and doesn't do anything else.

Khalid
  • 303
  • 1
  • 5
  • 16

5 Answers5

3

The condition

 departure.compare("MAN") != 0 || departure.compare("EMA") != 0 || departure.compare("LHR") != 0

is always true, regardless of what departure is.

compare returns 0 on equality. So what you're basically telling the compiler is

Run the loop while departure is different than "MAN" OR different than "EMA" OR different than "LHR".

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
1

You need && instead of || in your condition.

This condition always returns true since it can't not be all 3 at once.

The && will return false as soon as the input is one of the 3 accepted.

Bun
  • 1,465
  • 10
  • 23
1

Consider using boost::to_upper to convert the input into upper case before you perform the comparison in the while(...) statment. This will resolve the lowercase/uppercase issue.

http://www.boost.org/doc/libs/1_41_0/doc/html/boost/algorithm/to_upper.html

Also, when dealing with C++ strings, I recommend you simply do

departure == "MAN" || departure == "EMA" || departure == "LHR"

You don't need to do string.compare in C++, unlike some other languages (for example Java), as the == operator is overloaded to compare the /content/ of the string, rather than the string object itself.

Also somebody else beat me to it about the compare method returning 0 when equal.

Mike B
  • 478
  • 2
  • 11
0

First Your conditional for the while loop is incorrect. Right now it reads, while departure is not 'MAN' or is not "EMA" or is not "LHR", continue looping. Because departure cannot be all three of them simultaneously, the loop never ends. I would suggest replacing your OR's (||) with AND's (&&)

As well, each execution of the loop you need to clear the value in departure, otherwise the previously entered lines persist and your comparison will fail even when a correct airport code is entered.

waldol1
  • 1,841
  • 2
  • 18
  • 22
0

our main problem is that the string is being compared incorrectly. Let's say we type in "MAN".

The departure.comare("MAN") != 0 will be true if the string is not "MAN". Fine, we typed in "MAN", so it's false. Now we OR that with departure.compare("EMA") != 0 - which is true, because "MAN" is not equal to "EMA". So you need to combhine your condition with &&, not ||.

To fix for "owercase", there are two choices. Either convert the input string to uppercase, or compare with all different combinations of lower and upper case (Man, MaN, mAn, etc) - the latter gets very tedious very quickly.

Have a look at this one for some options of comparing strings in a case-insensitive way: Case insensitive string comparison C++

Community
  • 1
  • 1
Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • Thanks, but when I enter incorrect input, it goes onto next line, is there anyway to keep it next to question? – Khalid May 15 '13 at 15:07