So I'm supposed to make a program where you think of a number and the computer tries to guess it. However, it seems to be getting stuck in an infinite loop and I can't figure out why. Here's my code, any tips?
#include<iostream>
using namespace std;
int main()
{
int NumGuesses,min,max,my_guess,response;
NumGuesses=1;
my_guess=50;
cout<< "Think of a number between 1 and 100. I will attempt to guess your number.\n";
cout<< "Press 1 if the my guess is too high, 2 if it's too low, and 3 if it's right.\n";
cout<< "Is your number " <<my_guess<< " ?\n";
cin>> response;
do
{
max = my_guess - 1;
min = 2;
my_guess = ((max - min)/2 ) + min;
NumGuesses ++;
cout<< "Is your number " <<my_guess<< " ?\n";
}
while(response==1);
do
{
max=99;
min= my_guess+1;
my_guess = (max + min)/2;
NumGuesses ++;
cout<< "Is your number " <<my_guess<< " ?\n";
}
while(response==2);
do
{
cout<< "I got your number in " <<NumGuesses<< " amount of guesses.\n";
}
while(response==3);
I rewrote your code to flow a little bit better. Also, I didn't do ALL the work, but enough to hopefully let you fill in the blanks and complete your assignment ;) As long as your user eventually enters 3, this will keep you from an infinite loop scenario.
#include <iostream>
usingnamespace std;
int main()
{
int NumGuesses, min, max, my_guess, response;
cout<< "Think of a number between 1 and 100. I will attempt to guess your number.\n";
cout<< "Press 1 if the my guess is too high, 2 if it's too low, and 3 if it's right.\n";
my_guess=50;
NumGuesses = 0;
do {
cout << "\nIs your number " << my_guess << "?";
++NumGuesses;
cin >> response;
if (cin.fail()) return 1;
if (response == 1)
//I guessed too high
{
//DECREASE MY GUESS
}
elseif (response == 2)
//I guessed too low
{
//INCREASE MY GUESS
}
elseif (response == 3)
//I got it right!
{ break; }
else
//Entered something other than 1,2, or 3
{
cout << "\nSorry, I didn't understand you";
//This guess didn't count cause we are "guessing"
//the same thing when the loop reiterates.
--NumGuesses;
}
} while (response != 3);
cout << "I got your number in " << NumGuesses << "!\n";
system("pause");
return 0;
}
Oh the irony of life... so I was doing a few of the beginner tutorials on this website just to test my skills and I found basically the same problem you did here. So I did the whole program. Since you're complete, I'd love to see your solution to do a little compare and contrast.
#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
int compGuess = 50;
int newGuess = 0;
int lastGuess = 100;
cout << "Think of a number between 1 and 100. I will try to guess you number.\n";
cout << "Type 1 if I'm too high, 2 if I'm too low, 3 if I guessed correctly\n\n";
system("pause");
int playerResponse = 0;
do {
int change;
cout << "Is your number " << compGuess << "?";
cin >> playerResponse;
change = (abs(lastGuess-compGuess)/2);
if (change == 0) change = 1;
switch (playerResponse)
{
case 1: newGuess = compGuess - change; break;
case 2: newGuess = compGuess + change; break;
case 3: cout << "YEAH, I GOT IT!\n"; break;
default:
cout << "Sorry, I didn't understand your response!?\n";
newGuess = compGuess;
}
lastGuess = compGuess;
compGuess = newGuess;
} while (playerResponse != 3);
system("pause");
return 0;
}
"Hello World" at this point is more traditional. Every language I've ever learned or attempted to learn started with this. I think it's more used to just verify the compiler and IDE are setup and working correctly. In some cases, might also introduce the future programmer to key or generic libraries (like iostream).
The others are more common for beginners because they are not very complicated, yet touch on the basics of syntax and logic.