Function that calls on two others

I'm having problems with the below code. I set up the prototype, called on it in the main function, then set it up. (My teacher wants the user defined functions below the main function. When I compile my script, it says "expression listed treated as compound expression in initializer [-fpermissive]". Why is it telling me this? I'm new to user defined functions and this is only my second assignment with them. (having some weird problems with the first)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  (prototype)
  int mysteryNumber (int minNum, int maxNum);

  (calling in main func)
  int mysteryNumber (minNum, maxNum);

  (setup)
  int mysteryNumber (int minNum, int maxNum)
  {
	//declare variable
	int mystNum = 0;
	
	//set min & max
	int minNum (void);
	int maxNum (void);
	
	//pick random number
	srand(time(0))
	mystNum = rand() % minNum + maxNum;
	
	//return [new] mystery number
	return mystNum;
  }


Also, if anyone has a second to answer this, would I need "minNum" & "maxNum" in my prototype if I'm calling on the functions that provide those two int? (In other words since my mystNum function calls for both would I just put "void" in the ( )?)
Line 5: If that's supposed to be a function call, remove the int. The presence of the int makes that a function declaration and it will never be called.

Lines 14-15: What are these suppoed to be? minNum and maxNum are declared as arguments. These two lines appear to be function prototypes and are not needed.

Line 18: srand() should only ever be called ONCE in your program. It belongs at the beginning of main().
Last edited on
When I compile my script, it says "expression listed treated as compound expression in initializer [-fpermissive]". Why is it telling me this?


To answer your question: The code in the main function:

int mysteryNumber (minNum, maxNum);

is erroneous.
Instead of calling the function, the compiler sees this statement as a function declaration. This is because you put the int in front of the function call. The error results from the missing types in the parameter list: (minNum, maxNum). Leave the int out at the beginning and the compiler will treat the statement as a function call instead of a function declaration.
But remember that the function returns a value. It becomes useful if you store that value and do something with it after you obtained it. For example:

int randomNumber = mysteryNumber (minNum, maxNum);

Also, if anyone has a second to answer this, would I need "minNum" & "maxNum" in my prototype if I'm calling on the functions that provide those two int?


What I understand from what you write there, is that you ask if you have to put the parameters in the function declaration:

int mysteryNumber (int minNum, int maxNum)

And the answer is yes. The prototype is a blueprint to indicate to the compiler what types and how many values can be given to the function.


mystNum = rand() % minNum + maxNum;

Change this code if you would like a random number between minNum and maxNum into:

mystNum = rand() % (maxNum - minNum) + minNum;

(Keep in mind I made assumptions about the values of the variables used: positive and maxNum > minNum)
Abstraction Anon:
Thank you for your response. Lines 14-15 are suppose to be calling on the function for the player to set the min and max range for the random number. For Line 18, that goes at the beginning of the main function even if I want to be able to reset the random number later when the player says to play again?

Andres81:
Thank you for explaining everything, it helps me remember how to do it and makes more sense of what I'm doing. Thank you for answering the second question and fixing the random number code.
Ok, I made the changes and now I'm getting two errors for both my minNum and my maxNum. The two errors are:

invalid conversion from 'int (*)()' to 'int' [-fpermissive]

initializing argument 1 of 'int mysteryNumber(int, int)' [-fpermissive]
**for the maxNum it says "2" instead of "1"**

Any ideas??

**Update**
I had forgotten to take "int" out from in front of where I call upon my min and max functions and now it's telling me I can't use them as a function. OI VEY!!
Last edited on
I've made a simple program with your function and I recommend you give it a try.

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
#include <cstdlib>
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */
#include <iostream>

int mysteryNumber (int minNum, int maxNum);

/*
 * 
 */
int main(int argc, char** argv) {
    std::cout << mysteryNumber (0, 5) << std::endl;
    std::cout << mysteryNumber (5, 10) << std::endl;
    std::cout << mysteryNumber (1, 100) << std::endl;
    return 0;
}

int mysteryNumber (int minNum, int maxNum)
{
      //declare variable
      int mystNum = 0;

      //pick random number
      srand(time(0));
      mystNum = rand() % (maxNum - minNum) + minNum;

      //return [new] mystery number
      return mystNum;
}
Last edited on
@andres81 - As I pointed out earlier, the call to srand() should be made only once. By placing it at line 24, you are reinitializing the random number generator on each call to mysteryNumber() and therefore rand() will return the same number each time. The call to srand() should be placed in main() after line 11.
Last edited on
Abstraction:
Thank you for that explanation! Makes more sense now. I had made the change you suggested, but I didn't understand why I was doing it.

andres81:
Thank you for the simplified version. It looks much easier, but I don't understand what's going on in the std:: lines. For now I'm going to stick with my setup, that's how my teacher is expecting us to work.

Anyone:
I've made some modifications and everything works except one thing, my guessCheck function. I get two errors (one I'm assuming is talking about this function), but there's no line reference so I don't know where the error is; I asked my teacher and she said she gets the same error and doesn't know why either.
Here's my new code:

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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include<iostream>
#include<cstdlib>
#include<cctype>
#include<ctime>
#include<iomanip>

