While with logical operator "or" - C++

Asked

Viewed 110 times

-1

I’m having trouble finishing an algorithm.

When you insert a loop while In order not to allow an invalid form to be entered, the code shows an error in execution. If someone helps me by giving me some hint or solution, I am grateful.

Follows excerpt from the code:

printf("\n     DESEJA CONTINUAR O PROGRAMA?(YES ou NOT)== : ");                   
fflush(stdin);
gets(continuar);
strupr(continuar);
while((strcmp(continuar, "YES"!=0)) || (strcmp(continuar, "NOT"!=0))){
    printf("     \nDIGITE UMA FORMA VALIDA! (YES ou NOT)");                       
}       
  • 3

    "YES"!=0, that !=0 should not be out of function strcmp?

  • That’s right Anderson, my childish mistake. Thank you very much for your help.

1 answer

1

Misparenting:

while((strcmp(continuar, "YES")!=0) || (strcmp(continuar, "NOT")!=0)){

and I believe that the OU in this case will not meet what you expect since if it is either "YES" or "NOT" will continue running the loop which does not seem to me to be what you want.

Since you say you are using C++ why don’t you use the class compare <string>?

while ((continuar.compare("YES") != 0) && (continuar.compare("NOT") != 0)) {
  • Yes, you are right. I made a silly mistake inserting the logical operator together with the strcmp function, as I tested other ways before seeing the error, posted using the wrong operator, but already I correct the code. Thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.