C++ records(structs)

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
I have a file that contains 20 names and scores. I need to read them into a struct then find the letter grade for each student and the highest grade. Haven't gotten to the highest grade one yet. I am getting confused quick, thought this was going to be easy but I was wrong. Insight from you guy always clears me up =D I am just confused on how to create the struct then put an array inside of it and then read the file into it. Here is what I have so far:

Code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

struct studentType
	{
		string studentFName;
		string studentLName;
		int testScore;
		char grade;
	};

void getData (ifstream& inFile, studentType sList[], int size);
void calcGrade (studentType sList[], int size);
//int highScore (const studentType sList[], int size);
void printOut (ofstream& outFile, const studentType sList[], int size);

int main() {
	
	ifstream inp;
	ofstream outp;

	inp.open("C:\\Users\\Welcome Back\\Desktop\\Ch11_Ex01Data.txt");
	outp.open("C:\\Users\\Welcome Back\\Desktop\\Ch11_Ex01Out.txt");

	studentType sList[20];
	int size;
	
	

	void printOut(outp, sList, size);



	return 0;
}

void getData(ifstream& inp, studentType& sList, int size)
{
	struct studentType sList[20]
	{
		string studentFName;
		string studentLName;
		int testScore;
		char grade;
	}

	for (size = 0; size < 20; size++)
	{
		infile >> sList[size].studentFName
				>> sList[size].studentLName
				>> sList[size].testScore;
	}
	calcGrade(sList, size);
}

void calcGrade(studentType& sList, int size)
{
	
	for (size = 0; size < 20; size++)
	{
	if (sList[size].testScore >= 90)
		sList.grade = 'A';
	else if (sList[size].testScore >= 80)
		sList.grade = 'B';
	else if (sList[size].testScore >= 70)
		sList.grade = 'C';
	else if (sList[size].testScore >= 60)
		sList.grade = 'D';
	else
		sList.grade = 'F';
	}
}

void printOut(ofstream& outp, studentType& sList, int size)
{
	getData(inp, sList, size);

	for (size = 0; size <20; size++)
	{
		outp << sList[size].studentLName << ", " << sList[size].studentFName << ", " << sList[size].testScore << ", " << sList[size].grade << endl;
	}
}

ERRORS:
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(35) : error C2182: 'printOut' : illegal use of type 'void'
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(35) : error C2078: too many initializers
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(45) : error C2082: redefinition of formal parameter 'sList'
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(45) : error C2601: 'sList' : local function definitions are illegal
1> c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(43): this line contains a '{' which has not yet been matched
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(50) : fatal error C1903: unable to recover from previous error(s); stopping compilation
 

Zenthar

Distinguished
Dec 31, 2007
250
0
18,960
First, I'd like to point-out that your function's prototype differ from the actual implementation; in your prototype you take an array of studentType (studentType sList[]) )and in the actual function you take a reference to a single studentType (studentType& sList). Moreover, I think you cannot use studentType directly as a type, you have to say "struct studentType" or declare a type explicitly with typedef.

Fix those and let us know how it goes.

On a side-note, a little "best practice" tip: use a defined value for your array size, you will have much less code to change if you must expand the array.
Code:
#define ARRAY_SIZE 20

studentType sList[ARRAY_SIZE];

for (size = 0; size < ARRAY_SIZE; size++)
{
    // ...
}
 

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
Ok I think I changed what you were saying. When I do a function call do I just list the array sList or do i need to call studentType sList.

Code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

const int arraysize = 20;
struct studentType sList[arraysize]
	{
		string studentFName;
		string studentLName;
		int testScore;
		char grade;
	};

void getData (ifstream& inFile, studentType sList[], int size);
void calcGrade (studentType sList[], int size);
//int highScore (const studentType sList[], int size);
void printOut (ofstream& outFile, const studentType sList[], int size);

