I've been given a template on a high/low game to program. I'm having trouble understanding how to write the syntax for a function and how to call a function to get the correct outputs. Any help or advice is greatly appreciated.
void introduction(int high, int low);
int getGuess();
bool testGuess(int guess, int numberToGuess);
int main(int argc, char *argv[])
{
int high;
int low;
int numberToGuess;
bool stillPlaying = true;
bool winner = false;
int guess;
while (stillPlaying)
{
// generate a random number between low and high value
numberToGuess = random () % (high - low + 1) + low;
//tell the user about the game
introduction(high, low);
while (!winner)
{
guess = getGuess();
winner = testGuess(guess, numberToGuess);
if (winner)
{
cout << "Congratulations! You have guessed the correct number! " << endl;
}
else
;
//output the number of guesses they've made so far
}
//ask the user if they want to play again, and if not, change the loop control condition
}
cout << "Thanks for playing!" << endl;
cin.ignore();
cin.get();
}
//Tells the user the rules of the game
void introduction(int high, int low)
{
cout << "Welcome to High/Low. I'm thinking of a number between " << high << "and " << low << "." << endl;
}
//Prompts for, inputs, and returns the next guess
int getGuess()
{
return 0;
}
// returns true if guess is correct
//if guess is not correct, outputs a high or low message and returns false
bool testGuess(int guess, int numberToGuess)
{
return false;
}
Line 28: random() is not a valid library function.
Line 31: You probably only want to explain the game to the user once, not every time through the loop.
Line 70: The comment says what to do.
Line 77: Ditto.
I'm having trouble understanding how to write the syntax for a function and how to call a function to get the correct outputs.
The function calls and the function skeletons are written for you. All you have to do is fill in the instructions in the functions.
PLEASE USE CODE TAGS CORRECTLY (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You have a starting code tag, but are missing the closing code tag.
I will add to what AbstractionAnon said. It is always a good practice to initialize all your variables. Here you are trying to use "low" and "high" with not value. think of the function call introduction(high, low) as introduction(-858993460, -858993460) which is what I usually get for an uninitialized variable. Best to set "low" to zero and "high" to the highest number in the range.
In line 28 did you mean to call the function "rand()" and if so "srand()" needs to be called before the while loop.
Line 31 calls introduction(high, low); function, but on line 66 of the "introduction" function the variables "high" and "low" would look better if their positions are reversed. Quickest fix for the problem unless this is what you want.
int "getGuess()" and the comment above it tells you what to do, so just do it. Post your changes in a new message and anything can be fixed.
The sae applies for "testGuess(int guess, int numberToGuess)".
Thank you. That helps out. If random () isn't a valid library function, how would you write out the code to randomly select a number between the high and low specifications?
I'm still absolutely lost with this. I think that I have the random number generated coded correctly. As this is all very new to me, I'm not sure where to enter the syntax to make this code compile and run correctly.
void introduction(int high, int low);
int getGuess();
bool testGuess(int guess, int numberToGuess);
int main(int argc, char *argv[])
{
int high = 1;
int low = 100;
int numberToGuess;
bool stillPlaying = true;
bool winner = false;
int guess;
while (stillPlaying)
{
// generate a random number between low and high value
numberToGuess = rand () % (high - low + 1) + low;
srand (time(NULL));
//tell the user about the game
introduction(high, low);
while (!winner)
{
guess = getGuess();
winner = testGuess(guess, numberToGuess);
if (winner)
{
cout << "Congratulations! You have guessed the correct number! " << endl;
}
else
;
//output the number of guesses they've made so far
}
//ask the user if they want to play again, and if not, change the loop control condition
}
cout << "Thanks for playing!" << endl;
cin.ignore();
cin.get();
}
//Tells the user the rules of the game
void introduction(int high, int low)
{
cout << "Welcome to High/Low. I'm thinking of a number between " << high << " and " << low << "." << endl;
cout << "You must try to guess the number. " << endl;
}
//Prompts for, inputs, and returns the next guess
int getGuess()
{
return 0;
}
// returns true if guess is correct
//if guess is not correct, outputs a high or low message and returns false
bool testGuess(int guess, int numberToGuess)
{
return false;
}
#include <iostream>
#include <string>
#include <stdlib.h> // <--- Rarely need in a C++ program.
#include <time.h> // <--- Should be <ctime>.
usingnamespace std; // <--- Not the best idea to use this. In the future it will get you in trouble.
void introduction(int high, int low); // <--- Order is not inportant as long as they are used crrectly. It does look better as low, high.
int getGuess();
bool testGuess(int guess, int numberToGuess);
int main(int argc, char *argv[]) // <--- There is no use for these parameters. Empty () is all you need.
{
int high = 1; // <--- Should be 100.
int low = 100; // <--- Should be 1.
int numberToGuess; // <--- Should be initialized. int numberToGuess{};
bool stillPlaying = true;
bool winner = false;
int guess; // <--- Should be initialized.
srand(size_t(time(NULL))); // <--- Need to add. "size_t" is another name for unsigned int.
while (stillPlaying)
{
// generate a random number between low and high value
numberToGuess = rand() % (high - low + 1) + low;
// what this actually looks like: numberToGuess = rand() % (1 - 100 + 1) + 100;
// (-99 ) + 100;
// Or numberToGuess = rand() % 1 Not what you want.
// Usinng your original code it should be numberToGuess = rand() % low; Reversing the definitions it should be "high".
//tell the user about the game
introduction(high, low); // <--- Passing the variables this way is OK, but makes more sence as low, high.
while (!winner)
{
guess = getGuess();
winner = testGuess(guess, numberToGuess);
if (winner)
{
cout << "Congratulations! You have guessed the correct number! " << endl;
}
else
;
//output the number of guesses they've made so far
}
//ask the user if they want to play again, and if not, change the loop control condition
}
cout << "Thanks for playing!" << endl;
cin.ignore();
cin.get();
}
//Tells the user the rules of the game
void introduction(int high, int low) // <--- This order is fine as long as you remember which variable is what. Makes more sence if the order is reversed.
{
cout << "Welcome to High/Low. I'm thinking of a number between " << high << " and " << low << "." << endl;
cout << "You must try to guess the number. " << endl;
}
//Prompts for, inputs, and returns the next guess
int getGuess()
{
// <--- the above comment tells you what is needed, so what code would you put here?
return 0;
}
// returns true if guess is correct
//if guess is not correct, outputs a high or low message and returns false
bool testGuess(int guess, int numberToGuess)
{
returnfalse;
}
Both while loops need some work and all three functions need work. The first one needs the least fixing.
Look over the comments and if there is anything you do not understand let me know. It is much easier to work on the program in small pieced than the whole thing at once.
Thank you Andy, that will help a lot. I was running into the problem of creating a never ending loop with the next guess function. I should be able to work out the rest from here. Thank you for your help.
Hey guys, I've been trying to work on the functions of this code and so far have been able to come away with nothing. I have worked out the first part of the code where I introduce the game and the user can input their initial guess. I believe that I have the code correct for creating a random number. My problem lies in calling the functions listed at the end of the code into the main code to have everything run as is supposed to. This is all very new to me and I'm not quite sure what exactly I am doing wrong. I know what I need to have, it's just a matter of creating the correct syntax. Thanks!
void introduction(int low, int high);
int getGuess();
bool testGuess(int guess, int numberToGuess);
int main(int argc, char *argv[])
{
int low = 1;
int high = 100;
int numberToGuess;
bool stillPlaying = true;
bool winner = false;
int guess;
while (stillPlaying)
{
// generate a random number between low and high value
numberToGuess = rand () % (high - low + 1) + low;
srand (time(NULL));
//tell the user about the game
introduction(high, low);
cin >> guess;
while (!winner)
{
guess = getGuess();
winner = testGuess(guess, numberToGuess);
if (winner)
{
cout << "Congratulations! You have guessed the correct number! " << endl;
}
else
;
//output the number of guesses they've made so far
}
//ask the user if they want to play again, and if not, change the loop control condition
}
cout << "Thanks for playing!" << endl;
cin.ignore();
cin.get();
}
//Tells the user the rules of the game
void introduction(int high, int low)
{
cout << "Welcome to High/Low. I'm thinking of a number between " << low << " and " << high << "." << endl;
cout << "You must try to guess the number. " << endl;
cout << "What is your first guess: " << endl;
}
//Prompts for, inputs, and returns the next guess
int getGuess()
{
cout << "What is your next guess: " << endl;
return 0;
}
// returns true if guess is correct
//if guess is not correct, outputs a high or low message and returns false
bool testGuess(int guess, int numberToGuess)
{
return false;
}
//Prompts for, inputs, and returns the next guess
int getGuess()
{
cout << "What is your next guess: " << endl;
int guess = 0;
cin >> guess;
return guess;
}
// returns true if guess is correct
//if guess is not correct, outputs a high or low message and returns false
bool testGuess(int guess, int numberToGuess)
{
return guess == numberToGuess;
}
I've taken it a little further and I'll leave it at that. You need to tidy it up carefully and manage the number of tries allowed and what happens to decide whether user continues.
#include <iostream>
#include <string>
#include <stdlib.h>
#include <ctime>
usingnamespace std;
void introduction(int low, int high);
int getGuess();
bool testGuess(int guess, int numberToGuess);
int main()
{
int low = 1;
int high = 100;
int numberToGuess;
bool stillPlaying = true;
int guess = 0;
introduction(high, low);
while (stillPlaying)
{
srand (time(NULL));
numberToGuess = rand () % (high - low + 1) + low;
while(!testGuess(guess, numberToGuess))
{
guess = getGuess();
}
cout << "Congratulations! You have guessed the correct number! " << endl;
}
cout << "Thanks for playing!" << endl;
}
//Tells the user the rules of the game
void introduction(int high, int low)
{
cout << "Welcome to High/Low. I'm thinking of a number between " << low << " and " << high << "." << endl;
cout << "You must try to guess the number. " << endl;
}
//Prompts for, inputs, and returns the next guess
int getGuess()
{
cout << "What is your next guess: " << endl;
int guess = 0;
cin >> guess;
return guess;
}
// returns true if guess is correct
//if guess is not correct, outputs a high or low message and returns false
bool testGuess(int guess, int numberToGuess)
{
if(guess < numberToGuess)
cout << "Low\n";
elseif(guess > numberToGuess)
cout << "High\n";
else
cout << "Equals\n";
return guess == numberToGuess;
}
Once the correct answer is guessed, I am still not able to ask if the user would like to play again. Am I not adjusting my loop accordingly? Or am I placing the cout message in the wrong portion of the loop?
Hints:
This is tricky :) pseuodocode helps.
For a start, you have enough while loops
An if, after congratulations message can be used to manage the value of stillPlaying
Of course that still leaves the number of tries. Do that separately.
I think that I finally have the code where I want it. I am coming up with one error when I try to compile and run it. Please take a look at this at let me know where my problem lies. Thanks!
void welcome(string name, int low, int high);
int getGuess(string name);
bool testGuess(int guess, int numberToGuess);
void closing(int games);
int main()
{
int low = 1;
int high = 100;
int numberToGuess;
bool stillPlaying = true;
string name;
int guess = 0;
int count, gameCount = 0;
cout << "Enter you name: " << endl;
cin >> name;
char repeat;
do {
welcome (name, high, low);
while (stillPlaying)
{
// generate a random number between low and high value
srand (time(NULL));
stillPlaying = true;
count = 0;
numberToGuess = rand () % (high - low + 1) + low;
while (true)
{
guess = getGuess(name);
if(testGuess(guess,numberToGuess))break;
else
{
count++;
cout << "Current number of guesses: " << count << endl;
}
}
cout << "\nCongratulations! You have guessed the correct number!" << endl;
stillPlaying = false;
}
gameCount++;
cout << "Do you want to try your luck again? (y/n):";
cin >> repeat;
stillPlaying = true;
{
while(repeat == 'y');
closing(gameCount);
}
//Tells the user the rules of the game
void welcome(string name,int high, int low);
{
cout << "Welcome to High/Low, " << name << ". I'm thinking of a number between" << low << "and" << high << "." << endl;
cout << "Can you guess the number?" << endl;
}
cin.ignore();
cin.get();
}
//Prompts for, inputs, and returns the next guess
while (stillPlaying);
{
int getGuess (name);
cout << "What is your next guess, " << name << endl;
int guess = 0;
cin >> guess;
return guess;
}
// returns true if guess is correct
//if guess is not correct, outputs a high or low message and returns false
bool testGuess(int guess, int numberToGuess);