while loop not behaving properly.
Ok, I've been searching two books for answers which I don't seem to find. They are:
1.)"Learning to Program in C++" By Steve Heller
2.) "The C++ Programming Language" Third Edition By Bjarne
Stroustrup
I'm using bloodshed C++ 4 on w2k.
my program is something I've actually tried to come up with myself. A little excersise to get the program to detect values above and below a certain predetermined number. my code follows:
#include <iostream>
#include <string>
int main()
{
using namespace std;
int correctguess;
correctguess = 33;
int attempt;
cout << "Pick a number between 0 and 100:" << endl;
cin >> attempt;
if (attempt == correctguess)
cout << "Thats right on the money!" << endl;
//while (attempt != correctguess)
//{
//cout << "Sorry thats not correct, please try again." << endl;
//cin >> attempt;
//}
//cout << "Thats correct!" << endl;
while (attempt > correctguess)
{
cout << "Try a little lower." << endl;
cin >> attempt;
}
while (attempt < correctguess)
{
cout << "Thats too low." << endl;
cin >> attempt;
}
return 0;
}
My problem is that if I run the program and type 33 then everything is cool, however if I type something over 33 the first while of the two that are not commented out goes into action telling me that my selection was too high.
If I then type a value lower than 33 I get the second while going.
My problem is after that sometimes the program just exits.
How do I get the while functions (are they functions?) keep on repeating until the correct value is reached? Am I missing something here?
The funny thing is that sometimes I can type in 55 three times and get a "Try a little lower." message, then type 1 three times and get a "Thats too low."
and then I try another number and it just quits, or if I throw it a curve and give it a 123 or a 456 the program exits.
Can somebody point me to some detailed reading regarding this stuff? I'm not trying to get work done for me, I'm trying to teach myself but have a ton of questions. And documentation is so scattered.
What am I doing wrong? I don't see any obvious mistakes but then again my knowledge of C++ is severely limited.
Is this one of those bugs thats hard to nail down?
Thanks to everyone who is putting up with this.
Re: while loop not behaving properly.
Quote:
Originally posted by Dreamerv3
Code:
#include <iostream>
#include <string>
int main()
{
using namespace std;
int correctguess;
correctguess = 33;
int attempt;
cout << "Pick a number between 0 and 100:" << endl;
cin >> attempt;
if (attempt == correctguess)
cout << "Thats right on the money!" << endl;
//while (attempt != correctguess)
//{
//cout << "Sorry thats not correct, please try again." << endl;
//cin >> attempt;
//}
//cout << "Thats correct!" << endl;
while (attempt > correctguess)
{
cout << "Try a little lower." << endl;
cin >> attempt;
}
while (attempt < correctguess)
{
cout << "Thats too low." << endl;
cin >> attempt;
}
return 0;
}
First, the using namespace std; should be under the #includes and before main().
With loops it helps to step through on paper for different values. The two while loops have the same problem. Look at the first one, where attempt > correctguess. Ok, you entered 55 and entered that while loop. cout prints and you enter again. If you enter 55, or any number greater than 33, the loop repeats.
So on the third try, you enter 33. attempt now equals correctguess, so you exit the loop. You don't enter the last loop, but then again, there's nothing to print the correct answer message now, so the program just ends. If you entered a number less than 33 on the third try, you'll enter the last loop.
If the first attempt you entered was less than 33, say 27, you enter the last loop but not the first loop. That error message will print, and you'll enter another number. If it's less than 33, you'll stay in the loop. Enter 33 or higher and the while condition fails and you exit the loop. Since, again, there's nothing to print a correct message, and nothing to check for a high value at this point, the program just ends.
You may want to do some type of nested loops, if - else if statements in a while loop, or while loops in an if statement, or whatever.
Re: while loop not behaving properly.
What you want to do is use a while loop to keep looping until the guess is correct.
That is what you had commented out.
Then inside that while loop, use 2 if statements, to check if it is to high or to low and print out the apporiate hint.
here is some pseduocode
Code:
get guess from user
while( guess is not right answer)
{
if guess is to high
tell user guess is to high
else //there guess was not right, and not to high, it must be low
tell user guess is to low
get guess from user
}
tell user they got it :)