Function Call Error

I am having issues with calling from a void function on my cs assignment any ways to fix this issue would be greatly appreciated. All I get is contestant 2 is the winner when it is not.

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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include <iostream>				// listing all header files
#include <fstream>
#include <string>
#include <ctime>
#include <cmath>

using namespace std;

int flipcoin();
void OpenFile(ifstream& infile);
void getjudgescore(ifstream& infile, int& judgescore);
void getphonescore(ifstream& infile, int& phonescore);
void findwinner(int judgescore1, int totalphone1, int judgescore2, int totalphone2);


int main()
{
	string textfile;			// textfile name is stored here
	ifstream infile;

	int judgescore = 0,			// judge and phone scores labeled here
		totalphone = 0,
		judgescore1 = 0,
		totalphone1 = 0,
		judgescore2 = 0,
		totalphone2 = 0,
		contestant = 0;

	double temp;				// used for average judgescore



	int winner;
	int contestent = 1;
	unsigned int seed;
	seed = static_cast<unsigned>(time(NULL));

	srand(seed);
	winner = flipcoin();


	cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-" << endl;
	cout << "Welcome to Tallahassee Idol!  Where Stars are Born!!!" << endl;
	cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-" << endl;


	cout << endl;

	cout << "Enter the name of the file (no blanks!) -> ";
	cin >> textfile;
	cout << endl;
	infile.open(textfile.c_str());

	while (!infile)
	{
		cout << endl;
		cout << "The file name you gave is invalid.  Please try again." << endl;
		cout << "Enter the name of the file(no blanks!) -> ";
		cin >> textfile;
		cout << endl;
		infile.open(textfile.c_str());
	}



	cout << "Processing Judge Data for Contestant 1" << endl;
	cout << "== == == == == == == == == == == == == == == == == == =" << endl;
	cout << "Judge Scores are: ";

	getjudgescore(infile, judgescore1);
	cout << endl;

	cout << "Average Judge Scores are: " << judgescore1;
	cout << endl;
	cout << endl;


	cout << "Processing Phone Data for Contestant 1" << endl;
	cout << "== == == == == == == == == == == == == == == == == == =" << endl;
	getphonescore(infile, totalphone1);
	cout << "Total of Phone Calls for all Regions: " << totalphone1 << endl << endl;

	cout << endl;

	cout << "Processing Judge Data for Contestant 2" << endl;
	cout << "== == == == == == == == == == == == == == == == == == =" << endl;
	cout << "Judge Scores are: ";
	getjudgescore(infile, judgescore2);
	cout << endl;
	cout << "Average Judge Scores are: " << judgescore2 << endl;

	cout << endl;

	cout << "Processing Phone Data for Contestant 2" << endl;
	cout << "== == == == == == == == == == == == == == == == == == =" << endl;
	getphonescore(infile, totalphone2);
	cout << "Total of Phone Calls for all Regions: " << totalphone2 << endl << endl;

	cout << "Final Results Summary" << endl;
	cout << "--------------------" << endl;

	cout << "Judges winner is: ";
	findwinner (judgescore1, judgescore2, totalphone1, totalphone2);
	cout <<  endl;
	cout << "Phone Call Winner is: ";
	findwinner(judgescore1, judgescore2, totalphone1, totalphone2);
	cout <<  endl;

	cout << endl;

	cout << endl << endl << endl << endl << endl << endl;






	cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-" << endl;
	cout << "Thanks for visiting our show!!!" << endl;
	cout << "Execution Terminated Normally." << endl;
	cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-" << endl;
	cout << endl;






	return 0;

}


void getjudgescore(ifstream& infile, int& judgescore)
{

	int value,
		min,
		max;
	double average;

	infile >> value;
	cout << value << " ";
	judgescore += value;

	min = value;
	max = value;

	for (int i = 0; i < 4; i++)
	{

		infile >> value;
		cout << value << " ";
		judgescore += value;

		if (value > max)
			max = value;
		if (value < min)
			min = value;


	}

	judgescore = judgescore - min - max;

	average = (double)judgescore / 3.0;

	judgescore = ceil(average);



}

