Compiling issue?

Hey guys, I'm wondering why my program won't compile. Thank you in advance! I'm getting errors that say:

assignment3-.cpp: In function ‘int main()’:
assignment3-.cpp:33: error: invalid conversion from ‘int (*)()’ to ‘int’
assignment3-.cpp:33: error: initializing argument 1 of ‘double testscore(int)’
assignment3-.cpp:34: error: invalid conversion from ‘int (*)()’ to ‘int’
assignment3-.cpp:34: error: initializing argument 1 of ‘double quizscore(int)’
assignment3-.cpp:35: error: invalid conversion from ‘int (*)()’ to ‘int’
assignment3-.cpp:35: error: initializing argument 1 of ‘double assignscore(int)’
assignment3-.cpp:36: error: ‘promptlab’ was not declared in this scope
assignment3-.cpp:37: error: cannot convert ‘double (*)(std::string)’ to ‘double’ for argument ‘1’ to ‘double finalweight(double)’




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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
   #include <iostream>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <string>
#include <cstring>

using namespace std;

int prompttest();
int promptquiz();
int promptassign();
int promptlabs();
string promptfinal();
int chk_int(string);
string chk_str(string);
double testscore(int);
double quizscore(int);
double assignscore(int);
double labscore(int);
double finalscore(string);
double testweight(double);
double quizweight(double);
double assignweight(double);
double labweight(double);
double finalweight(double);


int main(){

   double final, test, quiz, assign, lab, total;

   test = testweight(testscore(prompttest));
   quiz = quizweight(quizscore(promptquiz));
   assign = assignweight(assignscore(promptassign));
   lab = labweight(labscore(promptlab));
   final = finalweight(finalscore);

   total = test + quiz + assign + lab;

   cout << "Your grade in the class is: " << total;
}


int promtptest(){

   string test_str;
   int test;

   cout << "How many tests will be entered? ";
   cin >> test_str;

   test = chk_int(test_str);
   return test;
}

int promptquiz(){

   string quizzes_str;
   int quizzes;

   cout << "How many quizzes will be entered? ";
   cin >> quizzes_str;

   quizzes = chk_int(quizzes_str);
   return quizzes;
}

int promptassign(){

   string assign_str;
   int assign;

   cout << "How many assignments will be entered? ";
   cin >> assign_str;

   assign = chk_int(assign_str);
   return assign;
}

int promtplabs(){

   string labs_str;
   int labs;

   cout << "How many labs will be entered? ";
   cin >> labs_str;

   labs = chk_int(labs_str);
   return labs;
}

string promptfinal(){

   string finaltest;

   cout << "Is there a final test? (Y/N): ";
   cin >> finaltest;

   finaltest = chk_str(finaltest);
   return finaltest;
}

int chk_int(string num){

   int number;

   for(int i=0; i<num.length(); i++){
      if(!((num.at(i) >= '0') && (num.at(i) <= '9'))){
         cout << "Your number needs to be greater than 0!" << endl;
         cin >> num;
         i=-1;
      }
   }
   number = atoi(num.c_str());
   return number;
}

string chk_str(string finaltest){

   for(int i=0; i<finaltest.length(); i++){
      if(!((finaltest.length()==0) && ((finaltest.at(0) != 'n') || (finaltest.at(0) != 'y')))){
         cout << "Please enter a valid number ya dingus!" <<endl;
         cin >> finaltest;
         i=-1;
      }
   }
   return finaltest;
}

double testscore(int numof){

   string score_str;

   double score;
   double total = 0;
   double totalnum;

   for(int i=0; i<numof; i++){
      cout << "Test " << i << "score: ";
      cin >> score_str;
      score = chk_int(score_str);
      total = (total + score);
      totalnum = i;
   }

   return (total/(totalnum*100));
}

double quizscore(int numof){

   string score_str;
   double score;
   double total = 0;
   double totalnum;

   for(int i=0; i<numof; i++){
      cout << "Quiz " << i << "score: ";
      cin >> score_str;
      score = chk_int(score_str);
      total = (total + score);
      totalnum = i;
   }

   return (total/(totalnum*100));
}

