Sign in with
Sign up | Sign in
Your question

C++ records(structs)

Last response: in Applications
Share
December 5, 2010 8:20:48 AM

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:

  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string>
  5. using namespace std;
  6.  
  7. struct studentType
  8. {
  9. string studentFName;
  10. string studentLName;
  11. int testScore;
  12. char grade;
  13. };
  14.  
  15. void getData (ifstream& inFile, studentType sList[], int size);
  16. void calcGrade (studentType sList[], int size);
  17. //int highScore (const studentType sList[], int size);
  18. void printOut (ofstream& outFile, const studentType sList[], int size);
  19.  
  20. int main() {
  21.  
  22. ifstream inp;
  23. ofstream outp;
  24.  
  25. inp.open("C:\\Users\\Welcome Back\\Desktop\\Ch11_Ex01Data.txt");
  26. outp.open("C:\\Users\\Welcome Back\\Desktop\\Ch11_Ex01Out.txt");
  27.  
  28. studentType sList[20];
  29. int size;
  30.  
  31.  
  32.  
  33. void printOut(outp, sList, size);
  34.  
  35.  
  36.  
  37. return 0;
  38. }
  39.  
  40. void getData(ifstream& inp, studentType& sList, int size)
  41. {
  42. struct studentType sList[20]
  43. {
  44. string studentFName;
  45. string studentLName;
  46. int testScore;
  47. char grade;
  48. }
  49.  
  50. for (size = 0; size < 20; size++)
  51. {
  52. infile >> sList[size].studentFName
  53. >> sList[size].studentLName
  54. >> sList[size].testScore;
  55. }
  56. calcGrade(sList, size);
  57. }
  58.  
  59. void calcGrade(studentType& sList, int size)
  60. {
  61.  
  62. for (size = 0; size < 20; size++)
  63. {
  64. if (sList[size].testScore >= 90)
  65. sList.grade = 'A';
  66. else if (sList[size].testScore >= 80)
  67. sList.grade = 'B';
  68. else if (sList[size].testScore >= 70)
  69. sList.grade = 'C';
  70. else if (sList[size].testScore >= 60)
  71. sList.grade = 'D';
  72. else
  73. sList.grade = 'F';
  74. }
  75. }
  76.  
  77. void printOut(ofstream& outp, studentType& sList, int size)
  78. {
  79. getData(inp, sList, size);
  80.  
  81. for (size = 0; size <20; size++)
  82. {
  83. outp << sList[size].studentLName << ", " << sList[size].studentFName << ", " << sList[size].testScore << ", " << sList[size].grade << endl;
  84. }
  85. }


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

More about : records structs

a b L Programming
December 5, 2010 4:50:55 PM

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.
  1. #define ARRAY_SIZE 20
  2.  
  3. studentType sList[ARRAY_SIZE];
  4.  
  5. for (size = 0; size < ARRAY_SIZE; size++)
  6. {
  7. // ...
  8. }
