I can't figure out this code in chapter 2 beginning c++. I thought that it shouldn't be as different as the guess my number is they just switch rolls, but I can't get it.I read others post with the same game but still I don't understand.
The code I did gives me an infinite loop with is "Nooo too high"
I think that I have to update the compuess, but I don't know.
Please somebody help. Thank you very much in advance!
int main()
{
srand(static_cast<unsigned int>(time(0))); //seed random number generator
int number;
int compguess = (rand() % 100) + 1; // random number between 1 and 100 ;
int tries = 0;
cout << "\tWelcome to Guess Your Number\n\n";
cout << "What number do I have to guess between 1 and 100?: ";
cin >> number;
do
{
cout <<"\n Is this number: "<< compguess <<"\n";
++tries;
if (compguess > number)
{
cout << " Nooo too high!\n\n";
}
else if (compguess < number)
{
cout << "Nooo too low!\n\n";
}
else
{
cout << "\nThat's it! You got it " << compguess << " \n";
}
Yes you need to update the compguess - at the moment the computer makes one 'guess' at the start of the code but then doesn't update its guess from that point onwards.
How about adding some code to your too high / too low statements to make another guess
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int main()
{
int number;
int compguess = (rand() % 100) + 1; // random number between 1 and 100 ;
int tries = 0;
cout << "\tWelcome to Guess Your Number\n\n";
cout << "What number do I have to guess between 1 and 100?: ";
cin >> number;
do
{
srand(static_cast<unsignedint>(time(0)));
int compguess = (rand() % 100) + 1;
cout <<"\n Is this the number: "<< compguess <<"\n";
++tries;
cin.get();
if (compguess > number)
{
cout << " Nooo too high!\n\n";
cin.get();
}
elseif (compguess < number)
{
cout << "Nooo too low!\n\n";
cin.get();
}
else
{
cout << "\nThat's it! You got it " << compguess << " \n";
cin.get();
}
} while (compguess != number);
return 0;
}
good! Thank you!! compguess is updated! but still I have the problem with the loop, it keep saying "Nooo too high, it never goes to too low so, I am going to try by adding the statements with high and low..let see what happens
Thank you!, I can't get it..I am stuck! because at one moment the comp got the number, but it didn't understood the number guesses before was too high or too low..i keep working on it
You know some code that you are using like rand_1toN100(int x) or NULL Idon't know yet.....
I add the cin.get() and it updates, but now the problem is that the computer keep guessing with knowing that the number guesses before was to high or too low...
I think that I need to update that part...'
Okay, so what you want is for the program to guess the number and once it's guessed a number and it's too high to not guess a number that's higher than that and vice versa?
You needed a lowest and highest variable to store the lowest and highest guess. Then you need to tell the rand function to only guess numbers within this range.
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int main()
{
int number;
int high = 100; // lowest number to guess
int low = 1; // highest number to guess
int compguess;
int tries = 0;
cout << "\t~ Welcome to Guess Your Number ~\n\n";
cout << "What number do I have to guess between "
<< low << " and " << high << ": ";
cin >> number;
cin.get();
do
{
srand(time(0));
compguess = (rand() % (high - low - 1)) + low + 1;
/* The line below is an algorithm based solely on
* mathmatics. To use it simply uncomment its line.*/
//compguess = low + ((high - low) / 2);
cout <<"\n Is this the number: "<< compguess << "?\n";
++tries;
if (compguess > number)
{
cout << "Nooo too high!\n\n";
high = compguess;
}
elseif (compguess < number)
{
cout << "Nooo too low!\n\n";
low = compguess;
}
cin.get();
} while (compguess != number);
cout << "\nThat's it! It took you "<< tries << " tries! \n";
return 0;
}
Thank you so much, carebearboy!..I was all day yesterday trying to figure out! and actually with the way you did it you don't need break; to stop the loop!
I'm sorry to interrupt again but I had to make an edit. The program as it was would not guess 1 or 100 (or the highest or lowest number in any case) which leads to a flawed guessing system. I edited lines 26, 39, and 44 to correct this. It now works perfectly.
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int main()
{
int number;
int high = 100;
int low = 1;
int compguess;
int tries = 0;
cout << "\t~ Welcome to Guess Your Number ~\n\n";
cout << "What number do I have to guess between "
<< low << " and " << high << ": ";
cin >> number;
cin.get();
do
{
srand(time(0));
compguess = (rand() % ((high - low) + 1)) + low;
/* The line below is an algorithm based solely on
* mathmatics. To use it simply uncomment its line.*/
//compguess = low + ((high - low)/ 2);
cout <<"\n Is this the number: "<< compguess << "?\n";
++tries;
if (compguess > number)
{
cout << "Nooo too high!\n\n";
high = compguess - 1;
}
elseif (compguess < number)
{
cout << "Nooo too low!\n\n";
low = compguess + 1;
}
cin.get();
} while (compguess != number);
cout << "\nThat's it! It took you "<< tries << " tries! \n";
return 0;
}