I am working on a project for my computer science class, and I have to make a guessing game, but the computer is doing the guessing. The user picks a number from 1-100 as their number and the program must guess based on whether the user says it is higher or lower. I am having trouble re-limiting the max and min value after each guess. Ex. if my # is 66, I press 2 for higher(changing to h/l after), and it seems to limit it to 51-100, but after that the limiting isn't working very well. I think the problem might be in line 44 with lowering, I'm not sure.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
usingnamespace std;
int main()
{
int i;
int max = 100;
int min = 1;
int compguess = 50;
int numguess = 1;
float answer;
compguess<max;
compguess>min;
srand ( time(NULL) );
cout << "Press 0 after guessing a number to exit the program." << endl;
cout << endl << "Please enter a number, I will attempt to guess the number." << endl;
cin >> i;
do
{
cout << "Is your number " << compguess << "? " << endl;
cout << "If it is, press 1. If it is higher than 50, press 2. If it is lower than 50, press 3." << endl;
cin >> answer;
if (answer==1)
{
cout << "Your number was " << compguess << "! I guessed in " << numguess << " turns." << endl;
}
if (answer==2)
{
min = i;
compguess = rand() % (100-min) + min;
cout << "My guess is: " << compguess << endl;
numguess++;
}
elseif (answer==3)
{
max = i;
compguess = rand() % (50-min) + min;
cout << "My guess is: " << compguess << endl;
numguess++;
}
}
while (i!=0, answer!=1);
return 0;
}
What is wrong with the code? Everything else seems to work so far.
int guessNumber(int min,int max);
void main(){
int min=1,max=100,compguess,numguess=0,int answer;
do{
compguess = guessNumber(min,max);
cout << "My guess is: " << compguess << endl;
numguess++;
cout << "Is your number " << compguess << "?\n ";
cout << "If it is, press 1. If it is higher than "<<compguess<<", press 2. If it is lower than "<<compguess<<", press 3.\n";
cin >> answer;
switch(answer){
case 0:
case 1: break;
case 2:{
max=compguess-1;
compguess = rand() % (max-min+1) + min;
cout << "My guess is: " << compguess << endl;
numguess++;
break;
}
case 3:{
min=compguess+1;
break;
}
default:{
cout<<"I think another number...";
break;
}
}
if(min>max)break;
compguess = guessNumber(min,max);
cout << "My guess is: " << compguess << endl;
numguess++;
}while(answer!=0);
if(max>=min) cout << "Your number was " << compguess << "! I guessed in " << numguess << "turns.\n";
else cout<<"I HAVE guessed! the number must be "<<compguess<<", you lied me!";
}
int guessNumber(int min,int max){
if(min>=max)return min;
elsereturn (rand() %(max-min) +min;
}