If else statement

Hello,

i been trying to get the outcome of the grades to show a letter grade but its not working properly and i tried lots of way and still stuck.

for example, if i were to enter 90 for 5 grades and it comes to be an A and if i were to enter 5 grades of 80 it still comes out to be A. no matter how much i look at the program it seems good to me but if u guys could spot the mistake please let me know.

thank you


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
]#include <iostream>
using namespace std;

float total(int[],int); 
int main()
{
  const int num = 5;
  int i, grade[num];
  int final;
  

  for (i = 0; i < num; i++)
  {
    cout << "Enter a grade: ";
    cin  >> grade[i];
}
    cout << "\nThe average of the grades is " << total(grade,num) << endl;
    {
    if (final >= 90)
      cout << "Your letter grade is A" << endl;
    else if (final >= 80 )
         cout << "Your letter grade is B" << endl;
    else if (final >= 70)
         cout << "Your letter grade is C" << endl;
    else if (final >= 60)
         cout << "Your letter grade is D" << endl;  
    else
        cout << "Your letter grade is F" << endl;
        }
  system("pause");
  return 0;
}

float total(int grade[], int i)
 {
      int total=0;
      int final;
      for(int j=0; j<i; j++)
     total = total + grade[j];
     final=total/i;


    return final;
}
In the total() function the returned float is final.

However in your code you don't put the returned value into anything, so you're just comparing against your uninitialized 'int final;' on line 9.

So you just need to do:
 
final = total(grade, sum);


You also need to change your final variable in the main function to a float, not an int.
Last edited on
oh i see. i got it thx for your help
Topic archived. No new replies allowed.