Array code need to figure out how close I am but won't compile.

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 586 Views - Last Post: 03 July 2012 - 09:05 PM Rate Topic: -----

#1 CoryMore  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 81
  • Joined: 26-June 12

Array code need to figure out how close I am but won't compile.

Posted 02 July 2012 - 07:57 PM

I'm attempting to do the following problem:


Write a program for the following problem. You’re given a file that contains a collection if IDs and scores (type int) for an exam in your computer course. You’re to compute the average of these scores and assign grades to each student according to the following rule:

If a student’s score is within 10 points (above or below) of the average, assign a grade of satisfactory. If a studnt’s score is more than 10 points above average, assign a grade of outstanding. If a student’s score is more than 10 points below average, assign a grade of unsatisfactory.

The output from your program should consist of a labeled three-column list that shows each ID, score, and corresponding grade. Use a struct to store each student’s data and an array of structs to store the whole class. The struct should have a data member for id, score, and grade.


The file given to us 'grades' is as follows:

3313 90 42 58 64 70 75 100
5688 88 48 79 70 79 70 94
4700 50 44 89 73 70 73 100
9561 88 69 88 87 84 63 98
3199 96 69 100 90 88 67 100
3768 78 57 80 59 57 15 60
8291 72 56 70 82 74 9 83
7754 76 62 93 100 78 41 58
8146 94 68 99 94 93 9 54
2106 98 47 96 94 70 27 100

So far, I have this code:
#include <fstream>		//required for file streams
#include <iostream>		//input/output
#include <cstdlib>		//for definition of EXIT_FAILURE
#include <string>
#include <iomanip>

using namespace std;

#define inFileGrades "grades.txt"	//grades
#define outFile "gradesOut.txt"	//grade sheet

//Functions used
void processGrades (ifstream&, ofstream&, int&, int&);		//process all 
void classAverage (double&, double&, string&);

int main()
{
	ifstream grades;		//input: grades
	ofstream gradesOut;		//output: all info per employee
	int studentSum;		//sum value
	int totalSum;			//totalSum
	studentSum = 0;			//initiate sum
	totalSum = 0;			//initiate total sum
	
	
	
	

	//Prepare files
	grades.open(inFileGrades);	//open grade file
	if (grades.fail())		//if file doesn't read in, fail message
	{
		cerr << "*** ERROR: Cannot open " << inFileGrades << " for input." << "\n";
		return EXIT_FAILURE;	//failure return
	}
	
	gradesOut.open(outFile);			//make output file
	if (gradesOut.fail())			//if file isn't made, fail message
	{
		cerr << "*** ERROR: Cannot open " << gradesOut << " for output." << "\n";
		return EXIT_FAILURE;	//failure return
	}
	
	processGrades(grades,		//process grade file to form output file
		gradesOut, 
		studentSum, 
		totalSum);	


	//Close files	
	grades.close();
	gradesOut.close();

	
	return 0;
}

void processGrades(ifstream& grades,
	 	ofstream& gradesOut,
		int& studentSum,
		int& totalSum)
		
{
	const int MAX_IDS = 10;
	const int MAX_SCORE = 7;	
	int id[MAX_IDS];		//student id
	int score[MAX_SCORE];		//first score
	double studentAverage[MAX_IDS];		//average value
	double totalAverage;
	string third[MAX_IDS];
	
	
	for (int j = 0;
		j < MAX_IDS;
		j++)
	{
		for (int i = 0;
			i < MAX_SCORE;
			i++)
		{
			grades >> id[j] >> score[i];
			studentSum += score[i];
			studentAverage[j] = studentSum / MAX_SCORE;
			totalSum = totalSum + studentSum;
		}
		studentSum = 0;
	}
	totalAverage = totalSum / MAX_IDS;
	
	classAverage (studentAverage, 
		totalAverage, 
		third);
	
	for (int j = 0;
		j < MAX_IDS;
		j++)
	{
	gradesOut << setw(4) << id[j] << setw(8) << studentAverage[j] << setw(14) << third[j];
	}

}

void classAverage (double& studentAverage, 
		double& totalAverage,
		string& third)
{
	if (studentAverage < totalAverage - 10)
		{
			third == "Unsatisfactory";
		}
	else if (studentAverage > totalAverage + 10)
		{
			third == "Oustanding";
		}
	else
		{
			third == "Satisfactory";
		}
}



