# A little help with if statements..

This topic is 4176 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.

##### Share on other sites
ok everything is fixed (Thanks to everyone!) but i cant get the loops to work right. It runs fine IF you input a number greater than 7 and then input one lower than 7. any other way it goes straight to the question "What do you think about that??". Not sure on how to fix it.

#include <iostream>

using namespace std;

int main()
{
int x, y;

cout << "How many times as Cheech beaten you in Albatross18? ";
cin >> x;
cin.ignore();

while ( x > 7 ){
cout << endl << "Nope. Guess again... ";
cin >> x;
}
while ( x < 7 ){
cout << endl << "No way. More than that!! ";
cin >> x;
}
if (x==7) {
cout << endl << "You **** right!! ..grr... You DANG right!! " << endl;
}

{
cout << "What do you think about that?? ";
cin >> y;
}
cout << endl << "IT DOESNT MATTER WHAT YOU THINK! " << endl;

system("PAUSE");
}

##### Share on other sites
Well, think about it. Just try to follow the flow of the program. What it will happen if you first enter a number <7 and then >7?

Quote:
 //First loopwhile ( x > 7 ){cout << endl << "Nope. Guess again... ";cin >> x;}//Second loopwhile ( x < 7 ){cout << endl << "No way. More than that!! ";cin >> x;}

If you enter '6', it will not enter the first while loop, and it will enter the second. It will display 'More than that' and input a new 'x'. If you enter '8', it will exit the second loop and continue to execute the next instructions. Why would it return to the first loop?

The user may enter numbers greater or less than 7 without any particular order. In contrasts, your program is constructed by 2 seperate loops executed in a very particular order. Think about it: What do you want to do? When the program must ask the user to input a new number? The answer is, when it is less than 7 or when it is greater than 7. In other words, when it's not 7. How would you handle that with a single loop?

##### Share on other sites
Quote:
Original post by mikeman
Well, think about it. Just try to follow the flow of the program. What it will happen if you first enter a number <7 and then >7?

Quote:
 //First loopwhile ( x > 7 ){cout << endl << "Nope. Guess again... ";cin >> x;}//Second loopwhile ( x < 7 ){cout << endl << "No way. More than that!! ";cin >> x;}

If you enter '6', it will not enter the first while loop, and it will enter the second. It will display 'More than that' and input a new 'x'. If you enter '8', it will exit the second loop and continue to execute the next instructions. Why would it return to the first loop?

The user may enter numbers greater or less than 7 without any particular order. In contrasts, your program is constructed by 2 seperate loops executed in a very particular order. Think about it: What do you want to do? When the program must ask the user to input a new number? The answer is, when it is less than 7 or when it is greater than 7. In other words, when it's not 7. How would you handle that with a single loop?

I really have no idea. I'm trying to make it a single loop but same results. I dont want to be told how becuz i want to figure this out myself but would appreciate a slight clue.

##### Share on other sites
Try writing out the process in English first. Make it as detailed as you can.

##### Share on other sites
yes, think about the program, what you want it to do, and think about everything you have learned so far in coding, and see what could work. Write down many things that could work, and write them out in english (like the above posted said). That way you follow the code. After you think you got it, code it and see how it works! Just keep on thinking. Don't ever give up.

I hope that helps.

##### Share on other sites
Think about this, at what point should it stop asking you for more guesses? Make a loop that continues while that condition is false.

##### Share on other sites
Quote:
 Original post by ZahlmanTry writing out the process in English first. Make it as detailed as you can.

This is one of those things I was taught in university, and hated. We had to do pseudo code before any programming and had to submit both our pseudo code and regular code as part of the project. We all thought our prof was just being an asshole. Even worse, if you knew the language well, it just seemed like a waste of time.

However, even the guys that knew the programming language as well as they knew english often caught bugs in their logic when going through this exercise. Or, often times, if their were bugs in their code when they finished the assignment, the bug was also in their plain english description of their code.

Granted, years later, I have almost completely dropped this process, unless the code im writing is very confusing to me. Then again, I code by contract alot now, which is basically a black box approach to the exact same thing.

It disappoints me to see that almost no introductory programming texts encourage this method. Outside a few books on programming ( such as Code complete, which every programmer should own ), I cant think of too many.

Any introductory text, especially one that proposes to teach programming in X days, should really encourage the reader to flesh out their concept outside the programming language first, then replace that with code.

Then again, most programming books are crap. Hate to say it, but its true.

##### Share on other sites
I GOT IT! I GOT IT...! ...thats exactly what i said with my fists in the air lol. thanks everyone!! Feels so good to finally accomplish this. Right now I feel like i 9wn c++ lmao.. thanks.. i never really knew what to do until i just sat down and really thought about it like u guys said to do.

I just put :

(x == 7);
do{
while ( x > 7 ){
cout << endl << "Nope. Guess again... ";
cin >> x;
cin.ignore();
}
while ( x < 7 ){
cout << endl << "No way. More than that!! ";
cin >> x;
cin.ignore();
}}while (x==false);

while (x > 7){
cout << endl << "Nope. Guess again... ";
cin >> x;
}

no.... nevermind. it doesnt work lol.