I'm pretty new to coding so please excuse me if these are dumb questions. This code needs to prompt the user to enter the highest positive integer from a range of numbers starting at 0. I then have use a function that will generate a random number from that range which the user will then guess. I'm having trouble with creating that random number function(getmagicnumber) and then several problems in the do-while loops. The last issue I'm having is figuring out how to show the user how many tries they've used up before entering the correct guess. Please help!!!
//Prompt user to enter a guess//
std::cout<< "Guess the value of the magic number: \n";
std::cin>> guess;
//If value is too large prompt for another entry//
do
{
while (guess > getmagicnumber && > 0)
std::cout<< "Your guess is too high! Please try again\n";
std::cout<< "Guesses remaining: " <<triesleft<< "tries\n\n";
std::cin>> guess;
//If guess is negative prompt for another entry//
while (guess < 0)
std::cout<< "Your guess must be a positive integer. Please try again\n";
std::cout<< "Guesses remaining: " <<triesleft<< "tries\n\n";
std::cin>> guess;
//If guess is too low prompt for another entry//
while (guess < getmagicnumber && > 0)
std::cout<< "Your guess is too low! Please try again\n";
std::cout<< "Guesses remaining: " <<triesleft<< "tries\n\n";
//If guess is negative prompt for another entry//
while (guess < 0)
std::cout<< "Your guess must also be a positive integer. Please try again\n";
std::cout<< "Guesses remaining: " <<triesleft<< "tries\n\n";
std::cin>> guess;
//If no more tries are left, display magic number//
while (triesleft == maxtries)
std::cout<< "You have reached the max number of tries, : "<<getmagicnumber<<"is the magic number\n\n";
while (guess == getmagicnumber)
std::cout<< "Congratulations, you have guessed the magic number and used, : "<<numtries>>"number of tries\n\n";
}
return 0;
}
}
The function to return a random number should look like this:
1 2 3
int getMagicNumber(int max) {
return rand() % max;
}
Now, with that number, you should create a while loop and ask for a number and if it does not equal getMagicNumber, continue. An example:
1 2 3 4 5 6 7
int random = getMagicNumber(5);
int input = -1;
while(input != random){
std::cout << "What is the number I'm thinking of?: ";
std::cin >> input;
}
std::cout << "Correct!" << std::endl;
If you want to figure out how many times they failed, just create another integer, and increase it everytime the loop repeats.
Thanks for replying! I'm having trouble understanding your example though in relation to my code. Sorry, I'm a newbie. I tried using that format in my code but I'm receiving an error message saying that int is not a function or function pointer.
Okay, I thought for some reason they would need to have the same name since my professor wants the getmagicnumber function to be the random number generator.
It does generate a random number and it returns it. That's why you set the "rand" variable to what the function returns. The problem was that you declared an integer with the exact same name as the function that was made.
I've changed the name to getMagicnumber for both the function and integer, but I'm still receiving errors. Could you show me the correct way to create the function and declare the integer?
std::cout<< "This program will generate a magic number given a set of random positive integers\n";
do
{
//Prompt user for the top value in the range//
std::cout<< "Enter the highest number in a range of numbers from 0 to a positive integer: \n";
//Validate user input//
while (askrange > 0)
std::cout<< "Your highest number in the range is a positive integer: \n";
std::cin>> askrange;
while (askrange < 0)
std::cout<< "Invalid input, please enter a positive integer: \n";
std::cin>> askrange;
//Conditions for guessing the magic number//
int rand = getMagicnumber(5);
int guess = -1;
while(guess != rand){
std::cout << "Guess the value of the magic number: ";
std::cin >> guess;
}
//Prompt user to enter a guess//
std::cout<< "Guess the value of the magic number: \n";
std::cin>> guess;
//If value is too large prompt for another entry//
do
{
while (guess > getmagicnumber && > 0)
std::cout<< "Your guess is too high! Please try again\n";
std::cout<< "Guesses remaining: " <<triesleft<< "tries\n\n";
std::cin>> guess;
//If guess is negative prompt for another entry//
while (guess < 0)
std::cout<< "Your guess must be a positive integer. Please try again\n";
std::cout<< "Guesses remaining: " <<triesleft<< "tries\n\n";
std::cin>> guess;
//If guess is too low prompt for another entry//
while (guess < getmagicnumber && > 0)
std::cout<< "Your guess is too low! Please try again\n";
std::cout<< "Guesses remaining: " <<triesleft<< "tries\n\n";
//If guess is negative prompt for another entry//
while (guess < 0)
std::cout<< "Your guess must also be a positive integer. Please try again\n";
std::cout<< "Guesses remaining: " <<triesleft<< "tries\n\n";
std::cin>> guess;
//If no more tries are left, display magic number//
while (triesleft == maxtries)
std::cout<< "You have reached the max number of tries, : "<<getmagicnumber<<"is the magic number\n\n";