m
0
l
Related resources
December 6, 2010 8:36:18 AM

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.

  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string>
  5. using namespace std;
  6.  
  7. const int arraysize = 20;
  8. struct studentType sList[arraysize]
  9. {
  10. string studentFName;
  11. string studentLName;
  12. int testScore;
  13. char grade;
  14. };
  15.  
  16. void getData (ifstream& inFile, studentType sList[], int size);
  17. void calcGrade (studentType sList[], int size);
  18. //int highScore (const studentType sList[], int size);
  19. void printOut (ofstream& outFile, const studentType sList[], int size);
  20.  
  21. int main() {
  22.  
  23. ifstream inp;
  24. ofstream outp;
  25.  
  26. inp.open("C:\\Users\\Welcome Back\\Desktop\\Ch11_Ex01Data.txt");
  27. outp.open("C:\\Users\\Welcome Back\\Desktop\\Ch11_Ex01Out.txt");
  28.  
  29. int size;
  30.  
  31.  
  32.  
  33. void printOut(outp, sList, size);
  34.  
  35.  
  36.  
  37. return 0;
  38. }
  39.  
  40. void getData(ifstream& inp, studentType sList, int size)
  41. {
  42. struct studentType sList[arraysize]
  43. {
  44. string studentFName;
  45. string studentLName;
  46. int testScore;
  47. char grade;
  48. }
  49.  
  50. for (size = 0; size < arraysize; size++)
  51. {
  52. infile >> sList[size].studentFName
  53. >> sList[size].studentLName
  54. >> sList[size].testScore;
  55. }
  56. calcGrade(sList, size);
  57. }
  58.  
  59. void calcGrade(studentType sList, int size)
  60. {
  61.  
  62. for (size = 0; size < arraysize; size++)
  63. {
  64. if (sList[size].testScore >= 90)
  65. sList.grade = 'A';
  66. else if (sList[size].testScore >= 80)
  67. sList.grade = 'B';
  68. else if (sList[size].testScore >= 70)
  69. sList.grade = 'C';
  70. else if (sList[size].testScore >= 60)
  71. sList.grade = 'D';
  72. else
  73. sList.grade = 'F';
  74. }
  75. }
  76.  
  77. void printOut(ofstream& outp, studentType sList, int size)
  78. {
  79. getData(inp, sList, size);
  80.  
  81. for (size = 0; size < arraysize; size++)
  82. {
  83. outp << sList[size].studentLName << ", " << sList[size].studentFName << ", " << sList[size].testScore << ", " << sList[size].grade << endl;
  84. }
  85. }


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.
m
0
l
a b L Programming
December 6, 2010 8:46:18 AM

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.
m
0
l
December 6, 2010 10:06:24 AM

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.
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string>
  5. using namespace std;
  6.  
  7. const int arraysize = 20;
  8. struct studentType
  9. {
  10. string studentFName;
  11. string studentLName;
  12. int testScore;
  13. char grade;
  14. };
  15.  
  16. void getData (ifstream& inFile, studentType sList[], int size);
  17. void calcGrade (studentType sList[], int size);
  18. //int highScore (const studentType sList[], int size);
  19. void printOut (ofstream& outFile, const studentType sList[], int size);
  20.  
  21. int main() {
  22.  
  23. ifstream inp;
  24. ofstream outp;
  25.  
  26. inp.open("C:\\Users\\Welcome Back\\Desktop\\Ch11_Ex01Data.txt");
  27. outp.open("C:\\Users\\Welcome Back\\Desktop\\Ch11_Ex01Out.txt");
  28.  
  29. int size;
  30.  
  31.  
  32.  
  33. printOut(outp, sList, size);
  34.  
  35.  
  36.  
  37. return 0;
  38. }
  39.  
  40. void getData(ifstream& inp, studentType sList, int size)
  41. {
  42.  
  43.  
  44. for (size = 0; size < arraysize; size++)
  45. {
  46. infile >> sList[size].studentFName
  47. >> sList[size].studentLName
  48. >> sList[size].testScore;
  49. }
  50. calcGrade(sList, size);
  51. }
  52.  
  53. void calcGrade(studentType sList, int size)
  54. {
  55.  
  56. for (size = 0; size < arraysize; size++)
  57. {
  58. if (sList[size].testScore >= 90)
  59. sList.grade = 'A';
  60. else if (sList[size].testScore >= 80)
  61. sList.grade = 'B';
  62. else if (sList[size].testScore >= 70)
  63. sList.grade = 'C';
  64. else if (sList[size].testScore >= 60)
  65. sList.grade = 'D';
  66. else
  67. sList.grade = 'F';
  68. }
  69. }
  70.  
  71. void printOut(ofstream& outp, studentType sList, int size)
  72. {
  73. getData(inp, sList, size);
  74.  
  75. for (size = 0; size < arraysize; size++)
  76. {
  77. outp << sList[size].studentLName << ", " << sList[size].studentFName << ", " << sList[size].testScore << ", " << sList[size].grade << endl;
  78. }
  79. }


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
m
0
l
a b L Programming
December 6, 2010 11:22:50 AM

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:
  1. void getData (ifstream& inFile, struct studentType sList[], int size);
  2. void calcGrade (struct studentType sList[], int size);
  3. //int highScore (const struct studentType sList[], int size);
  4. void printOut (ofstream& outFile, const struct studentType sList[], int size);
m
0
l
December 6, 2010 11:30:47 AM

