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.