A little help with if statements..

Started by
16 comments, last by cheech306 17 years, 7 months ago
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");
}

Advertisement
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 loop
while ( x > 7 ){
cout << endl << "Nope. Guess again... ";
cin >> x;
}

//Second loop
while ( 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?
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 loop
while ( x > 7 ){
cout << endl << "Nope. Guess again... ";
cin >> x;
}

//Second loop
while ( 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.
Try writing out the process in English first. Make it as detailed as you can.
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.

Chad.
Think about this, at what point should it stop asking you for more guesses? Make a loop that continues while that condition is false.
Quote:Original post by Zahlman
Try 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.
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.

This topic is closed to new replies.

Advertisement