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

From: Victor Bazarov (v.Abazarov_at_comAcast.net)
Date: 04/21/04


Date: Wed, 21 Apr 2004 02:07:26 GMT


"Thomas Matthews" <Thomas_MatthewsSpitsOnSpamBots@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
>



Relevant Pages

  • Re: Compare two strings and find longest common sub-string
    ... In the simple case where you match character by character, white space has to match exactly, etc. you're probably better off starting by comparing the endpoints of the longest possible match, then reducing the comparison length. ... the first char, you scan the second string for the 2nd char in the first ...
    (microsoft.public.word.vba.general)
  • Re: using char in do while condition
    ... my fever is taking over me ... evaluates the expression as type "char" which is not a valid type for the ... by comparing the character as a range instead ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: DMG II
    ... laser and my characters dino laser resistant vest your character can no ... Still comparing fruit and meat. ... since offensive and defensive *equipment* can also exist in the ...
    (rec.games.frp.gurps)
  • Re: Why does this not work?
    ... Since you're converting your dates to character strings and then comparing ... SQL Server MVP ...
    (microsoft.public.sqlserver.programming)
  • Re: What this symbol mean?
    ... dolphin wrote: ... is just another character, and Linux accepts any non-nul character ... -- Thomas Matthews ...
    (comp.os.linux.misc)