Issues with Writing and Making Functions

For HW (intro to C++) I had to modify a program that I wrote previously, I will be including that code here. Seeing the output of it will make things much more clearer:
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
  //Program to compute and display the average and appropriate letter grade of   3 test scores 
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;

int main()
{
    char grade;
    double Test_1,Test_2,Test_3,Avg,ClassAvg1,ClassAvg2,ClassAvg3;
    int sumTest_1,sumTest_2,sumTest_3;
    int n;
    sumTest_1=sumTest_2=sumTest_3=0;
    const int totalSum=5.0;
    for(n=1;n<=5;n++) {
        do {
            cout<<"What are the three test scores for student #"<<n;
            cin>>Test_1>>Test_2>>Test_3;
            if(Test_1<1||Test_1>100||Test_2<1||Test_2>100||Test_3<1||Test_3>100)
            cout<<"You entered an invalid score - please try again"<<endl;
        }

        while(Test_1<1||Test_1>100||Test_2<1||Test_2>100||Test_3<1||Test_3>100);
        Avg=((Test_1+Test_2+Test_3)/3.0);

        if(Avg<65) {
            grade= 'F';
        }
        else if(Avg<70)
        {
            grade= 'D';
        }
        else if(Avg<80)
        {
            grade= 'C';
        }
        else if(Avg<90)
        {
            grade= 'B';
        }
        else
        {
            grade='A';
        }
        cout<<setprecision (0)<<fixed;
        cout<<"Your test average is "<<Avg<<" and your grade is "        <<grade<<endl;
        sumTest_1=sumTest_1+Test_1;
        sumTest_2=sumTest_2+Test_2;
        sumTest_3=sumTest_3+Test_3;
        ClassAvg1=sumTest_1/5.0;
        ClassAvg2=sumTest_2/5.0;
        ClassAvg3=sumTest_3/5.0;
    }
    cout<<"The class average for test #1 is: "<<ClassAvg1<<endl;
    cout<<"The class average for test #2 is: "<<ClassAvg2<<endl;
    cout<<"The class average for test #3 is: "<<ClassAvg3<<endl; 
} 


For hw, I have to modify the program above by including two functions, ComputeAvg which will be called to compute and return the student's average and LetterGrade, to compute and return the letter grade. This is so that instead of using one main program, I now use these 2 functions. I have written this code so far, but I'm not sure if I'm doing it right and it will be great if someone can explain things out for me since I'm still not very comfortable writing functions. I haven't finished writing the program by the way because I'm just confused:

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
//Program to compute and display the average and appropriate letter grade of        3 test scores
#include<iostream>
using namespace std;

void ComputeAvg(int Test_1, int Test_2, int Test_3)
{
    double Avg;
    double ComputeAvg(int Test_1,int Test_2,int Test_3);
    {
        Avg=ComputeAvg(Test_1,Test_2,Test_3);
        return(Test_1+Test_2+Test_3)/3.0;
    }
}

int main()
{
    int Test_1,Test_2,Test_3,n;
    double Avg;
    for(n=1;n<=5;n++)
    {
        do
        {
            cout<<"What are the three test scores for student #"<<n;

            cin>>Test_1>>Test_2>>Test_3;

            if(Test_1<1||Test_1>100||Test_2<1||Test_2>100||Test_3<1||Test_3>100)

            cout<<"You entered an invalid score - please try again"<<endl;
        }
        cout<<"Your test average is:"<<Avg<<endl;
        ComputeAvg(Test_1,Test_2,Test_3);

    }
}



#include<iostream>
using namespace std; 

void LetterGrade(int Test_1, int Test_2, int Test_3)
{
    double Avg;
    double LetterGrade(int Test_1,int Test_2,int Test_3);
    {
        Grade=LetterGrade(Test_1,Test_2,Test_3);

I may be wrong but it looks like your over thinking the function.

I don't see why you would need lines 8, 9, or 10.

I'm going to recommend this, you can see if it works.

1
2
3
4
5
6
void ComputeAvg(int Test_1, int Test_2, int Test_3)
{
        double Avg=0;
        Avg=(Test_1+Test_2+Test_3)/3;
        return(Avg);
}




IMO, if you declare a INT or Double, you should always set its value, normally to zero. It will make for much easier trouble shooting later then the programs get more complicated.
int Test_1=0;

Topic archived. No new replies allowed.