Taking data from text file and storing in array - running into problems

Hey everyone, so I am having a really annoying problem with my code.

The goal of it is to be able to read a text file of an unknown length (it's more or less a class roster w/ scores) and calculate and display the average of each student. The code compiles without errors but when run it has huge numbers as answers rather than an ID, name, and average.

Thanks for the help!

Text file
1
2
3
1000 John   9.5  9.0  8.5  8.0  8.5  87.0  92.5  86.0 
2000 Tom   10.0  6.7 10.0 10.0 10.0 100.0 100.0 100.0 
3000 Alice  6.9  8.0  8.0  8.0  8.0  80.0  80.0  80.0


Program
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void input_scores (int id[], char Name[], double avg[], char letter[], int size, char input_file[]);
void output_scores(int id[], char Name[], double avg[], char letter[], int size);

int main(){
	//Decimal fix
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

	//Defining inputs, ID, class size, etc
    ifstream in_file;
    char input_file[1024], letter[1024];
    int id[1024];
	int size = 0;
    double avg[1024];
	char Name[1024];

	//File input
	cout << "Enter an input file: ";
    cin >> input_file;
	in_file.open(input_file);

//	in_file.open("C:\\Temp.txt");
	
	//Error check
    if (in_file.fail()){
        cout << "Error: input file open failed.\n";
        return(1);
    }

	char line[1024];
	while(in_file.good()){
			in_file.getline(line,1024);
			size++;
	}
	
	cout << size << endl;

	//Functions for reading -> calculating -> outputting
    input_scores (id,Name,avg,letter,size,input_file);
    output_scores(id,Name,avg,letter,size);
	return 0;
}
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
void input_scores(int id[], char Name[], double avg[], char letter[], int size, char input_file[]){
	//Calculations and ID tracking
	double quiz1, quiz2, quiz3, quiz4, quiz5, midterm1, midterm2, final, grade;

	//Input text file
    ifstream in_file;
    in_file.open(input_file);
	
	//Calculates average and letter grade, then stores in array
    for (int i=0; i < size; i++){
		in_file >> id[i];
		in_file >> Name[i];
        in_file >> quiz1 >> quiz2 >> quiz3 >> quiz4 >> quiz5 >> midterm1 >> midterm2 >> final;

		//Average calculation and grade used for letter tracking
        avg[i] = ((((quiz1*10)+(quiz2*10)+(quiz3*10)+(quiz4*10)+(quiz5*10))/5)*.2) + (((midterm1+midterm2)/2)*.4) + (final*.4);
        grade  = ((((quiz1*10)+(quiz2*10)+(quiz3*10)+(quiz4*10)+(quiz5*10))/5)*.2) + (((midterm1+midterm2)/2)*.4) + (final*.4);

		//Grade
        if (grade > 89){
            letter[i] = 'A';
        }
        else if (grade > 79){
            letter[i] = 'B';
        }
        else if (grade > 69){
            letter[i] = 'C';
        }
        else if (grade > 59){
            letter[i] = 'D';
        }
        else if (grade <= 59){
            letter[i] = 'F';
        }
    }//End for

	//Close files
	in_file.close();
}//End 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
27
28
void output_scores(int id[], char Name[], double avg[], char letter[], int size){
	//High score and ID tracking
	int highstudent;
    double highscore;
    highscore = avg[0];
    highstudent = id[0];

	cout << "---------------------------------------------------" << endl;
    cout << "Course Report" << endl;
    cout << "---------------------------------------------------" << endl;

	//Out of student's IDs, average and grade
    for (int i=0; i < size; i++){
        cout << "  " << id[i] << " " << avg[i] << " (" << letter[i] << ") " << endl;
	}
	
	//Finds highest score
    for (int i=1; i < size; i++){
        if (avg[i] > avg[0]){
            highscore = avg[i];
            highstudent = id[i];
        }
    }//End for loop

	//Displays high score
    cout << "---------------------------------------------------" << endl;
    cout << "*** " << highstudent << " had the best score with an average of " << highscore << "! ***" << endl << endl;
}//End function 
Topic archived. No new replies allowed.