Help With Struct for the first time

Ok i have been working on this for close to 9 hours or so combined and i could use some help.

I have some data that i am suppose to put into a struct then use a bubble sort to sort the data 1 of 2 different ways. I am having trouble with right now is trying to get the data into the struct i am fairly confident i can figure the rest of it out.

The data file that i have to read into the program is as follows

John Adams 5 6 7 9 3 4 8 8 6 10
Henry Smith 10 4 6 8 5 2 4 9 7 8


The out put will be
" Names" "Grades" "Avg
Adams, John 5 6 7 9 3 4 8 8 6 10 6.6
Smith, Henry 10 4 6 8 5 2 4 9 7 8 6.3

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

using namespace std;

const int NUM_STUDENTS = 10;
const int NUM_GRADES = 10;

struct StudentTytpe{
  string name;
  int grades [NUM_GRADES];
  float avg;
};

void getData(StudentTytpe Students[], ifstream&);
void openinputfile(ifstream&);
void openoutputfile(ofstream&);
void printData(StudentTytpe Students[], ofstream&);

int main(){
StudentTytpe Students[NUM_STUDENTS];
ifstream fin;
ofstream fout;

openinputfile(fin);
getData(Students, fin);
fin.close();

openoutputfile(fout);
printData(Students, fout);
fout.close();
return 0;}

void openinputfile(ifstream& fin){
string filename;

  cout << "Filename: ";
  cin >> filename;
  fin.open(filename.c_str());

while(!fin){
  cerr << "Invalid File Name" << endl;
  cout <<"Enter File Name: ";
  cin >> filename;
  fin.open(filename.c_str());}
}

void openoutputfile(ofstream& fout){
string filename;

  cout << "Enter Output File Name: ";
  cin >> filename;
  fout.open(filename.c_str());

while(!fout){
  cerr << "Invalid File Name" << endl;
  cout <<"Enter File Name: ";
  cin >> filename;
  fout.open(filename.c_str());}
}

//Right hear is where i am haveing the problem i think.

void getData(StudentTytpe Students[], ifstream& fin){
string fname, lname;
float total = 0;

for(int i = 0; i < NUM_STUDENTS; ++i){
  fin >> fname >> lname;
  Students[i].name = lname + "," + " " + fname;
  for(int j = 0; j < NUM_GRADES; ++j){
    //fin >> grades[i][j];   I replaced this code with
    //total += grades[i][j];
   fin >> Students[i].grades[i][j];
   total += Students[i].grades[i][j];
  }
  Students[i].avg = total / NUM_GRADES;
}
}
//void printData(StudentTytpe Students[], ofstream& fout){} 


now i get the error: invalid types 'int[int]' for array subscript


not sure how i can fix it.

I guess i am having the proble of getting a 2d array into a stuct

Last edited on
Excellent code.
The problem is quite simple.
On lines 74 and 75, you use the array "grades [i] [j]", however you never declared an array named grades xD

1
2
fin >> grades[i][j];   //Added this just a few min ago
    total += grades[i][j];


The simple way to fix this problem is to declare an array corresponding to your needs.

Whenever you see an error with scope in it, it usually refers to a variable being misspelled, not declared/initialized, or used incorrectly.

This should work.

Ps. There also seems to be an error on line 32 for me, an "Undefined Reference", but it googled it and it seems to be an error on my behalf. If it happens to you also, try this link, see if you understand. I would read it, but I gotta get some zzz.

http://stackoverflow.com/questions/6284720/undefined-reference-c
thanks, I think you saved the few hairs i had left on my head.
Topic archived. No new replies allowed.