grade average calculator issues

This program only compiles if the number of grades has already been predetermined, but not if the number is a value declared by the user (which varies each time the program is run).

Instructions:
Write a program that will read in grades, the number of which is
also input by the user. The program will find the sum of those grades and
pass it, along with the number of grades, to a function which has a “pass
by reference” parameter that will contain the numeric average of those
grades as processed by the function. The main function will then determine
the letter grade of that average based on a 10-point scale.

Code:

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

using namespace std;


void getNum(int &);
void calculateaverage(double &);
char calculategrade(double);
int main()

{
    
    int value;
    double avg=0;
    getNum(value);
    calculateaverage(avg);
    calculategrade(avg);
    cout<<calculategrade(avg)<<endl;
    
    system("pause");
    
}

void getNum(int &userNum)

{
    
    cout<<"Enter number of grades:";
    
    cin>>userNum;
    
}

void calculateaverage(double &avg)

{
    int score;
    for(int i=0;i<userNum;i++)
        
    {
        cout<<"Enter numeric grade between 0-100:";
        cin>>score;
        avg+=score;
    }
    
    avg/=userNum;
    
    cout<<avg<<endl;
    
}
;char calculategrade(double myAvg)

{
    

    
    if(myAvg<60)
        
        return 'F';
    
    else
        
        if(myAvg >60 && myAvg<70)
            
            return 'D';
    
        else
            
            if (myAvg>70 && myAvg<80)
                
                return 'C';
    
            else
                
                if(myAvg>80 &&myAvg<90)
                    
                    return 'B';
    
                else
                    
                    return 'A';
    
}
Line 41, userNum is not declared there.i suggest you return it and use it there.So it will give you a compilation error there.Thats the problem.
I would suggest that you make you void calculateaverage(double &avg) accepts two arguments. one for the length also
Try this

#include<iostream>
#include<string>
#include<iomanip>
#include<cstdlib>

using namespace std;


int getNum(int &);
void calculateaverage(double &, int);
char calculategrade(double);
int main()

{
int n;
int value;
double avg=0;
getNum(value);
calculateaverage(avg, value);
calculategrade(avg);
cout<<calculategrade(avg)<<endl;

system("pause");

}

int getNum(int &userNum)

{

cout<<"Enter number of grades:";

cin>>userNum;


}

void calculateaverage(double &avg, int userNum)

{
int score;
for(int i=0;i<userNum;i++)

{
cout<<"Enter numeric grade between 0-100:";
cin>>score;
avg+=score;
}

avg/=userNum;

cout<<avg<<endl;

}
char calculategrade(double myAvg)

{



if(myAvg<60)

return 'F';

else

if(myAvg >60 && myAvg<70)

return 'D';

else

if (myAvg>70 && myAvg<80)

return 'C';

else

if(myAvg>80 &&myAvg<90)

return 'B';

else

return 'A';

}
Topic archived. No new replies allowed.