int main() {
	
	ifstream inp;
	ofstream outp;

	inp.open("C:\\Users\\Welcome Back\\Desktop\\Ch11_Ex01Data.txt");
	outp.open("C:\\Users\\Welcome Back\\Desktop\\Ch11_Ex01Out.txt");

	int size;
	
	

	void printOut(outp, sList, size);



	return 0;
}

void getData(ifstream& inp, studentType sList, int size)
{
	struct studentType sList[arraysize]
	{
		string studentFName;
		string studentLName;
		int testScore;
		char grade;
	}

	for (size = 0; size < arraysize; size++)
	{
		infile >> sList[size].studentFName
				>> sList[size].studentLName
				>> sList[size].testScore;
	}
	calcGrade(sList, size);
}

void calcGrade(studentType sList, int size)
{
	
	for (size = 0; size < arraysize; size++)
	{
	if (sList[size].testScore >= 90)
		sList.grade = 'A';
	else if (sList[size].testScore >= 80)
		sList.grade = 'B';
	else if (sList[size].testScore >= 70)
		sList.grade = 'C';
	else if (sList[size].testScore >= 60)
		sList.grade = 'D';
	else
		sList.grade = 'F';
	}
}

void printOut(ofstream& outp, studentType sList, int size)
{
	getData(inp, sList, size);

	for (size = 0; size < arraysize; size++)
	{
		outp << sList[size].studentLName << ", " << sList[size].studentFName << ", " << sList[size].testScore << ", " << sList[size].grade << endl;
	}
}

ERRORS:
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(11) : error C2470: 'sList' : looks like a function definition, but there is no parameter list; skipping apparent body
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(35) : error C2065: 'sList' : undeclared identifier
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(35) : error C2182: 'printOut' : illegal use of type 'void'
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(35) : error C2078: too many initializers
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(45) : error C2082: redefinition of formal parameter 'sList'
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(45) : error C2601: 'sList' : local function definitions are illegal
1> c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(43): this line contains a '{' which has not yet been matched


damn structs looked so easy, this array in a struct is messing me up. My book doesn't show like example problems after explaining what they are which is how I usually learn, seeing it in an actually program and the proper uses of it.
 

Ijack

Distinguished
You are confusing your definition of the struct and your declaration of instances of it.

In lines 10-16 you are defining the struct "studentType"; don't include the "sList[20]" declaration.

Later when you use the struct just define variables as "struct studentType sList" or "struct studentType sList[20]", depending upon whether you want a scalar or an array. Don't repeat the definition (as you do in lines 45-50).

You are also making an error in line 35 where you call the function "printOut" as "void printOut ..."; return types are only specified when you declare or define a function, not when you call it.

Correct those errors and the try compiling again.
 

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
Got a lot of errors, listed below. Okay don't think I made all the changes I needed to, but here is what I thought you were saying. Also are you saying when I go to make an array inside the struct I just use it without defining it? Also when reading into the array like in 50-55ish do i need to type studentType sList[counter].name or is the sList[counter].name the way to do it?


also do i have to put in main like studentType sList[arraysize]? seems like the printOut call doesnt know what sList is.
Code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

const int arraysize = 20;
struct studentType
	{
		string studentFName;
		string studentLName;
		int testScore;
		char grade;
	};

void getData (ifstream& inFile, studentType sList[], int size);
void calcGrade (studentType sList[], int size);
//int highScore (const studentType sList[], int size);
void printOut (ofstream& outFile, const studentType sList[], int size);

int main() {
	
	ifstream inp;
	ofstream outp;

	inp.open("C:\\Users\\Welcome Back\\Desktop\\Ch11_Ex01Data.txt");
	outp.open("C:\\Users\\Welcome Back\\Desktop\\Ch11_Ex01Out.txt");

	int size;
	
	

	printOut(outp, sList, size);



	return 0;
}

void getData(ifstream& inp, studentType sList, int size)
{
	

	for (size = 0; size < arraysize; size++)
	{
		infile >> sList[size].studentFName
				>> sList[size].studentLName
				>> sList[size].testScore;
	}
	calcGrade(sList, size);
}