I'm not sure how close I am, because it will not compile do to this error.
./grader.cpp: In function `void processGrades(std::ifstream&, std::ofstream&, int&, int&)':
./grader.cpp:97: error: invalid initialization of non-const reference of type 'double&' from a temporary of type 'double*'
./grader.cpp:19: error: in passing argument 1 of `void classAverage(double&, double&, std::string&)'

I appreciate any advice.

Is This A Good Question/Topic? 0
  • +

Replies To: Array code need to figure out how close I am but won't compile.

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1937
  • View blog
  • Posts: 5,763
  • Joined: 05-May 12

Re: Array code need to figure out how close I am but won't compile.

Posted 02 July 2012 - 08:22 PM

Looks to me like studentAverage is an array of double's but you are trying to pass it as the first parameter to classAverage() with takes a reference to a double. The compiler is justified in complaining.
Was This Post Helpful? 1
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon

Reputation: 3058
  • View blog
  • Posts: 9,305
  • Joined: 25-December 09

Re: Array code need to figure out how close I am but won't compile.

Posted 02 July 2012 - 08:24 PM

Your function is defined as:
void classAverage (double&, double&, string&);

And the error is:

Quote

./grader.cpp:97: error: invalid initialization of non-const reference of type 'double&' from a temporary of type 'double*'
./grader.cpp:19: error: in passing argument 1 of `void classAverage(double&, double&, std::string&)'

Look closely at the first argument of your function. You said that you will be passing a single double value by reference into this function. In the following snippet:
	double studentAverage[MAX_IDS];	
	classAverage (studentAverage, totalAverage, third);

How is studentAverage defined? Is it a single double? What are you trying to pass into the function? A single double value or the entire array? Looking at your function it looks like you want to pass a single double, so you will need to send this single double. Maybe you need to call this function inside a loop?

Jim
Was This Post Helpful? 1
  • +
  • -

#4 CoryMore  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 81
  • Joined: 26-June 12

Re: Array code need to figure out how close I am but won't compile.

Posted 02 July 2012 - 10:11 PM

I see what you were saying, in fact I was making things too hard. Now I just think my logic is wrong, I'll have to think on it. I haven't quite wrapped my head around arrays yet.



#include <fstream>		//required for file streams
#include <iostream>		//input/output
#include <cstdlib>		//for definition of EXIT_FAILURE
#include <string>
#include <iomanip>

using namespace std;

#define inFileGrades "grades.txt"	//grades
#define outFile "gradesOut.txt"	//grade sheet

//Functions used
void processGrades (ifstream&, ofstream&, int&, int&);		//process all 


int main()
{
	ifstream grades;		//input: grades
	ofstream gradesOut;		//output: all info per employee
	int studentSum;		//sum value
	int totalSum;			//totalSum
	studentSum = 0;			//initiate sum
	totalSum = 0;			//initiate total sum
	
	
	
	

	//Prepare files
	grades.open(inFileGrades);	//open grade file
	if (grades.fail())		//if file doesn't read in, fail message
	{
		cerr << "*** ERROR: Cannot open " << inFileGrades << " for input." << "\n";
		return EXIT_FAILURE;	//failure return
	}
	
	gradesOut.open(outFile);			//make output file
	if (gradesOut.fail())			//if file isn't made, fail message
	{
		cerr << "*** ERROR: Cannot open " << gradesOut << " for output." << "\n";
		return EXIT_FAILURE;	//failure return
	}
	
	processGrades(grades,		//process grade file to form output file
		gradesOut, 
		studentSum, 
		totalSum);	


	//Close files	
	grades.close();
	gradesOut.close();

	
	return 0;
}

void processGrades(ifstream& grades,
	 	ofstream& gradesOut,
		int& studentSum,
		int& totalSum)
		
{
	const int MAX_IDS = 10;
	const int MAX_SCORE = 7;	
	int id[MAX_IDS];		//student id
	int score[MAX_SCORE];		//first score
	double studentAverage[MAX_IDS];		//average value
	double totalAverage;
	string third[MAX_IDS];
	
	
	for (int j = 0;
		j < MAX_IDS;
		j++)
	{
		for (int i = 0;
			i < MAX_SCORE;
			i++)
		{
			grades >> id[j] >> score[i];
			studentSum += score[i];
			studentAverage[j] = studentSum / MAX_SCORE;
			totalSum = totalSum + studentSum;
		}
		studentSum = 0;
	}
	totalAverage = totalSum / MAX_IDS;
	
	
	for (int j = 0;
		j < MAX_IDS;
		j++)
	{
		
		if (studentAverage[j] < totalAverage - 10)
		{
			third[j] == "Unsatisfactory";
		}
		else if (studentAverage[j] > totalAverage + 10)
		{
			third[j] == "Oustanding";
		}
		else
		{
			third[j] == "Satisfactory";
		}

	gradesOut << setw(4) << id[j] << setw(8) << studentAverage[j] << setw(14) << third[j] << "\n";
	}

}


Was This Post Helpful? 0
  • +
  • -

#5 CoryMore  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 81
  • Joined: 26-June 12

Re: Array code need to figure out how close I am but won't compile.

Posted 03 July 2012 - 11:55 AM

I just realized I'm supposed to "Use a struct to store each student’s data and an array of structs to store the whole class. The struct should have a data member for id, score, and grade." My current setup doesn't accomplish this correct? I may have to go back to the drawing board...
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5675
  • View blog
  • Posts: 22,537
  • Joined: 23-August 08

Re: Array code need to figure out how close I am but won't compile.

Posted 03 July 2012 - 12:00 PM

There is no struct in your code, so I'd say yes...you need to rethink how you're doing this.
Was This Post Helpful? 1
  • +
  • -

#7 CoryMore  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 81
  • Joined: 26-June 12

Re: Array code need to figure out how close I am but won't compile.

Posted 03 July 2012 - 03:28 PM

Well, I think I'm getting closer in theory, but it's still not right.



//grader.cpp
//Grades
//Cory Morewitz
//7/2/2012

#include <fstream>		//required for file streams
#include <iostream>		//input/output
#include <cstdlib>		//for definition of EXIT_FAILURE
#include <string>
#include <iomanip>

using namespace std;

#define inFileGrades "grades.txt"	//grades
#define outFile "gradesOut.txt"	//grade sheet

//Functions used
void marks(ifstream&, ofstream&);

int main()
{
	ifstream grades;		//input: grades
	ofstream gradesOut;		//output: all info per employee
	
	//Prepare files
	grades.open(inFileGrades);	//open grade file
	if (grades.fail())		//if file doesn't read in, fail message
	{
		cerr << "*** ERROR: Cannot open " << inFileGrades << " for input." << "\n";
		return EXIT_FAILURE;	//failure return
	}
	
	gradesOut.open(outFile);			//make output file
	if (gradesOut.fail())			//if file isn't made, fail message
	{
		cerr << "*** ERROR: Cannot open " << gradesOut << " for output." << "\n";
		return EXIT_FAILURE;	//failure return
	}
	
	marks(grades,		//process grade file to form output file
		gradesOut);	


	//Close files	
	grades.close();
	gradesOut.close();

	
	return 0;
}

void marks (ifstream& grades,
		ofstream& gradesOut)
{
	const int MAX_IDS = 10;
	const int MAX_SCORE = 7;
	int sum;
	float totalAverage;
	struct examStats
	{
		int id[MAX_IDS];
		int scores[MAX_SCORE];
		float average;
		string success;
	};
	struct examStats aStudent;
		for (int j = 0;
			j < MAX_IDS;
			j++)
		{
			for (int i = 0;
				i < MAX_SCORE;
				i++)
			{
				grades >> aStudent.id[j] >> aStudent.scores[i];
				sum = 0;
				sum += aStudent.scores[i];
			}
			aStudent.average = 0;
			aStudent.average = (sum / MAX_SCORE);
			totalAverage = 0;
			totalAverage += aStudent.average;
		}
		totalAverage = (totalAverage / MAX_IDS);
		
		gradesOut << setw(4) << "ID" << setw(8) << "Student Average" << setw(14) << "Grade" << "\n";
		for (int j = 0;
			j < MAX_IDS;
			j++)
			gradesOut <<setw(4) << aStudent.id[j] << setw(8) << aStudent.average << setw(14) << totalAverage << "\n";
}
	



My output file is off though, it is as follows:

Quote

IDStudent Average Grade
70 13 1.3
69 13 1.3
3768 13 1.3
9 13 1.3
94 13 1.3
27 13 1.3
2664824 13 1.3
1628822316 13 1.3
1628995352 13 1.3
1629093531 13 1.3

Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon

Reputation: 3058
  • View blog
  • Posts: 9,305
  • Joined: 25-December 09

Re: Array code need to figure out how close I am but won't compile.

Posted 03 July 2012 - 04:31 PM

You will need an array of your structure if you want to retrieve the data from the file in one loop then print this information out in another loop. Also in the following snippet:
	struct examStats
	{
		int id[MAX_IDS];
		int scores[MAX_SCORE];
		float average;
		string success;
	};

Why is id an array? It shouldn't need to be an array, you should use an array of your structure because you have multiple students.

Next I suggest you get used to writing your for loops like:
for (int j = 0; j < MAX_IDS; j++)
{

In my opinion you will have fewer problems keeping this control statement on one line. I also recommend that you move the definition of your structure outside any functions. Then you will be able to reuse this structure in your other functions.

Also you need to look closely at your input file:

Quote

3313 90 42 58 64 70 75 100
5688 88 48 79 70 79 70 94

Notice the first item on each line is your student's ID number. There is only one student ID number per line. Which means you should not be retrieving this information inside your second loop, which is the loop to retrieve the student's grades, maybe it should be inside the first loop.

Jim
Was This Post Helpful? 1
  • +
  • -

#9 CoryMore  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 81
  • Joined: 26-June 12

Re: Array code need to figure out how close I am but won't compile.

Posted 03 July 2012 - 05:48 PM

I think I see what you're saying, though moving the struct seemed to throw thing off, should I put it before main? How do I make a struct an array, I tried to add [MAX_IDS] it was a nogo. Currently, I THINK because it's not an array, my program is only showing me the last one.

#include <fstream>		//required for file streams
#include <iostream>		//input/output
#include <cstdlib>		//for definition of EXIT_FAILURE
#include <string>
#include <iomanip>

using namespace std;

#define inFileGrades "grades.txt"	//grades
#define outFile "gradesOut.txt"	//grade sheet

//Functions used
void marks(ifstream&, ofstream&);

int main()
{
	ifstream grades;		//input: grades
	ofstream gradesOut;		//output: all info per employee
	
	//Prepare files
	grades.open(inFileGrades);	//open grade file
	if (grades.fail())		//if file doesn't read in, fail message
	{
		cerr << "*** ERROR: Cannot open " << inFileGrades << " for input." << "\n";
		return EXIT_FAILURE;	//failure return
	}
	
	gradesOut.open(outFile);			//make output file
	if (gradesOut.fail())			//if file isn't made, fail message
	{
		cerr << "*** ERROR: Cannot open " << gradesOut << " for output." << "\n";
		return EXIT_FAILURE;	//failure return
	}
	
	marks(grades,		//process grade file to form output file
		gradesOut);	


	//Close files	
	grades.close();
	gradesOut.close();

	
	return 0;
}


void marks (ifstream& grades,
		ofstream& gradesOut)
{
	const int MAX_IDS = 10;
	const int MAX_SCORE = 7;
	int sum = 0;
	float totalAverage;
	struct examStats
	{
		int id;
		int scores[MAX_SCORE];
		float average;
		string success;
	};
	examStats aStudent;
		for (int count = 0; count < MAX_IDS; count++)
		{
			grades >> aStudent.id;
			for (int i = 0;
				i < MAX_SCORE;
				i++)
			{
				grades >> aStudent.scores[i];
				sum += aStudent.scores[i];
			}
			aStudent.average = (sum / MAX_SCORE);
			totalAverage += aStudent.average;
		}
		totalAverage = (totalAverage / MAX_IDS);
		
		cout << setw(4) << "ID" << setw(8) << "Student Average" << setw(14) << "Grade" << "\n";
		for (int count = 0;
			count < MAX_IDS;
			count++)
			cout <<setw(4) << aStudent.id << setw(8) << aStudent.average << setw(14) << totalAverage << "\n";
}



Output:

Quote

IDStudent Average Grade
2106 730 1.93479e+19
2106 730 1.93479e+19
2106 730 1.93479e+19
2106 730 1.93479e+19
2106 730 1.93479e+19
2106 730 1.93479e+19
2106 730 1.93479e+19
2106 730 1.93479e+19
2106 730 1.93479e+19
2106 730 1.93479e+19

Was This Post Helpful? 0
  • +
  • -

#10 CoryMore  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 81
  • Joined: 26-June 12

Re: Array code need to figure out how close I am but won't compile.

Posted 03 July 2012 - 06:23 PM

I feel like I'm closer, but now I'm getting an error when compiling.

Quote

./graderNew.cpp:86: error: expected primary-expression before "void"



#include <fstream>		//required for file streams
#include <iostream>		//input/output
#include <cstdlib>		//for definition of EXIT_FAILURE
#include <string>
#include <iomanip>

using namespace std;

#define inFileGrades "grades.txt"	//grades
#define outFile "gradesOut.txt"	//grade sheet
const int MAX_IDS = 10;
const int MAX_SCORE = 7;
struct examStats
{
	int id;
	int scores[MAX_SCORE];
	float average;
	string success;
}examstats[MAX_IDS];

//Functions used
void marks(ifstream&, ofstream&);
void printexamStats (examStats examstats);

int main()
{
	ifstream grades;		//input: grades
	ofstream gradesOut;		//output: all info per employee
	
	//Prepare files
	grades.open(inFileGrades);	//open grade file
	if (grades.fail())		//if file doesn't read in, fail message
	{
		cerr << "*** ERROR: Cannot open " << inFileGrades << " for input." << "\n";
		return EXIT_FAILURE;	//failure return
	}
	
	gradesOut.open(outFile);			//make output file
	if (gradesOut.fail())			//if file isn't made, fail message
	{
		cerr << "*** ERROR: Cannot open " << gradesOut << " for output." << "\n";
		return EXIT_FAILURE;	//failure return
	}
	
	marks(grades,		//process grade file to form output file
		gradesOut);	


	//Close files	
	grades.close();
	gradesOut.close();

	
	return 0;
}

void marks (ifstream& grades,
		ofstream& gradesOut)
{
	int sum = 0;
	float totalAverage;
	examStats examstats;
		for (int i = 0; i < MAX_IDS; i++)
		{
			grades >> examstats.id;
			for (int i = 0;
				i < MAX_SCORE;
				i++)
			{
				grades >> examstats.scores[i];
				sum += examstats.scores[i];
			}
			examstats.average = (sum / MAX_SCORE);
			totalAverage += examstats.average;
		}
		totalAverage = (totalAverage / MAX_IDS);
		
		cout << setw(4) << "ID" << setw(8) << "Student Average" << setw(14) << "Grade" << "\n";
		for (int i = 0; i < MAX_IDS; i++)
		{
			printexamStats (examstats[i]);
		}
}

void printexamStats (examStats examstats)
{
cout << setw(4) << examstats.id << setw(8) << examstats.average << setw(14) << "\n";
}



Sorry, old error. The current error is

./graderNew.cpp:86: error: no match for 'operator[]' in 'examstats[i]'


View PostCoryMore, on 03 July 2012 - 06:22 PM, said:

I feel like I'm closer, but now I'm getting an error when compiling.

Quote

./graderNew.cpp:86: error: expected primary-expression before "void"



#include <fstream>		//required for file streams
#include <iostream>		//input/output
#include <cstdlib>		//for definition of EXIT_FAILURE
#include <string>
#include <iomanip>

using namespace std;

#define inFileGrades "grades.txt"	//grades
#define outFile "gradesOut.txt"	//grade sheet
const int MAX_IDS = 10;
const int MAX_SCORE = 7;
struct examStats
{
	int id;
	int scores[MAX_SCORE];
	float average;
	string success;
}examstats[MAX_IDS];

//Functions used
void marks(ifstream&, ofstream&);
void printexamStats (examStats examstats);

int main()
{
	ifstream grades;		//input: grades
	ofstream gradesOut;		//output: all info per employee
	
	//Prepare files
	grades.open(inFileGrades);	//open grade file
	if (grades.fail())		//if file doesn't read in, fail message
	{
		cerr << "*** ERROR: Cannot open " << inFileGrades << " for input." << "\n";
		return EXIT_FAILURE;	//failure return
	}
	
	gradesOut.open(outFile);			//make output file
	if (gradesOut.fail())			//if file isn't made, fail message
	{
		cerr << "*** ERROR: Cannot open " << gradesOut << " for output." << "\n";
		return EXIT_FAILURE;	//failure return
	}
	
	marks(grades,		//process grade file to form output file
		gradesOut);	


	//Close files	
	grades.close();
	gradesOut.close();

	
	return 0;
}

void marks (ifstream& grades,
		ofstream& gradesOut)
{
	int sum = 0;
	float totalAverage;
	examStats examstats;
		for (int i = 0; i < MAX_IDS; i++)
		{
			grades >> examstats.id;
			for (int i = 0;
				i < MAX_SCORE;
				i++)
			{
				grades >> examstats.scores[i];
				sum += examstats.scores[i];
			}
			examstats.average = (sum / MAX_SCORE);
			totalAverage += examstats.average;
		}
		totalAverage = (totalAverage / MAX_IDS);
		
		cout << setw(4) << "ID" << setw(8) << "Student Average" << setw(14) << "Grade" << "\n";
		for (int i = 0; i < MAX_IDS; i++)
		{
			printexamStats (examstats[i]);
		}
}

void printexamStats (examStats examstats)
{
cout << setw(4) << examstats.id << setw(8) << examstats.average << setw(14) << "\n";
}


Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is offline

  • member icon

Reputation: 3058
  • View blog
  • Posts: 9,305
  • Joined: 25-December 09

Re: Array code need to figure out how close I am but won't compile.

Posted 03 July 2012 - 06:37 PM

In your marks() function is examstats an array? Doesn't look like one to me.

void marks (ifstream& grades,
		ofstream& gradesOut)
{
	int sum = 0;
	float totalAverage;
	examStats examstats; // Is this an array???



This is one of the problems you face when you try to use global variables.
struct examStats
{
	int id;
	int scores[MAX_SCORE];
	float average;
	string success;
}examstats[MAX_IDS];  // Why this global instance????



When you have a variable with the same name in a local scope the more global variable is not used. Solution, don't use global variables.


Jim
Was This Post Helpful? 1
  • +
  • -

#12 CoryMore  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 81
  • Joined: 26-June 12

Re: Array code need to figure out how close I am but won't compile.

Posted 03 July 2012 - 06:39 PM

Sorry, I thought I was to move it out of the function
Was This Post Helpful? 0
  • +
  • -

#13 CoryMore  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 81
  • Joined: 26-June 12

Re: Array code need to figure out how close I am but won't compile.

Posted 03 July 2012 - 07:02 PM

I took a step back here somewhere.

#include <fstream>		//required for file streams
#include <iostream>		//input/output
#include <cstdlib>		//for definition of EXIT_FAILURE
#include <string>
#include <iomanip>

using namespace std;

#define inFileGrades "grades.txt"	//grades
#define outFile "gradesOut.txt"	//grade sheet

//Functions used
void marks(ifstream&, ofstream&);

int main()
{
	ifstream grades;		//input: grades
	ofstream gradesOut;		//output: all info per employee
	
	//Prepare files
	grades.open(inFileGrades);	//open grade file
	if (grades.fail())		//if file doesn't read in, fail message
	{
		cerr << "*** ERROR: Cannot open " << inFileGrades << " for input." << "\n";
		return EXIT_FAILURE;	//failure return
	}
	
	gradesOut.open(outFile);			//make output file
	if (gradesOut.fail())			//if file isn't made, fail message
	{
		cerr << "*** ERROR: Cannot open " << gradesOut << " for output." << "\n";
		return EXIT_FAILURE;	//failure return
	}
	
	marks(grades,		//process grade file to form output file
		gradesOut);	


	//Close files	
	grades.close();
	gradesOut.close();

	
	return 0;
}


void marks (ifstream& grades,
		ofstream& gradesOut)
{
	const int MAX_IDS = 10;
	const int MAX_SCORE = 7;
	int sum = 0;
	float totalAverage;
	struct examStats
	{
		int id;
		int scores[MAX_SCORE];
		float average;
		string success;
	}aStudent[MAX_IDS];
	
		for (int count = 0; count < MAX_IDS; count++)
		{
			grades >> aStudent.id;
			for (int i = 0;
				i < MAX_SCORE;
				i++)
			{
				grades >> aStudent.scores[i];
				sum += aStudent.scores[i];
			}
			aStudent.average = (sum / MAX_SCORE);
			totalAverage += aStudent.average;
		}
		totalAverage = (totalAverage / MAX_IDS);
		
		cout << setw(4) << "ID" << setw(8) << "Student Average" << setw(14) << "Grade" << "\n";
		for (int count = 0;
			count < MAX_IDS;
			count++)
			cout <<setw(4) << aStudent.id << setw(8) << aStudent.average << setw(14) << totalAverage << "\n";
}


Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg  Icon User is offline

  • member icon

Reputation: 3058
  • View blog
  • Posts: 9,305
  • Joined: 25-December 09

Re: Array code need to figure out how close I am but won't compile.

Posted 03 July 2012 - 07:08 PM

I said to take the definition out of the function, not use a global instance. The following is a structure definition.
	struct examStats
	{
		int id;
		int scores[MAX_SCORE];
		float average;
		string success;
	};

Notice the lack of a variable name at the bottom. The following is a structure definition with a global instance.
	struct examStats
	{
		int id;
		int scores[MAX_SCORE];
		float average;
		string success;
	}aStudent[MAX_IDS];  // aStudent is a global array.



Then in your function create the proper instance:
void someFunction()
{
   examStats students[Max_IDS]; // Create an array of examStats.




Jim
Was This Post Helpful? 1
  • +
  • -

#15 CoryMore  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 81
  • Joined: 26-June 12

Re: Array code need to figure out how close I am but won't compile.

Posted 03 July 2012 - 07:24 PM

I'm sorry, I feel dense. I'm more confused now than I was. This is where I'm at, but I keep getting the errors:

./graderNew.cpp: In function `void marks(std::ifstream&, std::ofstream&)':
./graderNew.cpp:68: error: request for member `id' in `student', which is of non-class type `marks(std::ifstream&, std::ofstream&)::examStats[10]'
./graderNew.cpp:71: error: expected primary-expression before '.' token
./graderNew.cpp:72: error: expected primary-expression before '.' token
./graderNew.cpp:74: error: request for member `average' in `student', which is of non-class type `marks(std::ifstream&, std::ofstream&)::examStats[10]'
./graderNew.cpp:75: error: expected primary-expression before '.' token
./graderNew.cpp:82: error: request for member `id' in `student', which is of non-class type `int'
./graderNew.cpp:82: error: expected primary-expression before '.' token