void getphonescore(ifstream& infile, int& phonescore)
{
	int value,
		i;
	for (int i = 0; i < 4; i++)
	{

		infile >> value;
		cout << "Phone Call for Region " << i + 1 << ": " << value << endl;
		phonescore += value;
	}


}

void findwinner(int judgescore1, int totalphone1, int judgescore2, int totalphone2)
{
	int contestant1 = 0,
		contestant2 = 0;

	if (judgescore1 > judgescore2)
	{
		cout << "Contestant 1" << endl;
		contestant1++;
		return;
	}
	else if (judgescore1 < judgescore2)
	{
		cout << "Contestant 2" << endl;
		contestant2++;
		return;
	}

	if (totalphone1 > totalphone2)
	{
		cout << "Contestant 1" << endl;
		contestant1++;
		return;
	}
	else if (totalphone1 < totalphone2)
	{
		cout << "Contestant2" << endl;
		contestant2++;
		return;
	}

	if (contestant1 = contestant2)
	{
		cout << "Contestants are tied!!!" << endl;
		cout << "We will now flip a coin to see who wins." << endl;
		int winner = flipcoin();	 // this function is called if the contestants tie 

		if (winner == 1)	 // if the coin rolls a 1, contestant 1 wins 
			cout << "Contestant 1 is the winner!" << endl << endl;
		if (winner == 2)	 // if the coin rolls a 2, contestant 2 wins 
			cout << "Contestant 2 is the winner!" << endl << endl;
	}
	else if (contestant1>contestant2) //compare the Advantage Points 
			cout << "Contestant 1 is the winner!";
	else if (contestant2 >contestant1)
		cout << "Contestant 2 is the winner!";

	return;
	}




int flipcoin()
{
	const int max = 2;
	double x = 1 + 1.0;
	int y;

	y = static_cast<int> (1 + rand() * max);
	return y;
}
You need to be more specific about what you think the problem is.

Also, why did you create a new thread? This one was fine:
http://www.cplusplus.com/forum/beginner/127355/
Last edited on
My call from void function is only responding with contestant 2 and I do not need to change the function as that as is how it was given to us by our teacher and told would work. I made a new thread because i am hoping to put this out to more people I did not know that was a bad thing to do.
I can see several problems with your code. However, none of them would lead to contestant #2 winning. Could you please be more specific?

Here's what I see:

On line 220, you are assigning the value of contestant2 to contestant1. The operation doesn't return false, and therefore we enter the control structure always. The flipcoin function doesn't work properly, and it will return all sorts of values larger than 2. Here as some values it gave me:

30013
62203
9933

And so on. Since your flipcoin() function doesn't work properly, it's unlikely that program flow will ever enter the control structures on lines 226 and 228.
We then exit the initial control structure, and because of the previous accidental assignment on line 220, neither of the control structures on lines 231 or 233 will execute.

You also have unused variables on lines 245 and 177.
Last edited on
How exactly would I fix the flipcoin() function, what do you mean by control structure have never heard of this from my professor or ta and my ta told me to keep the unused variables.
A control structure is anything that can alter the program's flow, such as:

if()

else if()

else

switch

Ternary operation (more appropriately named a conditional expression)

and so on.

Here's how I would do the flipcoin() function, assuming that you've seeded the pseudo-random number generator at the beginning of your program, which you have:

1
2
3
4
5
6
7
int flipcoin() {
	int min = 1;
	int max = 2;
	int value = (rand() % (max + 1 - min)) + min;

	return value;
}


Please be more specific with your problems. Nobody can help you if you don't tell us what's wrong.

1.) If you have any instructions given to you by your teacher, post them here.

2.) If you're receiving any compilation errors, copy and paste them here.

3.) If the program is not behaving as expected, tell us why, and how it should be behaving.

4.) It looks like this program accepts a file. What would such a file look like?

5.) Give us a short but concise explanation of what the requirements are, what the program should do and how it should do it.
Last edited on
Topic archived. No new replies allowed.