C++ records(structs)

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:




#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
33 answers Last reply
More about records structs
  1. 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.
    
    #define ARRAY_SIZE 20
    
    studentType sList[ARRAY_SIZE];
    
    for (size = 0; size < ARRAY_SIZE; size++)
    {
        // ...
    }
    
  2. 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.

    
    #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.
  3. 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.
  4. 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.
    
    
    #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
  5. 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:
    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);
    
  6. OHH okay, yea wow obv i need to include struct in there, brain fart.
  7. 
    
    
    #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.testScore >= 90)
    sList.grade = 'A';
    else if (studentType.sList.testScore >= 80)
    sList.grade = 'B';
    else if (studentType.sList.testScore >= 70)
    sList.grade = 'C';
    else if (studentType.sList.testScore >= 60)
    sList.grade = 'D';
    else
    sList.grade = 'F';
  8. Does this have to have studentType in it like this:


    for (size = 0; size < arraysize; size++)
    {
    if (studentType.sList.testScore >= 90)
    sList.grade = 'A';
    else if (studentType.sList.testScore >= 80)
    sList.grade = 'B';
    else if (studentType.sList.testScore >= 70)
    sList.grade = 'C';
    else if (studentType.sList.testScore >= 60)
    sList.grade = 'D';
    else
    sList.grade = 'F';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.
  9. Okay I see, still need to add struct to a few more functions, is the print call fine with just listing sList?
  10. laserpp said:
    Okay I see, still need to add struct to a few more functions, is the print call fine with just listing sList?
    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.
  11. Ok, so all i would have to do is just add in ifstream& inFile to printOut prototype, call, and header and that should be fine.
  12. laserpp said:
    Ok, so all i would have to do is just add in ifstream& inFile to printOut prototype, call, and header and that should be fine.
    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 "&", "*", "[]", ...
  13. Okay when I get out of class and home, going to make these changes and see what errors I get and will re-post.
  14. Down to 1 error. YAY =D

    
    #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.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
  15. 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.
  16. lol i had that in there too, and removed it when all the errors were owning me. shucks

    
    
    #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
  17. That's another tiny error. You've declared (in line 19)
    void printOut (ifstream& inFile, ofstream& outFile, const struct studentType sList[], int size);

    but when you define the function (line 72) you use:
    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:
    const struct studentType slist[]

    the first time, and
    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.
  18. 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?
  19. Try single-stepping through the getData and printOut functions and watch what's happening.
  20. I guess the getData function is wrong, I'm more of a C than C++ programmer so I'm not too familiar with ifstream; can you give us a sample of the input file?
  21. 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
  22. 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.
  23. 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
     if (inp.good()) cout << "OK";
     else cout << "Oops!";
    after you open the file.
  24. 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");
  25. 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.
  26. I moved the file, and now it works, but I have done a different program that I had it just on my desktop and it worked, wonder why this one didn't.
  27. 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!
  28. 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.
  29. laserpp said:
    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.
    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.
  30. lol nice didn't know it had a name, just something I thought was smart to do. =D
  31. 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.
  32. There is also the "sandwich" programming, where you actually combine both top-down and bottom-up; not necessarily useful for small teams/projects, but very good for larger ones.
Ask a new question

Read More

Programming Apps