#include <fstream>		//required for file streams
#include <iostream>		//input/output
#include <cstdlib>		//for definition of EXIT_FAILURE
#include <string>
#include <iomanip>

using namespace std;

#define inFileGrades "grades.txt"	//grades
#define outFile "gradesOut.txt"	//grade sheet


//Functions used
void marks(ifstream&, ofstream&);

int main()
{
	ifstream grades;		//input: grades
	ofstream gradesOut;		//output: all info per employee
	
	//Prepare files
	grades.open(inFileGrades);	//open grade file
	if (grades.fail())		//if file doesn't read in, fail message
	{
		cerr << "*** ERROR: Cannot open " << inFileGrades << " for input." << "\n";
		return EXIT_FAILURE;	//failure return
	}
	
	gradesOut.open(outFile);			//make output file
	if (gradesOut.fail())			//if file isn't made, fail message
	{
		cerr << "*** ERROR: Cannot open " << gradesOut << " for output." << "\n";
		return EXIT_FAILURE;	//failure return
	}
	
	marks(grades,		//process grade file to form output file
		gradesOut);	


	//Close files	
	grades.close();
	gradesOut.close();

	
	return 0;
}


void marks (ifstream& grades,
		ofstream& gradesOut)
{
	int sum = 0;
	float totalAverage;
	const int MAX_IDS = 10;
	const int MAX_SCORE = 7;
	int student;
	struct examStats
	{
		int id;
		int scores[MAX_SCORE];
		float average;
		string success;
	};
	
		for (int i = 0; i < MAX_IDS; i++)
		{
			examStats student[MAX_IDS];
			grades >> student.id;
			for (int n = 0; n < MAX_SCORE; n++)
			{
				grades >> examStats.scores[n];
				sum += examStats.scores[n];
			}
			student.average = (sum / MAX_SCORE);
			totalAverage += examStats.average;
		}
		totalAverage = (totalAverage / MAX_IDS);
		
		cout << setw(4) << "ID" << setw(8) << "Student Average" << setw(14) << "Grade" << "\n";
		for (int i = 0; i < MAX_IDS; i++)
		{
			cout << setw(4) << student.id << setw(8) << examStats.average << setw(14) << "\n";

		}
}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2