void calcGrade(studentType sList, int size)
{
	
	for (size = 0; size < arraysize; size++)
	{
	if (sList[size].testScore >= 90)
		sList.grade = 'A';
	else if (sList[size].testScore >= 80)
		sList.grade = 'B';
	else if (sList[size].testScore >= 70)
		sList.grade = 'C';
	else if (sList[size].testScore >= 60)
		sList.grade = 'D';
	else
		sList.grade = 'F';
	}
}

void printOut(ofstream& outp, studentType sList, int size)
{
	getData(inp, sList, size);

	for (size = 0; size < arraysize; size++)
	{
		outp << sList[size].studentLName << ", " << sList[size].studentFName << ", " << sList[size].testScore << ", " << sList[size].grade << endl;
	}
}

ERRORS:
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(35) : error C2065: 'sList' : undeclared identifier
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(48) : error C2065: 'infile' : undeclared identifier
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(48) : error C2676: binary '[' : 'studentType' does not define this operator or a conversion to a type acceptable to the predefined operator
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(49) : error C2228: left of '.studentFName' must have class/struct/union
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(49) : error C2676: binary '[' : 'studentType' does not define this operator or a conversion to a type acceptable to the predefined operator
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(50) : error C2228: left of '.studentLName' must have class/struct/union
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(50) : error C2676: binary '[' : 'studentType' does not define this operator or a conversion to a type acceptable to the predefined operator
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(50) : error C2228: left of '.testScore' must have class/struct/union
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(52) : error C2664: 'calcGrade' : cannot convert parameter 1 from 'studentType' to 'studentType []'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(60) : error C2676: binary '[' : 'studentType' does not define this operator or a conversion to a type acceptable to the predefined operator
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(60) : error C2228: left of '.testScore' must have class/struct/union
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(62) : error C2676: binary '[' : 'studentType' does not define this operator or a conversion to a type acceptable to the predefined operator
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(62) : error C2228: left of '.testScore' must have class/struct/union
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(64) : error C2676: binary '[' : 'studentType' does not define this operator or a conversion to a type acceptable to the predefined operator
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(64) : error C2228: left of '.testScore' must have class/struct/union
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(66) : error C2676: binary '[' : 'studentType' does not define this operator or a conversion to a type acceptable to the predefined operator
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(66) : error C2228: left of '.testScore' must have class/struct/union
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(75) : error C2065: 'inp' : undeclared identifier
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(79) : error C2676: binary '[' : 'studentType' does not define this operator or a conversion to a type acceptable to the predefined operator
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(79) : error C2228: left of '.studentLName' must have class/struct/union
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(79) : error C2676: binary '[' : 'studentType' does not define this operator or a conversion to a type acceptable to the predefined operator
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(79) : error C2228: left of '.studentFName' must have class/struct/union
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(79) : error C2676: binary '[' : 'studentType' does not define this operator or a conversion to a type acceptable to the predefined operator
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(79) : error C2228: left of '.testScore' must have class/struct/union
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(79) : error C2676: binary '[' : 'studentType' does not define this operator or a conversion to a type acceptable to the predefined operator
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(79) : error C2228: left of '.grade' must have class/struct/union
 

Zenthar

Distinguished
Dec 31, 2007
250
0
18,960
As I mentioned before, you function's prototypes do not match your implementations. Your prototypes nearly perfect, but you didn't copy them properly for the implementation. Here is how your function signatures should look like:
Code:
void getData (ifstream& inFile, struct studentType sList[], int size);
void calcGrade (struct studentType sList[], int size);
//int highScore (const struct studentType sList[], int size);
void printOut (ofstream& outFile, const struct studentType sList[], int size);
 

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
Code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

const int arraysize = 20;
struct studentType
	{
		string studentFName;
		string studentLName;
		int testScore;
		char grade;
	};

void getData (ifstream& inFile, struct studentType sList[], int size);
void calcGrade (struct studentType sList[], int size);
//int highScore (const struct studentType sList[], int size);
void printOut (ofstream& outFile, const struct studentType sList[], int size);

