Errors on code using functions: Not sure how to fix them

Good day

I have written code to display matric students names, school, their mark average, whether they pass or fail, whether the get a distinction, and the code and symbol for their report. I am getting errors and am not sure why. Is anyone able to take a look and help me out?
Thanks

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
#include <iostream>
#include <string>
#include <cmath>

using namespace std;


void studentDetails ( string &name, string &surname, string &schoolName)
{
    cout << "Enter the students name: " << endl;
    cin >> name;
    cout << "Enter the students surname: " << endl;
    cin >> surname;
    cout << "Enter the students school: " << endl;
    cin >> schoolName;
}
 void checkMark(float m)
 {
     if (m >100 && m < 0)
     {
         cout << "ERROR - Invalid input";
    }
    else
    cout << "Thank you, mark is valid." << endl;
 }

 void getMarks(float & Engmark, float & Mathmark, float & LOmark, float & Hismark, float & CompLmark, float & Artmark)
 //These are the literal values as per the user input
{
    cout << "Enter a mark for English: " << endl;
    cin >> Engmark;
   checkMark(Engmark);
    cout << "Enter a mark for Mathematics: " << endl;
    cin >> Mathmark;
    checkMark(Mathmark);
    cout << "Enter a mark for Life Orientation: " << endl;
    cin >> LOmark;
    checkMark(LOmark);
    cout << "Enter a mark for History: " << endl;
    cin >> Hismark;
    checkMark(Hismark);
    cout << "Enter a mark for Computer Literacy: " << endl;
    cin >> CompLmark;
    checkMark(CompLmark);
    cout << "Enter a mark for Art: " << endl;
    cin >> Artmark;
    checkMark(Artmark);


}

void calcAverageYearMark(float a, float b, float c, float d, float e, float f)
//These are variables that get passed the true values from EngMark, Mathmark, etc. . .
{
    float calcAve = (a + b + c + d + e + f)/6;
    // calculate average of the input values
    cout << "Average Year Mark: " << calcAve;
}

void minMax(float &Engmark, float &Mathmark, float LOmark, float Hismark, float CompLmark, float Artmark)
{
    float largest = 0;
    float smallest = 100;

    if (Engmark < smallest)
    {
        smallest = Engmark;
    }
    else if (Engmark > largest)
    {
        largest = Engmark;
    }

    if (Mathmark < smallest)
    {
        smallest = Mathmark;
    }
    else if (Mathmark> largest)
    {
        largest = Mathmark;
    }

    if (LOmark < smallest)
    {
        smallest = LOmark;
    }
    else if (LOmark> largest)
    {
        largest = LOmark;
    }

    if (Hismark < smallest)
    {
        smallest = Hismark;
    }
    else if (Hismark> largest)
    {
        largest = Hismark;
    }

    if (CompLmark < smallest)
    {
        smallest = CompLmark;
    }
    else if (CompLmark > largest)
    {
        largest = CompLmark;
    }

    if (Artmark < smallest)
    {
        smallest = Artmark;
    }
    else if (Artmark > largest)
    {
        largest = Artmark;
    }

    cout << "The highest mark was " << largest << endl;
    cout << "The smallest mark was " << smallest << endl;

}

void passOrFail(float &Engmark, float &Mathmark, float &LOmark, float &Hismark, float &CompLmark, float &Artmark)
{
    bool passed = true;
    if (Engmark < 50)
    {
        passed = false;
    }
    else if(Engmark >= 50 && Mathmark >= 50 && LOmark >=50 && Hismark >=50 && Artmark >= 50)
    {
        passed = true;
    }
    else if(Engmark >= 50 && Mathmark >= 50 && LOmark >=50 && Artmark >=50 && CompLmark <= 50)
    {
        passed = true;
    }
    else // none of above criteria worked
    {
        passed = false;
    // statement of pass based on value of passed variable
    if (passed = true)
    {
        cout << "Outcome: Passed" << endl;
    }
    else (passed = false)
    {
        cout << "Outcome: Failed" << endl;
    }
    }
}

void awardDistinction(float &Engmark, float &Mathmark, float &LOmark, float &Hismark, float &CompLmark, float &Artmark)
{
    float disct = 75;

    if (Engmark >= disct || Mathmark >= disct || LOmark >= disct || Hismark >= disct || CompLmark >= disct || Artmark >= disct )
    {
        cout << "You have a subject distinction"
    }

    float calcAve = (EngMark + Mathmark + LOmark + Hismark + CompLmark + Artmark)/ 6;
    // calculate average of the input values
    if (calcAve >= 75)
    {
        cout << "Congrats, you have year distinction";
    }

}

