简体   繁体   中英

while-loop doesn't end in c++

So, this is a small piece of code out of my bigger c++ program. All of the relevant parts are included.

char weekDay;
char inputWeekDay;
char daysInTheWeek[7] = { 'm','t','w','p','f','s','z' } ;
int days = 1723;

weekDay = daysInTheWeek[days%7] ;
cout << weekDay << endl ;

cin >> inputWeekDay ;

while (inputWeekDay != ('m'&'t'&'w'&'p'&'f'&'s'&'z')) {
    cout << "Your input isn't one of the above.\nTry again." << endl ;
    cin >> inputWeekDay ;
}
if (inputWeekDay == weekDay) {
    cout << "Your input is correct." ;
} 
else {
    cout << "Your input isn't correct.\nYou will now get kicked out of this program." ;
    return 1;
}

When I run this part, and I type for example 'g' when I get asked for inputWeekDay, it gives me 'Your input isn't right.\\nYou will now get kicked out of this program.' error and asks me again, which is what should be happening. But after that when I type for example 'm', which isn't equal to weekDay but isn't one of the conditions in the while-loop, it keeps me in the while-loop and I get the error again. The same goes for when I type in the right 'weekDay', it still gives me the error message.

Any ideas what could cause this? Thank you in advance.

Your condition is comparing whether inputWeekDay is equal to the bitwise-AND of many characters.

Presumably you want to compare whether it's equal to any of the characters.

You could do that with a condition like this:

while ( inputWeekDay != 'm'
    && inputWeekDay != 't'
    && inputWeekDay != 'w'
    ...

But that gets old fast. Another way would be to use a switch statement in a do-while loop.

bool isValid = false;
do {
    switch ( inputWeekDay ) {
       case 'm':
       case 't':
         ...
         isValid = true;
         break;

       default:
         isValid = false;
         break;
   }
   if ( ! isValid ) {
       cout << "Your input isn't one of the above.\nTry again." << endl ;
       cin >> inputWeekDay ;
   } 
} while ( ! isValid );  

Your while has to be something like:

while(inputWeekDay != 'm' && inputWeekDay != 't' && ...)

So that you are really checking that inputWeekDay is really different to all the given values.

EDIT: you can make it much easier as follows:

std::string weekDay("mtwpfsz");
...
while (str.find(inputWeekDay)!=std::string::npos)
{
    ...
}

You are doing a bitwise AND operation on all of your chars, when what you want is a logical AND, also you must list each condition separately. C++ does not support such a style as you have in your while statement.

while (inputWeekDay != ('m'&'t'&'w'&'p'&'f'&'s'&'z')) {

should become

while ( inputWeekDay != 'm'
        && inputWeekDay != 't'
        && inputWeekDay != 'w'
        && inputWeekDay != 'p'
        && inputWeekDay != 'f'
        && inputWeekDay != 's'
        && inputWeekDay != 'z'){

I think problem is with bitwise-AND operator(&). You should use logical and(&&).

Since they're char s, you could spare yourself the loop and declare:

const char *daysInTheWeek = "mtwpfsz";

then:

while (strchr(daysInTheWeek, inputWeekDay) == NULL) {
  // ...
}

The problem is that you wrote the condition incorrectly. Operator & is the bitwise AND operator.

To check that the entered value is among the values of the array it is better to use standard algorithm std::find

For example

while ( std::find( daysInTheWeek, daysInTheWeek + 7, inputWeekDay ) == daysInTheWeek + 7 )
{
   //...
}

This loop can be written also like

while ( std::find( std::begin( daysInTheWeek ), std:;end( daysInTheWeek ), inputWeekDay ) == std::end( daysInTheWeek ) )
{
   //...
}

Or you could define array daysInTheWeek as

char daysInTheWeek[] = { "mtwpfsz" };

and use standard C function strchr For example

while ( std::strchr( daysInTheWeek, inputWeekDay ) == NULL )
{
   //...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM