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

Discussion in 'C++' started by SB, Apr 20, 2004.

  1. SB

    SB Guest

    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!
     
    SB, Apr 20, 2004
    #1
    1. Advertising

  2. Re: I have no idea why this isn't working - must be missing somethingsimple

    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.
     
    Gianni Mariani, Apr 20, 2004
    #2
    1. Advertising

  3. 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/
     
    Rob Williscroft, Apr 20, 2004
    #3
  4. Re: I have no idea why this isn't working - must be missing somethingsimple

    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
     
    Thomas Matthews, Apr 20, 2004
    #4
  5. "Thomas Matthews" <> 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
    >
     
    Victor Bazarov, Apr 21, 2004
    #5
  6. SB

    Dan Moos Guest

    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();
    }
    }
     
    Dan Moos, Apr 23, 2004
    #6
  7. SB

    Old Wolf Guest

    "Dan Moos" <> wrote in message news:<xW1ic.26626$>...
    > 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

    > }
     
    Old Wolf, Apr 23, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. luca passani

    must be missing something simple...

    luca passani, Sep 5, 2003, in forum: Java
    Replies:
    1
    Views:
    622
    luca passani
    Sep 5, 2003
  2. Mark M
    Replies:
    16
    Views:
    725
    Chris Uppal
    Mar 5, 2004
  3. Replies:
    5
    Views:
    327
    Patricia Shanahan
    May 22, 2005
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,999
    Smokey Grindel
    Dec 2, 2006
  5. Replies:
    4
    Views:
    128
    Brian Candler
    Feb 19, 2009
Loading...

Share This Page