Random Number Guessing

Apr 8, 2015 at 3:37am
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!!!


#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()

{
srand((unsigned) time(NULL));

int askrange, getmagicnumber, guess, maxtries, triesleft, numtries;
getmagicnumber=rand();

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//

maxtries=5;
triesleft=maxtries-1;
numtries=maxtries-triesleft;


//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";

//If guess matches magic number, congratulate user//

while (guess == getmagicnumber)
std::cout<< "Congratulations, you have guessed the magic number and used, : "<<numtries>>"number of tries\n\n";
}
return 0;
}
}
Last edited on Apr 8, 2015 at 3:40am
Apr 8, 2015 at 3:50am
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.
Apr 8, 2015 at 4:16am
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.
Apr 8, 2015 at 4:23am
Could I see how you implemented the code pieces that I've given you?
Apr 8, 2015 at 4:29am
srand((unsigned) time(NULL));
int askrange, getmagicnumber, guess, maxtries, triesleft, numtries;
int getmagicnumber
(int max) { return rand() % max;
}

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;
}
Apr 8, 2015 at 4:32am
Your first problem is that you're creating the function with the exact name as the variable "getmagicnumber" above.

Also, if all this code is within the int main() function, you should move the getmagicnumber function outside of it.

Apr 8, 2015 at 4:41am
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.
Apr 8, 2015 at 4:47am
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.
Apr 8, 2015 at 5:05am
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?
Apr 8, 2015 at 5:08am
What are your errors?
Apr 8, 2015 at 5:15am
For the (int max) line it's saying that the function definition is not allowed here.

For the int rand= getMagicnumber(5); It's saying the called object type "int" is not a function or function pointer
Apr 8, 2015 at 5:35am
Could I see your code again?
Apr 8, 2015 at 6:07am
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()

{
srand((unsigned) time(NULL));
int askrange, getmagicnumber, guess, maxtries, triesleft, numtries, getMagicnumber;
int getMagicnumber
(int max) { return rand() % max;
}

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";

//If guess matches magic number, congratulate user//

while (guess == getmagicnumber)
std::cout<< "Congratulations, you have guessed the magic number and used, : "<<numtries>>"number of tries\n\n";
}

return 0;
}
}
Apr 8, 2015 at 3:30pm
You continue to define your function within int main();

I suggest you look at this tutorial about functions: http://www.cplusplus.com/doc/tutorial/functions/

The function should be defined outside of int main(){} like this:

1
2
3
4
5
6
7
int anewfunction(){

}

int main(){

}
Topic archived. No new replies allowed.