void codeSymbol(float m)
{
    if(m <=100 && m >= 80)
    {
        cout << "A\t" << "7" << endl;
    }
    else if(m <= 79 && m >= 70)
    {
        cout << "B\t" << "6" << endl;
    }
    else if (m <= 69 && m >= 60)
    {
        cout << "C\t" << "5" << endl;
    }
    else if (m <= 59 && m >= 50)
    {
        cout << "D\t" << "4" << endl;
    }
    else if (m <= 49 && m >= 40)
    {
        cout << "E\t" << "3" << endl;
    }
    else if (m <= 39 && m >= 30)
    {
        cout << "F\t" << "2" << endl;
    }
    else if (m <= 29 && m >= 0)
    {
        cout << "FF\t" << "1" << endl;
    }
}

void displayReport(string name, string surname, string schoolName, float Engmark, float Mathmark, float LOmark, float Hismark, float CompLmark, float Artmark)
{
        cout << "****************************************************" << endl;
        cout << "\t\t\t STUDENT ACADEMIC RECORD" << endl;
        cout << "This program inputs the learner marks of matric level" << endl;
        cout << "subjects and prints the student final report." << endl;
        cout << "****************************************************************" << endl;
    cout << "NAME: " << name << " " << surname << "\tSCHOOL: " << schoolName << endl;
    cout << "Subject:\t\tMark:\tSymbol:\tCode:"<< endl;
    cout << "English\t" << Engmark << "%\t" << codeSymbol(Engmark) << endl;
    cout << "Maths\t" << Mathmark << "%\t" << codeSymbol(Mathmark) << endl;
    cout << "Life Orientation\t" << LOmark << "%\t" << codeSymbol(LOmark) << endl;
    cout << "History\t" << Hismark << "%\t" << codeSymbol(Hismark) << endl;
    cout << "Computer Literacy\t" << CompLmark << "%\t" << codeSymbol(CompLmark) << endl;
    cout << "Art\t" << Artmark << "%\t" << codeSymbol(Artmark) << endl;
}