OHH okay, yea wow obv i need to include struct in there, brain fart.
m
0
l
December 6, 2010 11:35:05 AM

  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string>
  5. using namespace std;
  6.  
  7. const int arraysize = 20;
  8. struct studentType
  9. {
  10. string studentFName;
  11. string studentLName;
  12. int testScore;
  13. char grade;
  14. };
  15.  
  16. void getData (ifstream& inFile, struct studentType sList[], int size);
  17. void calcGrade (struct studentType sList[], int size);
  18. //int highScore (const struct studentType sList[], int size);
  19. void printOut (ofstream& outFile, const struct studentType sList[], int size);
  20.  
  21. int main() {
  22.  
  23. ifstream inp;
  24. ofstream outp;
  25.  
  26. inp.open("C:\\Users\\Welcome Back\\Desktop\\Ch11_Ex01Data.txt");
  27. outp.open("C:\\Users\\Welcome Back\\Desktop\\Ch11_Ex01Out.txt");
  28.  
  29. int size;
  30.  
  31.  
  32.  
  33. printOut(outp, sList, size);
  34.  
  35.  
  36.  
  37. return 0;
  38. }
  39.  
  40. void getData(ifstream& inp, studentType sList, int size)
  41. {
  42.  
  43.  
  44. for (size = 0; size < arraysize; size++)
  45. {
  46. infile >> sList[size].studentFName
  47. >> sList[size].studentLName
  48. >> sList[size].testScore;
  49. }
  50. calcGrade(sList, size);
  51. }
  52.  
  53. void calcGrade(studentType sList, int size)
  54. {
  55.  
  56. for (size = 0; size < arraysize; size++)
  57. {
  58. if (sList[size].testScore >= 90)
  59. sList.grade = 'A';
  60. else if (sList[size].testScore >= 80)
  61. sList.grade = 'B';
  62. else if (sList[size].testScore >= 70)
  63. sList.grade = 'C';
  64. else if (sList[size].testScore >= 60)
  65. sList.grade = 'D';
  66. else
  67. sList.grade = 'F';
  68. }
  69. }
  70.  
  71. void printOut(ofstream& outp, studentType sList, int size)
  72. {
  73. getData(inp, sList, size);
  74.  
  75. for (size = 0; size < arraysize; size++)
  76. {
  77. outp << sList[size].studentLName << ", " << sList[size].studentFName << ", " << sList[size].testScore << ", " << sList[size].grade << endl;
  78. }
  79. }


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';


m
0
l
a b L Programming
December 6, 2010 11:53:44 AM

laserpp said:
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.
m
0
l
December 6, 2010 12:35:45 PM

Okay I see, still need to add struct to a few more functions, is the print call fine with just listing sList?
m
0
l
a b L Programming
December 6, 2010 1:19:29 PM

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.
m
0
l
December 6, 2010 1:49:05 PM

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.
m
0
l
a b L Programming
December 6, 2010 2:35:48 PM

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 "&", "*", "[]", ...
m
0
l
December 6, 2010 2:41:26 PM

Okay when I get out of class and home, going to make these changes and see what errors I get and will re-post.
m
0
l
December 7, 2010 7:30:41 AM

Down to 1 error. YAY =D

  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string>
  5. using namespace std;
  6.  
  7. const int arraysize = 20;
  8. struct studentType
  9. {
  10. string studentFName;
  11. string studentLName;
  12. int testScore;
  13. char grade;
  14. };
  15.  
  16. void getData (ifstream& inFile, struct studentType sList[], int size);
  17. void calcGrade (struct studentType sList[], int size);
  18. //int highScore (const struct studentType sList[], int size);
  19. void printOut (ifstream& inFile, ofstream& outFile, const struct studentType sList[], int size);
  20.  
  21. int main() {
  22.  
  23. ifstream inp;
  24. ofstream outp;
  25.  
  26. inp.open("C:\\Users\\Welcome Back\\Desktop\\Ch11_Ex01Data.txt");
  27. outp.open("C:\\Users\\Welcome Back\\Desktop\\Ch11_Ex01Out.txt");
  28.  
  29. int size;
  30.  
  31.  
  32.  
  33. printOut(inp, outp, sList, size);
  34.  
  35.  
  36.  
  37. return 0;
  38. }
  39.  
  40. void getData(ifstream& inp, struct studentType sList[], int size)
  41. {
  42.  
  43.  
  44. for (size = 0; size < arraysize; size++)
  45. {
  46. inp >> sList[size].studentFName
  47. >> sList[size].studentLName
  48. >> sList[size].testScore;
  49. }
  50. calcGrade(sList, size);
  51. }
  52.  
  53. void calcGrade(struct studentType sList[], int size)
  54. {
  55.  
  56. for (size = 0; size < arraysize; size++)
  57. {
  58. if (sList[size].testScore >= 90)
  59. sList[size].grade = 'A';
  60. else if (sList[size].testScore >= 80)
  61. sList[size].grade = 'B';
  62. else if (sList[size].testScore >= 70)
  63. sList[size].grade = 'C';
  64. else if (sList[size].testScore >= 60)
  65. sList[size].grade = 'D';
  66. else
  67. sList[size].grade = 'F';
  68. }
  69. }
  70.  
  71. void printOut(ifstream& inp, ofstream& outp, struct studentType sList[], int size)
  72. {
  73. getData(inp, sList, size);
  74.  
  75. for (size = 0; size < arraysize; size++)
  76. {
  77. outp << sList[size].studentLName << ", " << sList[size].studentFName << ", " << sList[size].testScore << ", " << sList[size].grade << endl;
  78. }
  79. }


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
m
0
l
a b L Programming
December 7, 2010 9:18:55 AM

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.
m
0
l
December 7, 2010 9:33:22 AM

