I am reading book called "jumping into c++" in which it wants me to build a program that find the solution of a guessing game that picks a number between 1 and 100 randomly and let user guess what the number is and tells them whether their guess is high,low or just right. Basically it wants me to predict the next random number but i am unable to figure out a way to do so since i am a newbie.
This is the code of guessing game:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int x,y;
int rand_range(int low, int high)
{
return rand() % (high - low) + low;
}
int main()
{
int seed = time(NULL);
srand(seed);
int y = rand_range(1, 100);
cout << "Program has picked a no. between 1 to 100... You just make a
guess....\n";
cin >> x;
while(1)
{
if(x == y)
{
cout << "just right\n";return 0;
}
else if(x < y)
{
cout << "low\n";return 0;
}
else
{
cout << "high\n";return 0;
}
}
}
Aucun commentaire:
Enregistrer un commentaire