int main()
{
    string name, surname, schoolName;
    float Engmark, Mathmark, LOmark, Hismark, CompLmark, Artmark;

    calcAverageYearMark(Engmark, Mathmark, LOmark, Hismark, CompLmark, Artmark)
    // Pass the real values to the function
    studentDetails(name,surname,schoolName)
    displayReport(name, surname, schoolName, Engmark, Mathmark, LOmark, Hismark, CompLmark, Artmark);
    passOrFail(Engmark, Mathmark, LOmark, Hismark, CompLmark, Artmark)

    awardDistinction(Engmark, Mathmark, LOmark, Hismark, CompLmark, Artmark)
    minMax(Engmark, Mathmark, LOmark, Hismark, CompLmark, Artmark)

    return 0; 
Last edited on
Why don't you post the errors you are getting for us?
 In function 'void passOrFail(float&, float&, float&, float&, float&, float&)':
143:22: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
148:5: error: expected ';' before '{' token
 In function 'void awardDistinction(float&, float&, float&, float&, float&, float&)':
161:5: error: expected ';' before '}' token
163:22: error: 'EngMark' was not declared in this scope
 In function 'void displayReport(std::string, std::string, std::string, float, float, float, float, float, float)':
213:45: error: no match for 'operator<<' (operand types are 'std::basic_ostream<char>' and 'void')
<template explosion removed> 



1
2
3
4
5
6
7
8
9
    // statement of pass based on value of passed variable
    if (passed = true)
    {
        cout << "Outcome: Passed" << endl;
    }
    else (passed = false)
    {
        cout << "Outcome: Failed" << endl;
    }

= is assignment, == tests for equality.
Do if (passed == true) { ... }
Further more, because you are dealing with simple booleans, you can simplify it to just:
1
2
3
4
5
6
7
8
if (passed)
{
    // ...
}
else
{
    // ...
}


Also, the logic itself is flawed because you set passed to true right before you check whether or not it's true...

void codeSymbol(float m)
This function returns void (i.e. nothing).
But you try using its return value here
cout << "English\t" << Engmark << "%\t" << codeSymbol(Engmark) << endl;

Printing something and returning something are not the same thing.
If you want codeSymbol to return a value, change the function.

e.g.
1
2
3
4
5
6
7
string codeSymbol(float m)
{
    if(m <=100 && m >= 80)
    {
        return "A\t7";
    }
    // ... 



cout << "You have a subject distinction"
You forget a semi-colon.

1
2
3
4
5
6
7
8
9
10
void awardDistinction(float &Engmark, float &Mathmark, float &LOmark, float &Hismark, float &CompLmark, float &Artmark)
{
    float disct = 75;

    if (Engmark >= disct || Mathmark >= disct || LOmark >= disct || Hismark >= disct || CompLmark >= disct || Artmark >= disct )
    {
        cout << "You have a subject distinction";
    }

    float calcAve = (EngMark + Mathmark + LOmark + Hismark + CompLmark + Artmark)/ 6;

Capitalization matters. EngMark != Engmark.
Thank you!
I have fixed those errors but i am still getting these errors.

1
2
3
4
5
6
7
8
In function 'void passOrFail(float&, float&, float&, float&, float&, float&)':|
|error: expected ';' before '{' token|

|In function 'int main()':|
229|error: expected ';' before 'studentDetails'|
|error: expected ';' before 'awardDistinction'|

||=== Build finished: 3 errors, 0 warnings ===|


My updated code for passOrFail.

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
void passOrFail(float &Engmark, float &Mathmark, float &LOmark, float &Hismark, float &CompLmark, float &Artmark)
{
    bool passed = true;

    if (Engmark < 50)
    {
        passed == false;
    }
    else if(Engmark >= 50 && Mathmark >= 50 && LOmark >=50 && Hismark >=50 && Artmark >= 50)
    {
        passed == true;
    }
    else if(Engmark >= 50 && Mathmark >= 50 && LOmark >=50 && Artmark >=50 && CompLmark <= 50)
    {
        passed == true;
    }
    else // none of above criteria worked
    {
        passed == false;
    // statement of pass based on value of passed variable
    if (passed == true)
    {
        cout << "Outcome: Passed" << endl;
    }
    else (passed == false)
    {
        cout << "Outcome: Failed" << endl;
    }
    }
}
You overcorrected yourself. Perhaps I didn't explain it enough.
= is for variable assignment.
a = b means that the value of b is copied into the value of a.
a == b is used to test for equality. a is compared to the value of b, and true or false is returned.
Lines 7, 11, 15, 19 -- you are comparing passed to true or false, and then throwing away the result (not using it). You are not assigning anything.
If you want to assign a value, use =.
If you want to test for equality, use ==.

Also, when you use if+else, the else statment has no condition (in parentheses) attached to it. It is simply
1
2
3
4
5
6
7
8
if
{
    statement1;
}
else // nothing here!!
{
    statement2;
}
"

Look at what I wrote again, my else statement has no condition attached to it. It is simply "else" with nothing else.

Also, notice your indentation. Two } (curly braces) right next to each other on same level of indentation should never happen -- lines 28, 29. Everything from lines 19 to 28 should be indented because they are within the else statement that starts on line 17.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
if (condition)
{
    statement1;
}
else
{
    statement2;

    if (other_condition)
    {
        statement3;
    }
    else if (some_other_condition)
    {
        statement4;
    }
    else
    {
        statement5;
    }
}

It is not wrong syntax to not do so, but it makes it much harder to quickly read the code.
Last edited on
I have fixed those errors


Actually, you fixed some and created new ones.

As @Ganado pointed out,
= is assignment, == tests for equality.
In your new function, lines 7, 11, 15 and 19, you are trying (as far as I can tell) to set passed to the value "true". That would be assignment. Only use == when you are trying to determine if 2 things are equal. Only use = if you are trying to assign a value to a variable.

Lines 21 - 28 only happen if you fall through the if/else if/else if/else construct. If you assign passed = true; in one of the earlier blocks, you will not print out the message you expect. What you probably want to do is move the closing curly brace from line 29 to just after line 19.

I don't see the first error you are encountering. The error should have come with a line number. Please tell us what line in your pasted code matches the line number where the error occurred.

The errors in main() are pretty obvious from the error messages. Please read the error messages and look at your code at the lines indicated in the error messages. It should be obvious where semicolons are missing.
Topic archived. No new replies allowed.