So I'm a little new to c++ and have had this problem with programs I try to loop with. When I run it and select the option to try again the output( 0 is too small try again) repeats infidently.Any clues what the problem might be? BTW I'm programing on my transformer tablet with g++ and a program called c4droid
# include <iostream>
# include <cstdlib>
using std::cout;
using std::cin;
using std::endl;
int main()
{
char again = 'Y';
do
{
srand(time(NULL));
int random = rand() % 100 + 1;
cout << " Guess the number I'm thinking of , 1-100" << endl;
int number, guess;
cin >> number;
while (number < random || number > random)
{
if (number < random)
{
cout << number << " is too small\nTry again" << endl;
cin >> number;
guess++;
}
if (number > random)
{
cout << number << " is too big\nTry again" << endl;
cin >> number;
guess++;
}
}
if (number == random)
{
cout << "You got it, it only took " << guess <<
" guesses.\nWant to pay again?" << endl;
cin >> again;
}
}
while (again == 'Y' || again == 'y');
cout << "Goodbye" << endl;
return 0;
}
It compiles and runs fine, the only thing i changed was that i initialized the variables number and guess. I would also recommend that you only seed rand once, before the do...while loop.
One thing I would do is move the seed to srand to just before the do loop. You only need to see once, and the way you have it you seed it every time you start the do loop, which will be before each game. Other than that, nothing jumps out at me really. Other thing is maybe on the 2nd if inside the while loop change it to else if.
Oh, and you haven't included <time.h>.
And lastly, you declared int guess but haven't initialized or assigned it a value. So any time you output guess it's an undefined value.
With adding <time.h> and initializing guess it works fine for me, and I moved the srand to right after line 10.
There are other things you might want to change, though.
For instance, the first time I guessed the number in about 8 guesses, but the program told me
You got it, it only took 245523422 guesses. Want to pay again?
(pay = play).
That´s because when you create variable int guess, you did not initializate it (give it an initial value = 0) so the portion of memory reserved for it had something and the program took it for the initial value. Understand?
When you say number < random || number > random you are saying "all the cases where random is different from number" (either it´s lower or it´s higher, so it´s always different). You may want to express it as random != number. It´s the same, but makes the code more easy to understand.
Thank you for the advice I implemented it into my code but the problem still persists. I don't think it's my tablet because the same thing happened on my tower which runs code blocks. So any advice?
Really don´t know. It works in my machine...
I found a little problem: guess should initialize as 1. Say you guess the number in the first attempt, guess never gets to increment.
About the other problem, sorry, i can´t help you...
Thank you so much it worked. Sorry if I'm being a little impractical here but how could i modify it so yes works? I've read a little about get. Char but i don't really understand the concept so sorry to ask but do you have the time to explain how that works?
# include <iostream>
# include <cstdlib>
# include <ctime>
usingnamespace std;
int main()
{
string again = "y";
do
{
srand(time(NULL));
int random = rand() % 100 + 1;
cout << " Guess the number I'm thinking of , 1-100" << endl;
int number, guess = 1;
cin >> number;
while (number != random)
{
if (number < random)
{
cout << number << " is too small\nTry again" << endl;
cin >> number;
guess++;
}
if (number > random)
{
cout << number << " is too big\nTry again" << endl;
cin >> number;
guess++;
}
}
if (number == random)
{
cout << "You got it, it only took " << guess <<
" guesses.\nWant to pay again?" << endl;
cin >> again;
again.resize(1);
}
}
while (again == "y" || again == "Y");
cout << "Goodbye" << endl;
return 0;
}
Notice how I´ve changed the declaration of namespace at the beggining, I´ve changed again´s type to string, and the trick is in line 37.
Now you can type "y", "yes", "yabadabadoo", and it will resize string to its first character only.
I don´t know really. I didn´t understand that part, yet. All I know is using namespace std; saves me all the std::cout, std::cin kind of thing. And when I first compiled after changing char to string compiler told me that string had not been declared... obviously it took it as a variable name, not type or class, so I went back to what I´ve been doing so far and always worked.
I wasn´t trying to correct anything, just wanted you to note it.
Glad to be helpful! Greetings from Argentina!