Question

Question: Is there a more efficient method to using operator or "|| " than what I'm using?

I'm creating the HangMan Game in which players type a letter and the only way to get out of the loop is by entering the correct word in which this case if statement is the only way out.

I'm running into a problem with my compiler, maybe is that i need to work on a better and cleaner version of my code but here is what I'm currently have so far...

// w is the word chosen
if(w1=='d' && w2=='u' && w3=='c' && w4=='k' || answer ==  error)
 {


  if(w1=='d' && w2=='u' && w3=='c' && w4=='k')
     {
     cout << "The word is correct \n";
     }
     else if (answer == error)
     {
     cout << " You got 5 strike you lost \n";
     }
 }

I'm currently using Qt Project Compiler in Ubuntu to Compile my C++ Program.

I don't have this error/ suggestion with Gcc g++ on the Command Line

/home/cristian/Qt_Programs/Hangman_Game/main.cpp:123: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
Était-ce utile?

La solution

You don't need the first if statement. The second if-else will accomplish the same with or without it. That should remove the compiler suggestion.

if (w1 == 'd' && w2 == 'u' && w3 == 'c' && w4 == 'k') {
    cout << "The word is correct \n";
}
else if (answer == error) {
    cout << " You got 5 strike you lost \n";
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top