double assignscore(int numof){

   string score_str;
   double score;
   double total = 0;
   double totalnum;

   for(int i=0; i<numof; i++){
      cout << "Assignment " << i << "score: ";
      cin >> score_str;
      score = chk_int(score_str);
      total = (total + score);
      totalnum = i;
   }

   return (total/(totalnum*100));
}

double labscore(int numof){

   string score_str;
   double score;
   double total = 0;
   double totalnum;

   for(int i=0; i<numof; i++){
      cout << "Lab " << i << "score: ";
      cin >> score_str;
      score = chk_int(score_str);
      total = (total + score);
      totalnum = i;
   }

   return (total/(totalnum*100));
}

double finalscore(string final){

   string score;
   double finalscore;

   if(final.at(0)=='y'){
      cout << "Enter your final test score: ";
      cin >> score;
      finalscore = chk_int(score);
      return finalscore;
   }

}

double testweight(double score){

   string weight_str;
   double testweight;
   double finalweight;

   cout << "Test weight: ";
   cin >> weight_str;
   testweight = chk_int(weight_str);
   finalweight = (testweight * score);

   return finalweight;
}

double quizweight(double score){

   string weight_str;
   double quizweight;
   double finalweight;

   cout << "Quiz weight: ";
   cin >> weight_str;
   quizweight = chk_int(weight_str);
   finalweight = (quizweight * score);

   return finalweight;
}

double assignweight(double score){

   string weight_str;
   double assignweight;
   double finalweight;

   cout << "Assignment weight: ";
   cin >> weight_str;
   assignweight = chk_int(weight_str);
   finalweight = (assignweight * score);

   return finalweight;
}

double labweight(double score){

   string weight_str;
   double labweight;
   double finalweight;

   cout << "Lab weight: ";
   cin >> weight_str;
   labweight = chk_int(weight_str);
   finalweight = (labweight * score);

   return finalweight;
}

double finalweight(double score){

   string weight_str;
   double weight;
   double finalweight;

   cout << "Final weight: ";
   cin >> weight_str;
   weight = chk_int(weight_str);
   finalweight = (weight * score);

   return finalweight;
}
You forgot the brackets for the most inner function call in lines 33 - 37. The compiler thinks the passed type is a function pointer while the actual parameter type is double.
Could you please elaborate? I'm fairly new to programming, this is my first program with functions.
You probably learned about function pointers unknowingly (Btw something you shouldn't be using as a beginner.)

Consider the following definitions:
1
2
int fun (int a);
int b;


The function fun returns an integer so the assignment
 
b = fun(10);
is valid

However fun on its own, without the () is also a variable, namely the pointer the int fun(int a). so if you do this:
 
b = fun;
the compiler thinks that you're trying to assign a function pointer to an integer. This is why you get "invalid conversion from ‘int (*)()’ to ‘int’"

Let's look at this line as an example:
test = testweight(testscore(prompttest));
We'll need to see what the function prototypes are as well:
1
2
3
int prompttest();
double testscore(int);
double testweight(double);


So looking back at what you are trying to do... You are trying to call several functions to get results, then pass the results as parameters to the next function, right?

testweight() looks good at first, as you are trying to pass it the result of testscore(), which is a double and that's what is needed.

testscore() looks good, as you are passing it the result of prompttest(), which is an int and that's what it requires.

...Hey, wait a second. Look carefully at that; you aren't calling prompttest() at all! You're just passing the name of the function - the prompttest function itself - to to testscore(), which is not what it wants (an integer, not a function). You'll need to call prompttest() so you can return its result to testscore().

Hope this helps you see the reasoning I used to figure out the problem.
A-ha! so from what you guys are saying is that I'm passing a function that doesn't carry a specific value to another function that can't compute without an actual value? That makes a lot more sense, if that's what I'm understanding. Is there an easy way to fix this? I feel like I would need to re-write my functions. If the function prompttest is returning test, and then in the main test = testweight(testscore(prompttest)) I don't know how you could return a value?
Last edited on
Yes, the functions want an integer, but you are feeding them with a function.
They don't care that this function returns an integer: It is a function.
Just calling the function will solve the problem, since it returns an int.
Note: we are talking about prompt*.
Just put a () in front of them.
Topic archived. No new replies allowed.