#include <iostream>
#include <stdlib.h>
using namespace std;
void Guess(int Answer,int guess)
{
if(guess!=Answer)
{
if(guess<Answer && guess!=Answer)
{
cout<<"The number is higher, try again"<<endl;
cin>>guess;
}
else
if(guess>Answer && guess!=Answer)
{
cout<<"The number is lower, try again"<<endl;
cin>>guess;
}
}
}
int main()
{
int Answer,guess;
Answer=rand()%100;
cout<<Answer<<endl;
cout<<"Guess the number from 1 to 100"<<endl;
cin>>guess;
while(Answer!=guess)
{
Guess(Answer,guess);
if(Answer==guess)
break;
}
if(guess==Answer)
cout<<endl<<"Congratulations, you guessed the number, it was "<<Answer<<" !";
}
So, this is the code I made after I learned some basic functions, you can probably tell that it is supposed to generate a random number, and then you have to guess what the number is, but I have a few problems:
- it generates the same random number
- If you don't guess it right the first time, the code keeps going forever even if at some point u guess the number.
- If you guess a higher number, let's say 50 and the answer if 40, even if u then guess 30 it still says the number is lower when it's supposed to say higher.
What did I do wrong?