C++ records(structs)

Page 2 - Seeking answers? Join the Tom's Guide community: where nearly two million members share solutions and discuss the latest tech.

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
 

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
Ducky Donald 85
Goof Goofy 89
Brave Balto 93
Snow White 93
Alice Wonderful 89
Sleepy Beauty 85
Simba Lion 95
Dumbo Elephant 90
Brown Deer 86
Johny Jocky 95

Grump Grumpy 75
Hap Happy 80
Dop Dopey 80
Bash Bashful 85
Sleep Sleepy 70
Sneez Sneezey 83
Shelly Malik 95
Chelsea Tomek 95
Angela Clodfelt 95
Allison Nields 95
 

Zenthar

Distinguished
Dec 31, 2007
250
0
18,960
Not sure what is wrong here, you might want to try using getline to read from the file and then a istringstream to "parse it". You can take a look here to get an idea on how to do that; your implementation will be different of course (as you are not reading from a CSV file.
 

Ijack

Distinguished
With that input file the program works fine for me. That means, most likely, that you're not opening the input file properly. Are you sure that the path is correct and that you have authority to read that file?

You can check whether the file opened OK by adding this code
Code:
 if (inp.good()) cout << "OK";
 else cout << "Oops!";
after you open the file.
 

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
yup it is saying oops

this is the path when I go to props: C:\Users\Welcome Back\Desktop

this is what I have in program: inp.open("C:\\Users\\Welcome Back\\Desktop\\Ch11_Ex01Data.txt");
 

Ijack

Distinguished
Try putting the file somewhere else, say C:\, and check the security on it. (Although, presumably, you can open it in notepad in which case you have the appropriate rights.) I wonder if the space in the path is causing a problem, but I don't know why it should.
 

Ijack

Distinguished
One of life's little mysteries. There must be some reason why you couldn't open that file, but without actually seeing your computer it's difficult to guess why.

But, because the program failed, you have learnt a valuable lesson. When writing real programs you should never assume that an operation such as opening a file has succeeded. In real life you would write various tests into the program at every point where a failure could occur. C++ provides very powerful facilities to allow error checking such as exceptions and try/catch blocks. But I suspect that is a little beyond your current level and is something you will learn about in due course.

Often you learn more from failure than success!
 

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
Yea, haven't learned much about catching failures, but that is why I usually write in bits, 1 function at a time and run it so if an error I know its in that 1 function instead of figuring out which of the like 5 functions it could be in. I agree, once you fail and figure out why you learn WAY more than getting it right the first time.
 

Zenthar

Distinguished
Dec 31, 2007
250
0
18,960
There is actually a name for that, it's called bottom-up layered integration. Basically you start with the bottom, you test that, then add "upper layers of functionality" then test that and so on. Testing everything only when all is done is called big-bang integration and it's not considered a good practice ;). With time, the size of the "blocks" you code before testing will increase, but the concept is the same.
 

Ijack

Distinguished
The alternative is top-down programming where you construct the framework of the program with just stubs for the functions then gradually refine them. Both ways have their advantages and their adherents. Top-down programming is particularly adaptable to object-oriented programming.