"if" statement not showing cout

I'm supposed to write a program that will give me the average scores for a practicum, and if the average is less than 67 then it should input the message: "You can retake practicums during the final." but it will input the other message as if it was a passing grade. I'm not sure how to fix it.
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
#include <iostream>
#include <iomanip>

using namespace std;
int main(){
   
    
    float score1,
          score2,
          score3,
          average,
          total;

    cout << "Enter practicum 1 score:\n";
    cin >> score1;
    cout << "Enter practicum 2 score:\n";
    cin >> score2;
    cout << "Enter practicum 3 score:\n";
    cin >> score3;  

    total = score1 + score2 + score3;
    average = total / 3;
    
    cout <<"Practicum average: " << fixed << setprecision(2) <<average <<endl;
    if(average > 67.00)
    cout << "You have a passing practicum average." << endl;
    else if(average = 67.00)
    cout << "You have a passing practicum average." << endl;
    else if(average <67)
    cout <<"You can retake practicums during the final."<< endl;
    return 0;
}
Last edited on
You are using = (assignment) when you should be using == (equality).

Note: To prevent this, you could make average be a const variable, by not defining it on line 11, but on line 22 instead.
const float average = total / 3;
Then your code wouldn't compile if you tried to incorrectly re-assign average.

You also have mismatched parentheses, so your code shouldn't've compiled at all.
Also, your else if on line 29 could just be an else, since you have exhausted all other possibilities.
Last edited on
Don't use equality for floating point numbers. Due to the way that these are stored, the comparison may or may not succeed. Normally for floating point equality you would test that the absolute difference between the number and the value to be tested was less than a small value.

1
2
if (abs(average - 67.0) < 0.000001)
    // equality code here 


However, in the above the display is the same for == or >. So just:

1
2
3
4
5
if(average >= 67.0)
    cout << "You have a passing practicum average." << endl;
else 
    cout <<"You can retake practicums during the final."<< endl;
}

Topic archived. No new replies allowed.