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

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

#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;
}
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
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

Recommended Answers

All 2 Replies

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

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

void input_scores(int id[], [B]char Name[][/B], double avg[], char letter[], int size, char input_file[]){
...
	in_file >> Name[i];

Notice how Name is declared - it's an array of chars. That is, Name is a char. It means that you are reading not a name, but a single character. That means in turn that all subsequent reads are wrong.

BTW, a debugger helps immensely.

Notice how Name is declared - it's an array of chars. That is, Name is a char. It means that you are reading not a name, but a single character. That means in turn that all subsequent reads are wrong.

BTW, a debugger helps immensely.

Opps looks like I uploaded an older version of it. I got that fixed.

It turns out the program was having trouble passing the file_name between the main() and function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.