//declare user defined functions
void rules(void);
int minVal (void);
int maxVal (void);
int mysteryNumber (int, int);
bool checkGuess (int guess, int computerChoice);
int gameStats (int gamesPlayed, int numGuess);

using namespace std;

int main ()
{
	//declare variables
	int guess = 0;
	int gamesPlayed = 0;
	int numGuess = 0;
	int min, max;
	int computerChoice = 0;
	
	bool guessCorrect = false;
	
	char Play = ' ';
	
	srand(time(0));
	
	while (Play != 'N')
	{
		//display the rules
		rules ();
		
		//set the mystery number
		min = minVal ();
		max = maxVal ();
		computerChoice = mysteryNumber (min, max);
		
		//player guesses
		cout<<"\nWhat is your guess?"<<endl;
		cin>>guess;
		
		//check guess
		do
		{
		//check if guess is correct
		guessCorrect = checkGuess (guess, computerChoice);
		}
		
		while (guessCorrect == false);
		{
			//ask to play again
			cout<<"\nWould you like to play again?";
			cout<<"\nY/N"<<endl;
			cin>>Play;
			toupper(Play);
		}
		
		//check for incorrect response
		if (Play != 'Y' || Play != 'N')
		{
			cout<<"Please enter Y/N"<<endl;
		}
	}
}

//setup user definded functions
int mysteryNumber (int min, int max)
{
	//declare variable
	int mystNum = 0;
	
	//pick random number
	mystNum = rand() %(max - min) + min;
	
	//return [new] mystery number
	return mystNum;
}

int minVal (void)
{
	//declare variable
	int minNum = 0;
	
	//ask if user defined or auto
	cout<<"\nIf you would like to define the mystery number range, please enter the lowest number below.";
	cout<<"\nIf you leave it blank, it will default to 0"<<endl;
	cin>>minNum;
	
	//check to see if left blank
	if (minNum = ' ')
	{
		minNum = 0;
	}
	
	//return
	return minNum;
}

int maxVal (void)
{
	//declare variable
	int maxNum = 0;
	
	//ask if user defined or auto
	cout<<"\nIf you would like to define the mystery number range, please enter the highest number below.";
	cout<<"\nIf you leave it blank, it will default to 100"<<endl;
	cin>>maxNum;
	
	//check to see if left blank
	if (maxNum = ' ')
	{
		maxNum = 100;
	}
	
	//return 
	return maxNum;
}

int gameStats (int gamesPlayed, int numGuess)
{
	//declare variable
	int gameAve = 0;
	
	//update game average value
	gameAve = gamesPlayed / numGuess;
	
	//return game average value
	return gameAve;
}

void rules (void) 
{
	cout<<"\t\tWelcome to the Hi / Lo Game";
	cout<<"\nHere are the rules of the game: ";
	cout<<"\n1)\tYou guess the mystery number";
	cout<<"\n2)\tI tell you if your number is too high, too low, or correct";
	cout<<"\n3)\tThe default settings for the number range is 0-100.  You will have the option to set the range if you want";
	cout<<"\n3)\tThe game tracks your stats, once you guess correct you will see how many games played, the number of guesses, and your average";
	cout<<"\n\t\tReady to play?  Press Enter!";
}

bool guessCheck (int guess, int computerChoice)
{
	//set string
	if (guess > computerChoice)
	{
		cout<<"\nYour guess is too high, please try again";
		return false;
	}
			
	if (guess < computerChoice)
	{
		cout<<"\nYour guess is too low, please try again";
		return false;
	}
	
	if (guess = computerChoice)
	{
		cout<<"\nThat's correct!  YAY!";
		return true;
	}
			
	else
	{
		cout<<"\nThat is not a valid guess. Please enter your guess in number form.";
		return false;
	}
}
I don't get any errors, however, I do get a minor warning at line 30 indicating time() is expecting a parameter of type time_t.

Can you post the exact text of the errors?

I do see a problem at line 161. You're using the assignment operator (=), not the equality operator (==).

Lines 167-171 are unnecessary. If you fix line 161, these lines can't possibly be executed. In fact, the if statement at 161 is unnecessary. If not < and not >, then the only possibility is that the two numbers are equal. No reason to test.


I don't get anything for line 30, that's weird...

I have lines 167 - 171 in case the player types something other than a number.

The errosr I'm getting are:
[Linker error] E:/School Work/Spring 2015/1307 - C++/asgn 5/hilo.cpp:54: undefined reference to `checkGuess(int, int)'

&

E:\School Work\Spring 2015\1307 - C++\asgn 5\collect2.exe [Error] ld returned 1 exit status

Thank you for your help.
Lines 167-171: You're comparing two ints. That won't detect the player typing something other than a number.

You're getting a linker error because you have a declaration for checkGuess at line 12. Where's the implementation for checkGuess?
I'm assuming you meant that to be line 146 which is guessCheck, not checkGuess.

The second message is simply a result of the first message.
THANK YOU!!!

Ok, I'll just figure out how to tell it to check for anything other than a number, then give that msg.

-.- I can't believe it's that simple! I checked every instance of "checkGuess" and never once noticed they were flipped lol.

I figured it was a result of the first.

Thank you so much for your help!!
Topic archived. No new replies allowed.