This program has been bugging me a lot to get working, so far I've had to fight through tons of errors Hydra style (every one I got rid of, 2 took its place). Then I finally got an error free compile, but the txt file where the answers should be was sadly empty. After more messing around, I got this crazy new error that doesn't show up on compile, rather it shows up while the program is running.

This program is designed to take a bunch of students and grades from the file "data.txt", manipulate the scores to get a final grade, and display the test average, homework average, and final grade, as well as a tally of how many of each grade the class got, in the file "out.txt".

Code time.

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;
struct studentType
{
	string idno;
	double test[2];
	double a[7];
};

studentType studentData[30];
double hwsum;
double testsum;
double hwavg;
double testavg;
double finalavg;
int finalavg2;
char grade;
int acount;
int bcount;
int ccount;
int dcount;
int ecount;
int read;
int index = 0;
ofstream outfile;

	int getInput(studentType studentData[30], ifstream& indata, ifstream& infile);
	void process(studentType studentData[30]);
	void display(const studentType studentData[30], ofstream& outfile);

int main()
{
	ifstream infile;

	ifstream indata;


  	outfile << "STUDENT ID  HW AVE  TEST AVE  FINAL SCORE  GRADE" << endl;

	getInput(studentData, indata, infile);

	outfile << endl;
	outfile << "Number of A's: " << acount << endl;
	outfile << "Number of B's: " << bcount << endl;
	outfile << "Number of C's: " << ccount << endl;
	outfile << "Number of D's: " << dcount << endl;
	outfile << "Number of E's: " << ecount << endl;
	infile.close();
	outfile.close();
	system("pause");
	return 0;
}

int getInput(studentType studentData[30], ifstream& indata, ifstream& infile)
{
	infile.open("data.txt");

	while(!infile.eof())
	{
		indata >> studentData[index].idno >> studentData[index].test[0] >> studentData[index].test[1] >> studentData[index].a[0] >> studentData[index].a[1] >> studentData[index].a[2] >> studentData[index].a[3] >> studentData[index].a[4] >> studentData[index].a[5] >> studentData[index].a[6];
		process(studentData);
		read += 1;
		index += 1;
	}
	return read;
}

void process(studentType studentData[30])
{
	int entry;
	for (entry = 0; entry < 2; entry++)
		testsum += studentData[index].test[entry];
	testavg = testsum / 2;
	for (entry = 0; entry < 7; entry++)
		hwsum += studentData[index].a[entry];
	hwavg = hwsum / 7;
	finalavg = (testavg * .6) + (hwavg * .4);
	finalavg2 = finalavg;
	switch(finalavg2 / 10)
	{
	case 0:
	case 1:
	case 2:
	case 3:
	case 4:
	case 5:
		grade = 'E';
		ecount += 1;
		break;
	case 6:
		grade = 'D';
		dcount += 1;
		break;
	case 7:
		grade = 'C';
		ccount += 1;
		break;
	case 8:
		grade = 'B';
		bcount += 1;
		break;
	case 9:
	case 10:
		grade = 'A';
		acount += 1;
		break;
	default:
		cout << "???" << endl;
	}
	display(studentData, outfile);

}

void display(const studentType studentData[30], ofstream& outfile)
{
	outfile.open("out.txt");

	outfile << setw(11) << studentData[index].idno << setw(8) << hwavg << setw(10) << testavg << setw(13) << finalavg << setw(7) << grade << endl;
}

First thing to do right now, is get rid of this new error that's been bugging me. It compiles fine, but then the program displays a bunch of rows of "???"s, and a box pops up saying this:

"Unhandled exception at 0x009c1cea in Assignment4.exe: 0xC0000005: Access violation reading location 0x009d3000."

Then it points to this line:

"testsum += studentData[index].test[entry];" (line 76)

Closing that out and hovering my mouse over some of the variables around there, I get this.

entry = 1
index = 232 (I'm guessing this is the problem, but I have no clue why it's happening)

Checking some of the variables I'm supposedly getting from the file, the first two variables (idno and test[0]) are "" and 0 respectivly. But the rest of them have this weird value of "0x009d3008". The text files that I'm using are in the right folder, so that isn't a problem.

I have no clue what to do to solve these problems I'm having, I'm pretty confident that if I can just get it to read and write correctly, it should give the right answers, but getting to that point has been heck so far, and I need to have this done by midnight tomorrow.

So if any kind soul is able to help me in any way, I would appreciate it very much.

EDIT: Minor breakthrough here, the error no longer comes up after I changed "indata" to "infile" in line 65. Now the program displays 8 rows of ??? and finishes running. But still nothing has gone to the text file.

In getInput() the first thing you need to do is check if the file was opened. Next, don't use eof() because it doesn't work the way you think it does.

int getInput(studentType studentData[30], ifstream& indata, ifstream& infile)
{
	infile.open("data.txt");
    if( infile.is_open() )
    {
            index = 0;
            read = 0; 
	    while(indata >> studentData[index].idno >> studentData[index].test[0] >> studentData[index].test[1] >> studentData[index].a[0] >> studentData[index].a[1] >> studentData[index].a[2] >> studentData[index].a[3] >> studentData[index].a[4] >> studentData[index].a[5] >> studentData[index].a[6])
	    {
		    process(studentData);
		    read += 1;
		    index += 1;
	    }
    }
	return read;
}

Thanks for the help, now I'm finally done getting any sort of errors, and I'm actually getting something in my output file!

Unfortunatly it's not exactly the right answer, all it prints are the headings and the counts of each grade, no actual student data is going into the file. But there's something in there, that's step one.

I also made several changes myself, I moved all the function calls in other functions to main, and inserted for loops in the other 2 functions starting index at 0 and counting up until it hits the amount read.

I'll continue to play around with it, and check back here if I just can't get it.

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.