0
votes

I'm very new to coding and have run into an odd roadblock with an if-else program. There are a lot comments in my code to explain what's going on, so I'll just post the code, but the basic problem is that when I try to simplify the nested statements, I only get one condition executed (overriding all the other conditions). For instance, if I want something to happen when "x = 4", only that condition gets executed--even if it's false! However, if I state it as "x < 5 && x > 3" then the program works fine (appropriate executions for true and false conditions), but that seems messy--especially since I want certain responses for multiple inputs.

I searched the site and couldn't find this specific issue, and the "Similar Questions" don't seem to apply either. So any idea what's going on? Is there something that can make the program execute only one if-else statement and ignore all the others, even if that statement is false?

Code: The problem is with the two elseif statements near the end (labelled with comments).

#include <iostream>

using namespace std;

int main()
{//This a program using loop functions to have the user
 //guess variable "guess" (which we'll make "5"), until the user gets it
 //right, and provides hints to help the user out
    cout<<"Can you guess the number I'm thinking? It's between 1 and 10.\n";
    //Prompts the user to input a guess
    int guess;
    //Declares the variable that the user will input
    cin>>guess;
    //Before the loop begins, the user input a guess
    cout<<"\n";
    //This gives us a line break after the answer is submitted
    while (guess != 5){//Guessing incorrectly starts the loop
                       //The program is going to keep asking the user to guess
                       //until the user guesses "5"
        if (guess < 1 ){//The program will warn the user if they're out of range, here it's too low
                cout<<"No, that's too low! Guess a number between 1 and 10.\n";
                cin>>guess; //Allow the user to guess again
                cout<<"\n"; //Add a line break after the input
    }
            else //Now, give responses for other conditions
                if(guess > 10){//Warn the user if they guess too high
                cout<<"Too high! Guess a number between 1 and 10.\n";
                cin>>guess;
                cout<<"\n";
            }
            else //Tell the user if they're getting close.
                 //Not sure why I can't simply say if "guess = 4,6"
                 //Doing so causes only this response to execute, ignoring
                 //the others, except the above ">" and "<" statements
                 //which is why I'm continung to use </> statements here
                if(guess > 5 && guess < 7 || guess < 5 && guess > 3){
                cout<<"VERY close! Keep trying.\n";
                cin>>guess;
                cout<<"\n";
            }
            else //Tell the user if they're sort of close
                 //Again, not sure why I can't just say "guess=3,7"
                if(guess > 2 && guess < 4 || guess > 6 && guess < 8){
                cout<<"Close, but no cigar. Try again.\n";
                cin>>guess;
                cout<<"\n";
            }
            else {//For all other responses, we output this
                cout<<"Guess again.\n";
                cin>>guess;
                cout<<endl; //We need to end the loop here, as these are all the conditions
                //This kept creating a line break. My assumption is that
                //by virtue of ending the loop, a line break was created
            }
    }
    if(guess = 5){//Outside the loop, we need to specify the correct answer
                  //because the loop is only working while the answer in incorrect
        cout<<"Good job. "<<guess<<" is right!\n";
    }
    cin.get();//This keeps the program from closing after completion
}
//Done
3
Look up the difference between = and ==.Beta
Turn up your warning level and your compiler should have something to say that really helps.chris
"want something to happen when "x = 4", only that condition gets executed"... x = 4 assigns the value 4 into x, overwriting any previous value, then checks whether that's true in a boolean sense - which it always is as non-zero numbers are considered true. Your condition should be x == 4.Tony Delroy
According to a comment to one of the answers, the only problem was that assignment was used instead of comparison. Voting to close as too localized.jogojapan

3 Answers

1
votes

One point is:

 if(guess = 5){

should be

 if(guess == 5){

You really mean logical comparison not assignment.

Another point is that according to C++ Operator Precedence, && has higher precedence than ||. you should use () in your logical conditions to clearly express your logic:

 if((guess > 2 && guess < 4) || (guess > 6 && guess < 8))
0
votes

When talking integers,

(guess > 5 && guess < 7 || guess < 5 && guess > 3)

is equivalent to

(guess == 6 || guess == 4)

Therefore, as a minimum. restructure as follows:

while (guess != 5) {
    if (guess < 1 ) {
    }
    else if (guess > 10)
    }
    else if (guess == 6 || guess == 4) {
    }
    else if (guess == 3 || guess == 7) {
    }
    else { // executes for 1, 2, 5 (then leaves loop), 8, 9, 10
    }
}

Also you mean ...

if (guess == 5) // equivalence check

not

if (guess = 5) // assignment and "if" becomes true (always)
0
votes

In C++, it's not syntactically incorrect to ask if(x = whatever). However, it doesn't do what you think it does.

Here's the difference:

//is the value of x equal to 4?
if(x == 4) 

//does the assignment of 4 to the variable x return true? (meaning it worked)`
if(x = 4)

So what's happening is that what you think is false is actually true.