help:passing a function from array of struct

I'm working on a beginner practice problem, where data is input from file into a struct. basically the input file has a list of grades of at most 15 students, and I need to calculate & print their grade averages & letter grade.
Attached is my code. (I put comment "error line" after the line that's giving me trouble. basically I was trying to pass all the homework/quiz grades through the function HQGRADE, which will calculate the average homework/quiz grade. but my input "HQGRADE(The_Class[n].hqgrades);" just won't work, so I'm stuck at this step. Any one can show me how to pass a function correctly? Thanks a lot!)

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

#include <fstream>
#include <string>

using namespace std;

struct A_Student
{
       string name;
       int midterm;
       int final;
       int hqgrades; // homework & quiz grades
       float hqavg; // homework & quiz average
       float avg; // course average
       string letter_grade;      
} The_Class [15]; 

float HQGRADE (int arg[])
{ 
     float sum;
     float grade;
     for (int n=0; n<18; n++)
     {
         sum = sum + arg[n];
     }
     grade = sum *100 /180;
     return grade; 
}

int main ()
{  
  int x = 0;
  int n=0;
  int hqsum = 0;
  
  fstream textfile;
  textfile.open("sample input.txt");
  
  for (x=0;x<15;x++)
  {
  while(!textfile.eof())
   {    
        textfile>> The_Class[x].name;
        textfile>> The_Class[x].midterm;
        textfile>> The_Class[x].final;
        cout << The_Class[x].name << " ";
        cout << The_Class[x].midterm << " ";
        cout << The_Class[x].final << " " << endl;
        x++;
        while (n<18)
       {
          textfile >> The_Class[n].hqgrades;
          cout << The_Class[n].hqgrades<< " ";
          cout << HQGRADE(The_Class[n].hqgrades); //error line 
          
          n++;
       }
       
       cout<< endl;
       n=0;
                   
   }
}
  textfile.close();   
  cout <<endl;                  
  system("pause");
                        
} 
Last edited on
i wrote a test function separately to make sure nothing's wrong with the function. attached is the test function code. it runs and gives the correct output. so the problem is probably with the argument passed to the function.

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

#include <iostream>

using namespace std;

float hqgrade (int arg[])
{ 
     float sum;
     float grade;
     
     for (int n=0; n<18; n++)
     {
         sum = sum + arg[n];
     }
     grade = sum *100 /180;
     return grade; 
}
int main ()
{
  int hq[] = {10, 9, 8, 7, 8, 9, 10, 10, 9, 8, 7, 8, 9, 10, 10, 9, 9, 10};
  cout.precision(2);
  cout << fixed << hqgrade(hq);
  system ("pause");
  return 0;
}

Last edited on
Use [code][ /code] tags. Put your code in between. This enables syntax highlighting etc.
Last edited on
oh and the sample input file looks something like this: (Name, midterm, final, 18 hw/quiz grades)

Tom
90 100
8 9 10 10 10 10 10 10 10
10 2 1 7 10 10
1 2 3
Mary
90 90
1 2 3 4 5 6 7 8 9 10 10 10
10 10 10 10 10 10
Linda
100 100
10 10 10 10 10 10 10 10 10 10 10 10
10 10 10 10 10 10
Lucille
50 90
10 10 2 7 10 1 1 10 10 10 10 10 0 0 0 0 0 0
Ted
47 85
7 8 9 8 10 10 10 9 8 9 6 10 7 10 10 10 10 10
updated. code in highlights now. someone please help? thanks!!
 
 while (n<18)


You let n go from 0-17, but your array only has elements from 0-14. For future reference, also post exactly what kind of error you get (compiler error, linker error, runtime error? And the exact error message (if any) too).
when i comment out the error line, the program runs fine and
cout << The_Class[n].hqgrades;
prints out 18 hw/quiz grades so it is 18 elements.
i just need to pass these 18 hw/quiz grades through the function HQGRADE, which calculates the average of those 18 grades. what input argument should i use?

the error message just says:
initializing argument 1 of float HQGRADE(int*)'

doesn't exactly tells me what error it is.
Last edited on
Topic archived. No new replies allowed.