int main() {
	
	ifstream inp;
	ofstream outp;

	inp.open("C:\\Users\\Welcome Back\\Desktop\\Ch11_Ex01Data.txt");
	outp.open("C:\\Users\\Welcome Back\\Desktop\\Ch11_Ex01Out.txt");

	int size;
	
	

	printOut(outp, sList, size);



	return 0;
}

void getData(ifstream& inp, studentType sList, int size)
{
	

	for (size = 0; size < arraysize; size++)
	{
		infile >> sList[size].studentFName
				>> sList[size].studentLName
				>> sList[size].testScore;
	}
	calcGrade(sList, size);
}

void calcGrade(studentType sList, int size)
{
	
	for (size = 0; size < arraysize; size++)
	{
	if (sList[size].testScore >= 90)
		sList.grade = 'A';
	else if (sList[size].testScore >= 80)
		sList.grade = 'B';
	else if (sList[size].testScore >= 70)
		sList.grade = 'C';
	else if (sList[size].testScore >= 60)
		sList.grade = 'D';
	else
		sList.grade = 'F';
	}
}

void printOut(ofstream& outp, studentType sList, int size)
{
	getData(inp, sList, size);

	for (size = 0; size < arraysize; size++)
	{
		outp << sList[size].studentLName << ", " << sList[size].studentFName << ", " << sList[size].testScore << ", " << sList[size].grade << endl;
	}
}

ERRORS:
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(35) : error C2065: 'sList' : undeclared identifier
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(48) : error C2065: 'infile' : undeclared identifier
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(48) : error C2676: binary '[' : 'studentType' does not define this operator or a conversion to a type acceptable to the predefined operator
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(49) : error C2228: left of '.studentFName' must have class/struct/union
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(49) : error C2676: binary '[' : 'studentType' does not define this operator or a conversion to a type acceptable to the predefined operator
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(50) : error C2228: left of '.studentLName' must have class/struct/union
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(50) : error C2676: binary '[' : 'studentType' does not define this operator or a conversion to a type acceptable to the predefined operator
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(50) : error C2228: left of '.testScore' must have class/struct/union
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(52) : error C2664: 'calcGrade' : cannot convert parameter 1 from 'studentType' to 'studentType []'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(60) : error C2676: binary '[' : 'studentType' does not define this operator or a conversion to a type acceptable to the predefined operator
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(60) : error C2228: left of '.testScore' must have class/struct/union
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(62) : error C2676: binary '[' : 'studentType' does not define this operator or a conversion to a type acceptable to the predefined operator
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(62) : error C2228: left of '.testScore' must have class/struct/union
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(64) : error C2676: binary '[' : 'studentType' does not define this operator or a conversion to a type acceptable to the predefined operator
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(64) : error C2228: left of '.testScore' must have class/struct/union
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(66) : error C2676: binary '[' : 'studentType' does not define this operator or a conversion to a type acceptable to the predefined operator
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(66) : error C2228: left of '.testScore' must have class/struct/union
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(75) : error C2065: 'inp' : undeclared identifier
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(79) : error C2676: binary '[' : 'studentType' does not define this operator or a conversion to a type acceptable to the predefined operator
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(79) : error C2228: left of '.studentLName' must have class/struct/union
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(79) : error C2676: binary '[' : 'studentType' does not define this operator or a conversion to a type acceptable to the predefined operator
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(79) : error C2228: left of '.studentFName' must have class/struct/union
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(79) : error C2676: binary '[' : 'studentType' does not define this operator or a conversion to a type acceptable to the predefined operator
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(79) : error C2228: left of '.testScore' must have class/struct/union
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(79) : error C2676: binary '[' : 'studentType' does not define this operator or a conversion to a type acceptable to the predefined operator
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(79) : error C2228: left of '.grade' must have class/struct/union


Does this have to have studentType in it like this:


