By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,864 Members | 1,293 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,864 IT Pros & Developers. It's quick & easy.

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

P: n/a
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!
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
SB wrote:
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.
Jul 22 '05 #2

P: n/a
SB wrote in news:hB0hc.7567$2e6.1239@lakeread01 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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3

P: n/a
SB wrote:
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

Jul 22 '05 #4

P: n/a
"Thomas Matthews" <Th****************************@sbcglobal.net> wrote...
SB wrote:
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")
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?
{
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

Jul 22 '05 #5

P: n/a
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();
}
}

Jul 22 '05 #6

P: n/a
"Dan Moos" <da******@verizon.net> wrote in message news:<xW******************@nwrddc03.gnilink.net>.. .
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
}

Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.