lol i had that in there too, and removed it when all the errors were owning me. shucks

  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string>
  5. using namespace std;
  6.  
  7. const int arraysize = 20;
  8. struct studentType
  9. {
  10. string studentFName;
  11. string studentLName;
  12. int testScore;
  13. char grade;
  14. };
  15.  
  16. void getData (ifstream& inFile, struct studentType sList[], int size);
  17. void calcGrade (struct studentType sList[], int size);
  18. //int highScore (const struct studentType sList[], int size);
  19. void printOut (ifstream& inFile, ofstream& outFile, const struct studentType sList[], int size);
  20.  
  21. int main() {
  22.  
  23. ifstream inp;
  24. ofstream outp;
  25.  
  26. inp.open("C:\\Users\\Welcome Back\\Desktop\\Ch11_Ex01Data.txt");
  27. outp.open("C:\\Users\\Welcome Back\\Desktop\\Ch11_Ex01Out.txt");
  28.  
  29. int size = 0;
  30. struct studentType sList[arraysize];
  31.  
  32.  
  33.  
  34. printOut(inp, outp, sList, size);
  35.  
  36.  
  37.  
  38. return 0;
  39. }
  40.  
  41. void getData(ifstream& inp, struct studentType sList[], int size)
  42. {
  43.  
  44.  
  45. for (size = 0; size < arraysize; size++)
  46. {
  47. inp >> sList[size].studentFName
  48. >> sList[size].studentLName
  49. >> sList[size].testScore;
  50. }
  51. calcGrade(sList, size);
  52. }
  53.  
  54. void calcGrade(struct studentType sList[], int size)
  55. {
  56.  
  57. for (size = 0; size < arraysize; size++)
  58. {
  59. if (sList[size].testScore >= 90)
  60. sList[size].grade = 'A';
  61. else if (sList[size].testScore >= 80)
  62. sList[size].grade = 'B';
  63. else if (sList[size].testScore >= 70)
  64. sList[size].grade = 'C';
  65. else if (sList[size].testScore >= 60)
  66. sList[size].grade = 'D';
  67. else
  68. sList[size].grade = 'F';
  69. }
  70. }
  71.  
  72. void printOut(ifstream& inp, ofstream& outp, struct studentType sList[], int size)
  73. {
  74. getData(inp, sList, size);
  75.  
  76. for (size = 0; size < arraysize; size++)
  77. {
  78. outp << sList[size].studentLName << ", " << sList[size].studentFName << ", " << sList[size].testScore << ", " << sList[size].grade << endl;
  79. }
  80. }


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
m
0
l
a b L Programming
December 7, 2010 10:07:07 AM

That's another tiny error. You've declared (in line 19)
  1. void printOut (ifstream& inFile, ofstream& outFile, const struct studentType sList[], int size);

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

the first time, and
  1. 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.
m
0
l
December 7, 2010 10:29:49 AM

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?
m
0
l
a b L Programming
December 7, 2010 11:07:10 AM

Try single-stepping through the getData and printOut functions and watch what's happening.
m
0
l
a b L Programming
December 7, 2010 11:09:39 AM

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?
m
0
l
December 7, 2010 11:23:33 AM

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
m
0
l
a b L Programming
December 7, 2010 1:07:14 PM

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.
m
0
l
a b L Programming
December 7, 2010 1:28:25 PM

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
  1. if (inp.good()) cout << "OK";
  2. else cout << "Oops!";
after you open the file.
m
0
l
December 7, 2010 11:21:14 PM

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");
m
0
l
a b L Programming
December 8, 2010 6:02:04 AM

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.
m
0
l
December 8, 2010 7:27:59 AM

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.
m
0
l
a b L Programming
December 8, 2010 8:28:37 AM

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!
m
0
l
December 8, 2010 11:26:15 AM

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.
m
0
l
a b L Programming
December 8, 2010 11:46:10 AM

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.
m
0
l
December 8, 2010 1:07:57 PM

lol nice didn't know it had a name, just something I thought was smart to do. =D
m
0
l
a b L Programming
December 8, 2010 1:53:46 PM

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.
m
0
l
a b L Programming
December 8, 2010 2:18:22 PM

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.
m
0
l
!