I have no idea why this isn't working - must be missing something simple

S

SB

This while loop keeps repeating even when a correct character is entered....

cout<<endl<<"What day would you like to schedule the appointment?"<<endl;
cout<<endl<<"Enter 'M' for Monday, 'T' for Tuesday, 'W' for Wednesday, 'H'
for Thursday \n"<<
"and 'F' for Friday: ";
cin>>day;
while (day != "M" || day != "m" || day != "T" || day != "t" ||
day != "W" || day != "w" || day != "H" || day != "h" ||
day != "F" || day != "f")
{
cout<<endl<<"Enter 'M' for Monday, 'T' for Tuesday, 'W' for Wednesday,
'H' for Thursday \n"<<
"and 'F' for Friday: ";
cin>>day;
}

cout<<endl<<"You entered "<<day<<endl;

No matter what character is input, it repeats. If I check for just "M" and
enter that it works. As soon as I add the first || it fails. What is wrong?

Thanks!
 
G

Gianni Mariani

SB said:
This while loop keeps repeating even when a correct character is entered....

cout<<endl<<"What day would you like to schedule the appointment?"<<endl;
cout<<endl<<"Enter 'M' for Monday, 'T' for Tuesday, 'W' for Wednesday, 'H'
for Thursday \n"<<
"and 'F' for Friday: ";
cin>>day;
while (day != "M" || day != "m" || day != "T" || day != "t" ||
day != "W" || day != "w" || day != "H" || day != "h" ||
day != "F" || day != "f")
{
cout<<endl<<"Enter 'M' for Monday, 'T' for Tuesday, 'W' for Wednesday,
'H' for Thursday \n"<<
"and 'F' for Friday: ";
cin>>day;
}

cout<<endl<<"You entered "<<day<<endl;

No matter what character is input, it repeats. If I check for just "M" and
enter that it works. As soon as I add the first || it fails. What is wrong?

You're using the wrong expression and I assume std::string day;

e.g. if day == "M" then it's obvious that day != "m" and hence the while
expression allways evaluates to true.

Try this.


while ( day != "M" && day != "m" ... etc


Now, you'll also have problems with different languages which use
different letter abbreviations.
 
R

Rob Williscroft

SB wrote in in comp.lang.c++:
This while loop keeps repeating even when a correct character is
entered....

cout<<endl<<"What day would you like to schedule the
appointment?"<<endl; cout<<endl<<"Enter 'M' for Monday, 'T' for
Tuesday, 'W' for Wednesday, 'H' for Thursday \n"<<
"and 'F' for Friday: ";
cin>>day;
while (day != "M" || day != "m" || day != "T" || day != "t" ||
day != "W" || day != "w" || day != "H" || day != "h" ||
day != "F" || day != "f")
{
cout<<endl<<"Enter 'M' for Monday, 'T' for Tuesday, 'W' for
Wednesday,
'H' for Thursday \n"<<
"and 'F' for Friday: ";
cin>>day;
}

cout<<endl<<"You entered "<<day<<endl;

No matter what character is input, it repeats. If I check for just "M"
and enter that it works. As soon as I add the first || it fails. What
is wrong?

Change all the || to && (logical or to logical and)

HTH.

Rob.
 
T

Thomas Matthews

SB said:
This while loop keeps repeating even when a correct character is entered....

cout<<endl<<"What day would you like to schedule the appointment?"<<endl;
cout<<endl<<"Enter 'M' for Monday, 'T' for Tuesday, 'W' for Wednesday, 'H'
for Thursday \n"<<
"and 'F' for Friday: ";
cin>>day;
while (day != "M" || day != "m" || day != "T" || day != "t" ||
day != "W" || day != "w" || day != "H" || day != "h" ||
day != "F" || day != "f")
{
cout<<endl<<"Enter 'M' for Monday, 'T' for Tuesday, 'W' for Wednesday,
'H' for Thursday \n"<<
"and 'F' for Friday: ";
cin>>day;
}

cout<<endl<<"You entered "<<day<<endl;

No matter what character is input, it repeats. If I check for just "M" and
enter that it works. As soon as I add the first || it fails. What is wrong?

Thanks!

You could simplify this:
const std::string day_letters("MTWHFmtwhf");
char day;

cin >> day;
// check for any stream errors first!
while (day_letters.find(day) == std::string::npos)
{
cout << '\'' << day << "\' is not a valid letter.\n";
cout << "Enter 'M' for Monday, 'T' for Tuesday,"
" 'W' for Wednesday, 'H' for Thursday \n"
"and 'F' for Friday: ";
cout.flush(); // make sure the text is displayed.
cin >> day;
}

Or one could use the std::toupper or std::tolower to
convert to one letter-case and reduce the comparison
string:
const std::string day_letters("mtwhf");
char day;

cin >> day;
// check for any stream errors first!
day = std::tolower(day);
//...
cin >> day;
day = std::tolower(day);


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
V

Victor Bazarov

I think SB should examine this condition. Imagine I have a number. Let's
name it 'N'. Then I have a condition (N != 1 || N != 2). What's its value?
If N is neither 2 nor 1, first we test it against 1. It does not equal 1,
so the first one is true. We stop checking and execute the controlled
statement. Now, if N is 1, we begin again. N != 1? False. Keep going:
N != 2. Sure. 1 != 2, true. Execute the controlled statement. Now, N is
2.
Is N not equal 1? Sure. 2 != 1, true, execute the controlled statement.

WTF? How come we always execute the controlled statement? Simple. How can
the logical expression be completely false? Only if BOTH parts of it are
false. When is the first one false? When N equals 1. When is the second
one false? When N equals 2. N cannot SIMULTANEOUSLY be 1 and 2 to make
both parts false. So, at least one of them is always true.

Can we do anything about it? Of course. When do you want to keep asking
the user for the correct input? If the day is neither of the accepted
values. How do you achieve the "neither" condition? You use 'AND', not
'OR'.

if (day != "M" && day != "m" ...
^^^^

Now, another problem is how you declared 'day'. If it's the same as Thomas
suggested:

char day;

then you will NEVER get the right answer if you try comparing it with a
string
literal (something in double quotes). Although in that case the compiler
will
complain about comparing a char to a pointer. You probalby declared it
'string',
which is OK, as long as the user always enters ONE character. If they enter
MO
or monday, the comparison will again fail. You should think of comparing
only
the first character of 'day'.

Anyway, enough for now?
 
D

Dan Moos

Like the other folks said, your "OR"s should be "ANDS"

But on another note, isn't this what "switch" expresions are for?:

std::tolower(day);
switch (day){

case "m":{}

case "t":{}

case "w":{}

case "h":{}

case "f":
{
DoStuff();
break;
}
default:
{
TryAgain();
}
}
 
O

Old Wolf

Dan Moos said:
Like the other folks said, your "OR"s should be "ANDS"

But on another note, isn't this what "switch" expresions are for?:

std::tolower(day);

Undefined behaviour - std::tolower takes an unsigned int.
Note that this does not alter the value of 'day' either.
switch (day){

case "m":{}

'day' is either a char or a std::string (the OP didn't say), but
in neither case is it going to match the string literal "m"
case "t":{}

case "w":{}

case "h":{}

case "f":
{
DoStuff();
break;
}
default:
{
TryAgain();
}

A lot of superfluous braces here
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top