for (size = 0; size < arraysize; size++)
{
if (studentType.sList[size].testScore >= 90)
sList.grade = 'A';
else if (studentType.sList[size].testScore >= 80)
sList.grade = 'B';
else if (studentType.sList[size].testScore >= 70)
sList.grade = 'C';
else if (studentType.sList[size].testScore >= 60)
sList.grade = 'D';
else
sList.grade = 'F';


 

Zenthar

Distinguished
Dec 31, 2007
250
0
18,960
No, is sList is correctly delcared as a studentType struct, you don't need it. BTW, there are still inconsistencies between your function's prototype and the implementation; just look at your latest lines 16 and 40 for example.
 

Zenthar

Distinguished
Dec 31, 2007
250
0
18,960
You need to add "struct" and also the "[]" brackets to declare them as arrays, not just as single element. The printOut for loop seems fine for the final output yes, but in printOut, you also try to send the "inp" variable to getData, but you never received it as a parameter.
 

Zenthar

Distinguished
Dec 31, 2007
250
0
18,960
I think so. C/C++ is a very demanding language syntax-wise (like many actually), there is a lot of meaning in things that can seem trivial like "&", "*", "[]", ...
 

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
Down to 1 error. YAY =D

Code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

const int arraysize = 20;
struct studentType
	{
		string studentFName;
		string studentLName;
		int testScore;
		char grade;
	};

void getData (ifstream& inFile, struct studentType sList[], int size);
void calcGrade (struct studentType sList[], int size);
//int highScore (const struct studentType sList[], int size);
void printOut (ifstream& inFile, ofstream& outFile, const struct studentType sList[], int size);

int main() {
	
	ifstream inp;
	ofstream outp;

	inp.open("C:\\Users\\Welcome Back\\Desktop\\Ch11_Ex01Data.txt");
	outp.open("C:\\Users\\Welcome Back\\Desktop\\Ch11_Ex01Out.txt");

	int size;
	
	

	printOut(inp, outp, sList, size);



	return 0;
}

void getData(ifstream& inp, struct studentType sList[], int size)
{
	

	for (size = 0; size < arraysize; size++)
	{
		inp >> sList[size].studentFName
				>> sList[size].studentLName
				>> sList[size].testScore;
	}
	calcGrade(sList, size);
}

void calcGrade(struct studentType sList[], int size)
{
	
	for (size = 0; size < arraysize; size++)
	{
	if (sList[size].testScore >= 90)
		sList[size].grade = 'A';
	else if (sList[size].testScore >= 80)
		sList[size].grade = 'B';
	else if (sList[size].testScore >= 70)
		sList[size].grade = 'C';
	else if (sList[size].testScore >= 60)
		sList[size].grade = 'D';
	else
		sList[size].grade = 'F';
	}
}

void printOut(ifstream& inp, ofstream& outp, struct studentType sList[], int size)
{
	getData(inp, sList, size);

	for (size = 0; size < arraysize; size++)
	{
		outp << sList[size].studentLName << ", " << sList[size].studentFName << ", " << sList[size].testScore << ", " << sList[size].grade << endl;
	}
}

changed all that struct and [] to the functions, also added the inFile to printOut, also found that i had sList.grade = 'A' in calcGrade function when it had to be sList[size].grade. So changed that. Also in getData I had inFile instead of what it should of been, inp so changed that too.Now just this 1 error is left.

ERROR:
1>c:\users\welcome back\documents\visual studio 2008\projects\struct\struct\structs.cpp(35) : error C2065: 'sList' : undeclared identifier



This program was more tricky because I usually do it 1 step at a time, 1 function, test it and get it working, with this one I just did everything at once which made debugging it harder. Thank you all for the help BTW! =D
 

Ijack

Distinguished
That's an easy one! The error message is telling you exactly what the error is - "sList: undeclared identifier". In C and C++ you have to declare all variables before you use them, but you haven't declared the variable sList anywhere where it will be in scope at that part of the program.

A simle declaration "struct studentType slist[20];" in the "main" function is needed.
 

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
lol i had that in there too, and removed it when all the errors were owning me. shucks

Code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

const int arraysize = 20;
struct studentType
	{
		string studentFName;
		string studentLName;
		int testScore;
		char grade;
	};

void getData (ifstream& inFile, struct studentType sList[], int size);
void calcGrade (struct studentType sList[], int size);
//int highScore (const struct studentType sList[], int size);
void printOut (ifstream& inFile, ofstream& outFile, const struct studentType sList[], int size);

int main() {
	
	ifstream inp;
	ofstream outp;

	inp.open("C:\\Users\\Welcome Back\\Desktop\\Ch11_Ex01Data.txt");
	outp.open("C:\\Users\\Welcome Back\\Desktop\\Ch11_Ex01Out.txt");

	int size = 0;
	struct studentType sList[arraysize];
	
	

	printOut(inp, outp, sList, size);



	return 0;
}

void getData(ifstream& inp, struct studentType sList[], int size)
{
	

	for (size = 0; size < arraysize; size++)
	{
		inp >> sList[size].studentFName
				>> sList[size].studentLName
				>> sList[size].testScore;
	}
	calcGrade(sList, size);
}

void calcGrade(struct studentType sList[], int size)
{
	
	for (size = 0; size < arraysize; size++)
	{
	if (sList[size].testScore >= 90)
		sList[size].grade = 'A';
	else if (sList[size].testScore >= 80)
		sList[size].grade = 'B';
	else if (sList[size].testScore >= 70)
		sList[size].grade = 'C';
	else if (sList[size].testScore >= 60)
		sList[size].grade = 'D';
	else
		sList[size].grade = 'F';
	}
}

void printOut(ifstream& inp, ofstream& outp, struct studentType sList[], int size)
{
	getData(inp, sList, size);

	for (size = 0; size < arraysize; size++)
	{
		outp << sList[size].studentLName << ", " << sList[size].studentFName << ", " << sList[size].testScore << ", " << sList[size].grade << endl;
	}
}

ERRORS:
1>structs.obj : error LNK2019: unresolved external symbol "void __cdecl printOut(class std::basic_ifstream<char,struct std::char_traits<char> > &,class std::basic_ofstream<char,struct std::char_traits<char> > &,struct studentType const * const,int)" (?printOut@@YAXAAV?$basic_ifstream@DU?$char_traits@D@std@@@std@@AAV?$basic_ofstream@DU?$char_traits@D@std@@@2@QBUstudentType@@H@Z) referenced in function _main
1>C:\Users\Welcome Back\Documents\Visual Studio 2008\Projects\struct\Debug\struct.exe : fatal error LNK1120: 1 unresolved externals
 

Ijack

Distinguished
That's another tiny error. You've declared (in line 19)
Code:
void printOut (ifstream& inFile, ofstream& outFile, const struct studentType sList[], int size);
but when you define the function (line 72) you use:
Code:
void printOut(ifstream& inp, ofstream& outp, struct studentType sList[], int size)
You've used different names for the first two parameters, which is a bit confusing but not an error. However the third parameter you define with different types:
Code:
const struct studentType slist[]
the first time, and
Code:
struct studentType slist[]
the second time. The compiler thinks they are different functions and can't find an implementation of the first one, hence the error. Remove the "const" and the program will compile (but I don't guarantee it's correct!).

It's all down to the fact that C++ lets you define functions with the same name but taking different parameters (which is called the "signature" of the function); very useful but a potential source of errors.
 

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
Nice, ok no more errors but the output to the file is very wrong.

FILE OUTPUT:
, , -858993460, F
, , -858993460, F
, , -858993460, F
, , -858993460, F
, , -858993460, F
, , -858993460, F
, , -858993460, F
, , -858993460, F
, , -858993460, F
, , -858993460, F
, , -858993460, F
, , -858993460, F
, , -858993460, F
, , -858993460, F
, , -858993460, F
, , -858993460, F
, , -858993460, F
, , -858993460, F
, , -858993460, F
, , -858993460, F

is the way I am reading the file into the array wrong in getData?