Here's the deal, I'm trying to create a guess my number game where the computer tries to guess the Number that the user inputs. But I want to make it so that the user only needs to input his/her number once. Now here's the problem: If I put it in the do while loop, it keeps printing it out on the screen. (Yes I know that it should do that) But if I put it OUTSIDE the do while loop; I input a number and it creates an infinite loop. God knows why.
Here's the code:
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int main()
{
srand(static_cast<unsignedint>(time(0)));
int UserNum;
int tries = 0;
int CompGuess;
cout << "\t Welcome to Guess My Number\n";
cout << "\n\nIn this version of the game the Computer tries to guess YOUR number.";
do
{
cout << "\n\nPlease Enter the Number You Want Me to Guess: ";
cin >> UserNum;
++tries;
if(CompGuess > UserNum)
{
cout << "Too High!";
}
elseif(CompGuess < UserNum)
{
cout << "Too low!";
}
else
{
cout << "That's it, you got it in " << tries << " guesses.";
}
}while(CompGuess !=UserNum);
}
I'm sure that there is a million more problems with this code, and if there is, could you kindly point them out for me to correct.
Apologies if they are any stupid mistakes in the code, I tend to make stupid mistakes.
FYI I'm a beginner if you didn't know.
#include <iostream>
#include <cstdlib>
#include <ctime>
int main()
{
std::srand(static_cast<unsignedint>(std::time(0)));
int UserNum;
int tries = 0;
int CompGuess;
std::cout << "\t Welcome to Guess My Number\n" << std::endl;
std::cout << "In this version of the game the Computer tries to guess YOUR number." << std::endl;
std::cout << "Please Enter the Number You Want Me to Guess: " << std::endl;
std::cin >> UserNum;
do
{
++tries;
CompGuess = std::rand() % 201; //0 to 200, including 200.
if (CompGuess > UserNum)
{
std::cout << "Too High![" << CompGuess << "]" << std::endl;
}
elseif (CompGuess < UserNum)
{
std::cout << "Too low![" << CompGuess << "]" << std::endl;
}
else
{
std::cout << "That's it, you got it in " << tries << " guesses.[" << CompGuess << "]" << std::endl;
}
}while(CompGuess != UserNum);
}
now you may want to make it smarter, and do checks to make sure the user doesn't enter under 0 or more than 200
Yeah, I made the same thing a few weeks after I started, but I did it the other way around, the user had to guess the number, I think it makes it more fun since its the user that plays, whereas with yours they enter a number for the whole of the 'game'