Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > I have no idea why this isn't working - must be missing something simple

Reply
Thread Tools

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

 
 
SB
Guest
Posts: n/a
 
      04-20-2004
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!


 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      04-20-2004
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.


 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      04-20-2004
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/
 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      04-20-2004
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

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      04-21-2004
"Thomas Matthews" <(E-Mail Removed)> 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
>



 
Reply With Quote
 
Dan Moos
Guest
Posts: n/a
 
      04-23-2004
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();
}
}





 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      04-23-2004
"Dan Moos" <(E-Mail Removed)> wrote in message news:<xW1ic.26626$(E-Mail Removed)>.. .
> 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

> }

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I have to be missing something simple here. thermowax@gmail.com Ruby 4 02-19-2009 08:52 PM
I Must Be Missing Something Using 'Vista'? Bigbazza Computer Support 7 09-14-2008 06:58 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Weak references - I must be missing something... Mark M Java 16 03-05-2004 05:31 PM
must be missing something simple... luca passani Java 1 09-05-2003 02:43 PM



Advertisments