Function Calling

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.

[code]
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>

using namespace std;

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: high, low are uninitialized variables.

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.
[/code]



Last edited on
Hello smitty007,

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)".

Hope that helps,

Andy
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?
Hello smitty007,

You could use rand() % high or rand() % high + 1.

If I remember correctly the "+ 1" keeps "rand" from returning zero otherwise the first example will return a zero.

This may be helpful http://www.cplusplus.com/reference/cstdlib/rand/?kw=rand

Hope that helps,

Andy
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.

#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>

using namespace std;

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;
}
Hello smitty007,

Here is your program with comments about what to do or what I did different:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <string>
#include <stdlib.h>  // <--- Rarely need in a C++ program.
#include <time.h>    // <--- Should be <ctime>.

using namespace 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)
{
	return false;
}


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.

Hope that helps,

Andy
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!





#include <iostream>
#include <string>
#include <stdlib.h>
#include <ctime>

using namespace std;

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;
}
closed account (48T7M4Gy)
Here are 2 changes you need to make:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//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;
}
That makes sense now. I had assumed that the return 0 I was looking at was a part of the end of the code instead of a random return of 0. Thank you!
In the bool section, will I still need to add a cout message that states whether the number was too high or low?
closed account (48T7M4Gy)
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <string>
#include <stdlib.h>
#include <ctime>

using namespace 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";
    else if(guess > numberToGuess)
        cout << "High\n";
    else
        cout << "Equals\n";
    
    return guess == numberToGuess;
}
Thank you for your help.
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?
closed account (48T7M4Gy)
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.
Thank you. I'll go ahead and mess around with it some more. Thanks for all of the advice!

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!



#include <iostream>
#include <string>
#include <stdlib.h>
#include <ctime>

using namespace std;

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

if (guess < numberToGuess)
{
cout << "Too low\n" << endl;
}

else if (guess > numberToGuess)
{
cout << "Too high\n"<< endl;
}
return guess == numberToGuess;
}

void closing(int games)
{
cout << "You played " << games << " games\n" << endl;
cout << "Thanks for playing! Goodbye!" << endl;
}




Topic archived. No new replies allowed.