Public Group

# A little help with if statements..

This topic is 4438 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Im trying to make a little program that gives u different responses based on your answer. For some reason it will ask you "how many times has cheech beaten you in ...?" lets say u answer 8 it will say "Nope, guess again." if u guess again and enter a 4 it skips the of the else if code and goes to straight to the else code. idk if im explaining this right. here is the code.. i'd appreciate help on my coding plz.. thx #include <iostream> using namespace std; int main() { int x, y; (x == 7); cout << "How many times as Cheech beaten you in Albatross18? "; cin >> x; cin.ignore(); if (x > 7){ cout << "Nope. Guess again... "; cin >> x; } else if (x < 7){ cout << "No way. More than that!! "; cin >> x; } else (x == 7);{ cout << "You **** right!! ..grr... You DANG right!! " << endl; } cout << "What do you think about that?? "; cin >> y; cout << "IT DOESNT MATTER WHAT YOU THINK! " << endl; system("PAUSE"); }

##### Share on other sites
Doesn't the compiler give you a warning about the following?

else (x == 7);

##### Share on other sites
No I had it else (x == 7) and tried to run it but it told me to put the semicolon there.

##### Share on other sites
There are various mistakes in the code. First of all:

Quote:
 int x, y;(x == 7);

I don't know what you think (x==7) does, but all it does is return a boolean value, true or false, depending whether x equals 7 or not. It doesn't actually do anything, because that value is not used. The compiler lets you do it(for example you can even write a line that all it contains is "42;" and it will compile), but it's hardly ever useful. If you want to assign 7 to x you will do x=7. But since x is inputed by the user in the next line, I don't really understand what you intended to do. Anyway...

Quote:
 if (x > 7){cout << "Nope. Guess again... ";cin >> x;}else if (x < 7){cout << "No way. More than that!! ";cin >> x;}

That says: If x>7 execute the first block, else if x>7 execute the second. The else means only one of them will be executed, never both. It doesn't matter if "x" is changed inside the first block, because the evaluation (x>7) has happened before that.

Quote:
 else (x == 7);{cout << "You **** right!! ..grr... You DANG right!! " << endl;cin >> x;}

Again, the first line does pretty much nothing. I suppose you meant to write "else if (x==7)", but you need to also lose the semicolon. The next block is not part of the "else", it's just an irrelevant block(you can use {} to define blocks that don't have anything to do with ifs or loops) that gets executed always no matter what.

##### Share on other sites
EDIT: bah, someone beat me [grin]
------------------------------

First of all, just 'else' never has an expression. So "else ( x == 7 )" gives an error (which may be somewhat confusing, but still :P). You can only use an expression with if and else if - else is just for catching anything you missed with the if and else if statements...

So, in this case it'd be best to use another else if for when the user guesses the correct answer and use another else to catch off any wrong answers.

#include <iostream>using namespace std;int main(){    int x, y;    // (x == 7);  <=== I commented this out because it doesn't do anything      cout << "How many times as Cheech beaten you in Albatross18? ";    cin >> x;    cin.ignore();    // I added endl's or it would all clutter up on one line    if ( x > 7 ) {        cout << endl << "Nope. Guess again... ";        cin >> x;    }    else if ( x < 7 ){        cout << endl << "No way. More than that!! ";        cin >> x;    }    else if ( x == 7 ) {        cout << endl << "You **** right!! ..grr... You DANG right!! " << endl;    }    else {        cout << "You were horribly wrong!" << endl;    }    cout << "What do you think about that?? ";    cin >> y;    cout << endl << "IT DOESNT MATTER WHAT YOU THINK! " << endl;    system("PAUSE");}

This should work unless there are typo's from my side in it :P A few comments though, your second line in main() doesn't do anything. It says ( x == 7 ); but at the point you do that x has not been initialized. The expression will therefore result in a 'false' but what use is it anyway?

Other than that and the extra 'else if' and 'else' it was alright, except for a few end of lines to prevent the output from getting all on one line ;)

##### Share on other sites
(1) (x == 7); does nothing. == tests for equality, but doesn't actually change anything. This appears twice in your code...this refers to the first instance. I'll get to the second one in a bit.
(2) When you have a series of else if blocks, at most one will be executed, and the the program continues at the end of them all. So if you input something less than 7, the user is asked to input a new value, and then the program immediately jumps to the "what do you think about that" line. Or would, if we didn't have (3) coming up. The important thing is that it doesn't try and recheck the values with your updated x...the else if is ignored, even if you guess correctly the second time. You need a loop to pull that off.
(3) You don't actually need braces to define an if block. Without them, only the next line of code will be executed. This is where that second (x == 7); comes in. That else is followed by a single statement, so that is all that gets associated with it. The stuff in the actual braces will always be executed, because they aren't part of the conditional at all, even though their formatting suggests they are.
(4) Somebody will chastise your use of system("PAUSE"). Listen to what they say, then decide on your own. It isn't that big a deal.

Example [hopefully] clarifying #3:
if(x == true)   cout << "Only shown if x is true" << endl;cout << "Always shown." << endl;{   cout << "Also always shown...that brace doesn't affect execution order at all." << endl;}

CM

##### Share on other sites
One should use the Case statement for this.

##### Share on other sites
wow thanks everyone... its took me a while to absorb all of that. seems like all of your advice has helped me tremendously. im tryin to see how/where i can put a loop so it can use the else if's.

what are case statements?

##### Share on other sites
My advice: Find a c/c++ book that people suggest, buy it used on amazon, learn to code and have fun.

##### Share on other sites
Quote:
 Original post by dpadam450My advice: Find a c/c++ book that people suggest, buy it used on amazon, learn to code and have fun.

i bought a c++ book only to learn after that it got horrible reviews. Most of the code in the book doesnt even work. Im going by online tutorials now until i can afford them.

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633300
• Total Posts
3011266
• ### Who's Online (See full list)

There are no registered users currently online

×