943,866 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 18705
  • C++ RSS
Apr 20th, 2005
0

struct array and enum problems

Expand Post »
Our program got cut down from a .h and 2 .cpps to just one .h and one .cpp.... so trying to convert fucntions back to my .h I received tons of errors. Hopefully someone can help me get rid of most of these? please? I need to turn in the final program in like 23 hours. Thanks if you can help or if you will. I am not asking anyone to do it... just steer me in the right direction if you see where i am going wrong.... My program is nasty. :cry:

Here is the main thing on my lab that I am working on with it too.... for where the structs and arrays come in:
An array of 10 structures of type aClass will make up the storage mechanism; i.e., you will process 10 students.

Heres my .h:

C++ Syntax (Toggle Plain Text)
  1. #include<iostream> // all the classes I could ever need for this program
  2. #include<iomanip> // and a few extra just incase.
  3. #include<cmath>
  4. #include<cstdlib>
  5. #include<string>
  6. using namespace std;
  7.  
  8. enum aGrade {a, b, c, d, f, w, i};
  9.  
  10. struct aClass
  11. {
  12. string courses;
  13. aGrade grades;
  14. int hours;
  15. float GP;
  16. };
  17.  
  18. aClass mycourses;
  19.  
  20. void printHead () // this section will display the heading for the output table
  21. {
  22. cout<<"\n\nList of the Courses"<<endl<<"\n Class Hours Grade \n";
  23. cout<<"-----------------------------------"<<endl;
  24. }
  25.  
  26. aGrade convert(enum aGrade in, //OUT: in
  27. aClass mycourses) //IN: grades
  28.  
  29. {
  30. switch (mycourses.grades) //switch stmt to get all the letters into the enum catagories
  31. {
  32. case 'a':case 'A':
  33. in = a;
  34. break;
  35. case 'b':case 'B':
  36. in = b;
  37. break;
  38. case 'c':case 'C':
  39. in = c;
  40. break;
  41. case 'd':case 'D':
  42. in = d;
  43. break;
  44. case 'f':case 'F':
  45. in = f;
  46. break;
  47. case 'w':case 'W':
  48. in = w;
  49. break;
  50. case 'i':case 'I':
  51. in = i;
  52. break;
  53.  
  54. }
  55.  
  56. return in;
  57. }
  58.  
  59. int calcGradePoint(enum aGrade in,
  60. aClass mycourses) //This function is used to calculate the totalgpa for one class
  61.  
  62. {
  63. if(in == a)
  64. return 4 * mycourses.hours;
  65. if(in == b)
  66. return 3 * mycourses.hours;
  67. if(in == c)
  68. return 2 * mycourses.hours;
  69. if(in == d)
  70. return 1 * mycourses.hours;
  71. if(in == f)
  72. return 0 * mycourses.hours;
  73. if(in == w)
  74. return 0 * mycourses.hours;
  75. if(in == i)
  76. return 0 * mycourses.hours;
  77.  
  78. }
  79.  
  80.  
  81. void sortClass (aClass mycourses) //IN: cnt
  82. //This bubblesort is used to put the courses into alphabetical order
  83.  
  84. {
  85. int i = cnt, j = 0, k = 1, temper;
  86. string temp;
  87. char tmp;
  88.  
  89. while (i >= 0 && k)
  90. {
  91.  
  92. k = 0;
  93. for (j = 0; j <= i; j++)
  94. if (mycourses.courses[j] > mycourses.courses[j+1])
  95. {
  96. temp = mycourses.courses[j];
  97. mycourses.courses[j] = mycourses.courses[j+1];
  98. mycourses.courses[j+1] = temp;
  99.  
  100. temper = mycourses.hours[j];
  101. mycourses.hours[j] = mycourses.hours[j+1];
  102. mycourses.hours[j+1] = temper;
  103.  
  104. tmp = mycourses.grades[j];
  105. mycourses.grades[j] = mycourses.grades[j+1];
  106. mycourses.grades[j+1] = tmp;
  107.  
  108. k = 1;
  109. }
  110. i--;
  111. }
  112. }
  113.  
  114.  
  115.  
  116. float calcGPA(const float gpaSum, //IN: gpaSum
  117. const float totalHours ) //IN: totalHours
  118. //This function is used to calculate overall gpa = totalofgpa / totalhours
  119.  
  120. {
  121. float gpa;
  122. gpa = float (gpaSum / totalHours);
  123. return gpa;
  124. }
  125.  
  126.  
  127. void printLine (cnt, b, aClass mycourses, gpa, totalHour)
  128. {
  129. for (int b = 1; b <= cnt; b++) //puts the information into the table
  130. {
  131. cout<<setw(12)<<mycourses.courses<<setw(9)<<mycourses.hours<<setw(10)<<mycourses.grades<<endl;
  132. }
  133.  
  134. cout<<"\nThe total GPA is "<<gpa<<" and you attempted "<<totalHours<<" hours."<<endl;
  135. }

and here is my .cpp


C++ Syntax (Toggle Plain Text)
  1. #include "schedule.h"
  2.  
  3.  
  4. int main(){ //Calls main function
  5.  
  6. int cnt = 0; //Variables used
  7. char gpaForOneClass;
  8. float gpa = 0.0, totalHours = 0.0, gpaSum = 0.0;
  9. aGrade in;
  10.  
  11.  
  12. while (cnt < 10)
  13. {
  14. while (1)
  15. {
  16. cout<<"\nEnter a course, the letter grade received, and the credit hours. "<<endl;
  17. cin >> mycourses.courses;
  18.  
  19. if (mycourses.courses == "quit" || mycourses.courses == "QUIT" || mycourses.courses == "Quit") break; //ends input when quit or QUIT is typed
  20.  
  21. cin >> mycourses.grades;
  22. cin >> mycourses.hours;
  23.  
  24. in = convert(in, aClass mycourses); //converts letter input into right value
  25. gpaForOneClass = calcGradePoint(in, aClass mycourses, cnt); //gets gpa total for one class
  26. gpaSum += gpaForOneClass; //totals up gpa
  27. totalHours = totalHours + mycourses.hours; //totals up hours
  28. cnt = cnt + 1;
  29.  
  30. }
  31.  
  32. }
  33.  
  34. gpa = calcGPA(gpaSum, totalHours); //calls gpa function
  35. printHead(); //calls header function to display table
  36. sortClass(aClass mycourses, cnt); // calls bsort function
  37. printLine(cnt, b, aClass mycourses, gpa, totalHours);
  38.  
  39.  
  40.  
  41. cin>>cnt;
  42.  
  43. return 0;
  44. }

and here are the ridiculous amount errors i get:

C++ Syntax (Toggle Plain Text)
  1. 15 C:\Documents and Settings\Josh\Desktop\Lab5\cs110lab5.cpp In file included from C:/Documents and Settings/Josh/Desktop/Lab5/cs110lab5.cpp
  2. C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h In function `void sortClass(aClass)':
  3. 87 C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h `cnt' undeclared (first use this function)
  4. 87 C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h (Each undeclared identifier is reported only once for each function it appears
  5. 100 C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h cannot convert `std::string' to `char' in assignment
  6.  
  7. 102 C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h invalid types `int[int]' for array subscript
  8. 103 C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h invalid types `int[int]' for array subscript
  9. 103 C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h invalid types `int[int]' for array subscript
  10. 104 C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h invalid types `int[int]' for array subscript
  11. 106 C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h invalid types `aGrade[int]' for array subscript
  12. 107 C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h invalid types `aGrade[int]' for array subscript
  13. 107 C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h invalid types `aGrade[int]' for array subscript
  14. 108 C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h invalid types `aGrade[int]' for array subscript
  15.  
  16. /Documents and Settings/Josh/Desktop/Lab5/schedule.h C:\Documents and Settings\Josh\Desktop\Lab5\C At global scope:
  17. 129 C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h syntax error before `,' token
  18. C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h In function `void printLine(...)':
  19. 136 C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h `gpa' undeclared (first use this function)
  20. 136 C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h `totalHours' undeclared (first use this function)
  21. C:\Documents and Settings\Josh\Desktop\Lab5\cs110lab5.cpp In function `int main()':
  22.  
  23. 35 C:\Documents and Settings\Josh\Desktop\Lab5\cs110lab5.cpp no match
  24. for 'operator>>' in 'std::cin >> mycourses.aClass::grades'
  25. error C:\Dev-Cpp\include\c++\3.3.1\bits\istream.tcc:83 candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT,
  26. 92 C:\Dev-Cpp\include\c++\3.3.1\bits\istream.tcc std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT,
  27. 101 C:\Dev-Cpp\include\c++\3.3.1\bits\istream.tcc std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT,
  28. That same error.... 19 more times then..
  29.  
  30. 74 C:\Dev-Cpp\include\c++\3.3.1\iomanip std::basic_istream<_CharT, _Traits>&
  31. that same error....4 more times
  32.  
  33. 38 C:\Documents and Settings\Josh\Desktop\Lab5\cs110lab5.cpp syntax error before `)' token
  34. 39 C:\Documents and Settings\Josh\Desktop\Lab5\cs110lab5.cpp syntax error before `,' token
  35. 50 C:\Documents and Settings\Josh\Desktop\Lab5\cs110lab5.cpp syntax error before `,' token
  36. 51 C:\Documents and Settings\Josh\Desktop\Lab5\cs110lab5.cpp syntax error before `,' token
  37.  
Last edited by BountyX; Apr 20th, 2005 at 7:46 pm. Reason: Code Tags
Similar Threads
Reputation Points: 10
Solved Threads: 3
Junior Poster
jhdobbins is offline Offline
105 posts
since Apr 2005
Apr 20th, 2005
0

Re: struct array and enum problems

Some of the variables being used in the .h file are declared in the .cpp file, which is after you include the info from the .h file.

I reccomend making a schedule class. The code, as it is now, seems unorganzied and making a class would tidy things up a bit. It would also fix most of your problems since they are related to using undeclared variables from the .h file.

Schedule.h can be a class declaration while Schedule.cpp can be the implementation. Have a seperate file (main.cpp?) that contains the main function and implements the class.
Reputation Points: 28
Solved Threads: 9
Posting Whiz in Training
BountyX is offline Offline
222 posts
since Mar 2004
Apr 20th, 2005
0

Re: struct array and enum problems

Quote originally posted by BountyX ...
I reccomend making a schedule class. The code, as it is now, seems unorganzied and making a class would tidy things up a bit. It would also fix most of your problems since they are related to using undeclared variables from the .h file.
Well, originally I had a schedule class. I thought that was how it was supposed to be turned in (a schedule class, a schedule.cpp, and the main program .cpp) I went to lab today and was informed that "thats harder to do it that way and we are saving classes for lab 6". I had spent about 8 hours working on the program prior to this with defining the class, getting all the function variables lined up, etc. I was told if I did that, points would be taken away because they didnt want to look thru extra code if they didnt have to... Therefore I was trying to edit my program back to just the header file and the main.cpp file.... I agree its unorganized and needs major cleanup work. Thanks for your help.
Reputation Points: 10
Solved Threads: 3
Junior Poster
jhdobbins is offline Offline
105 posts
since Apr 2005
Apr 21st, 2005
0

Re: struct array and enum problems

here's my header file... (we are supposed to include our functions here this file... i know its ugly.. but its at the professors/Teaching Assistants discretion.)


C++ Syntax (Toggle Plain Text)
  1. #include<iostream> // all the classes I could ever need for this program
  2. #include<iomanip> // and a few extra just incase.
  3. #include<cmath>
  4. #include<cstdlib>
  5. #include<string>
  6. using namespace std;
  7.  
  8. enum aGrade {a, b, c, d, f, w, i};
  9.  
  10. struct aClass
  11. {
  12. string courses;
  13. aGrade grades;
  14. int hours;
  15. float GP;
  16. };
  17.  
  18. int cnt = 0; //Variables used
  19. char gpaForOneClass;
  20. float gpa = 0.0, totalHours = 0.0, gpaSum = 0.0;
  21. aGrade in;
  22.  
  23. aClass mycourses[10];
  24.  
  25. void printHead () // this section will display the heading for the output table
  26. {
  27. cout<<"\n\nList of the Courses"<<endl<<"\n Class Hours Grade \n";
  28. cout<<"-----------------------------------"<<endl;
  29. }
  30.  
  31. aGrade convert(enum aGrade in, int cnt, //OUT: in
  32. aClass mycourses[]) //IN: grades
  33.  
  34. {
  35. switch (mycourses[cnt].grades) //switch stmt to get all the letters into the enum catagories
  36. {
  37. case 'a':case 'A':
  38. in = a;
  39. break;
  40. case 'b':case 'B':
  41. in = b;
  42. break;
  43. case 'c':case 'C':
  44. in = c;
  45. break;
  46. case 'd':case 'D':
  47. in = d;
  48. break;
  49. case 'f':case 'F':
  50. in = f;
  51. break;
  52. case 'w':case 'W':
  53. in = w;
  54. break;
  55. case 'i':case 'I':
  56. in = i;
  57. break;
  58.  
  59. }
  60.  
  61. return in;
  62. }
  63.  
  64. int calcGradePoint(enum aGrade in,
  65. aClass mycourses[]) //This function is used to calculate the totalgpa for one class
  66.  
  67. {
  68. if(in == a)
  69. return 4 * mycourses[cnt].hours;
  70. if(in == b)
  71. return 3 * mycourses[cnt].hours;
  72. if(in == c)
  73. return 2 * mycourses[cnt].hours;
  74. if(in == d)
  75. return 1 * mycourses[cnt].hours;
  76. if(in == f)
  77. return 0 * mycourses[cnt].hours;
  78. if(in == w)
  79. return 0 * mycourses[cnt].hours;
  80. if(in == i)
  81. return 0 * mycourses[cnt].hours;
  82.  
  83. }
  84.  
  85.  
  86. void sortClass (aClass mycourses[], int cnt) //IN: cnt
  87. //This bubblesort is used to put the courses into alphabetical order
  88.  
  89. {
  90. int i = cnt, j = 0, k = 1, temper;
  91. string temp;
  92. char tmp;
  93.  
  94. while (i >= 0 && k)
  95. {
  96.  
  97. k = 0;
  98. for (j = 0; j <= i; j++)
  99. if (mycourses[j].courses > mycourses[j+1].courses)
  100. {
  101. temp = mycourses[j].courses;
  102. mycourses[j].courses = mycourses[j+1].courses;
  103. mycourses[j+1].courses = temp;
  104.  
  105. temper = mycourses[j].hours;
  106. mycourses[j].hours = mycourses[j+1].hours;
  107. mycourses[j+1].hours = temper;
  108.  
  109. tmp = mycourses[j].grades;
  110. mycourses[j].grades = mycourses[j+1].grades;
  111. mycourses[j+1].grades = tmp;
  112.  
  113. k = 1;
  114. }
  115. i--;
  116. }
  117. }
  118.  
  119.  
  120.  
  121. float calcGPA(const float gpaSum, //IN: gpaSum
  122. const float totalHours ) //IN: totalHours
  123. //This function is used to calculate overall gpa = totalofgpa / totalhours
  124.  
  125. {
  126. float gpa;
  127. gpa = float (gpaSum / totalHours);
  128. return gpa;
  129. }
  130.  
  131.  
  132. void printLine (cnt, b, aClass mycourses[], gpa, totalHour)
  133. {
  134. for (int b = 1; b <= cnt; b++) //puts the information into the table
  135. {
  136. cout<<setw(12)<<mycourses[b].courses<<setw(9)<<mycourses[b].hours<<setw(10)<<mycourses[b].grades<<endl;
  137. }
  138.  
  139. cout<<"\nThe total GPA is "<<gpa<<" and you attempted "<<totalHours<<" hours."<<endl;
  140. }


and here is my main program... .cpp

C++ Syntax (Toggle Plain Text)
  1. #include "schedule.h"
  2.  
  3.  
  4. int main(){ //Calls main function
  5.  
  6.  
  7.  
  8. while (cnt < 10)
  9. {
  10. while (1)
  11. {
  12. cout<<"\nEnter a course, the letter grade received, and the credit hours. "<<endl;
  13. cin >> mycourses[cnt].courses;
  14.  
  15. if (mycourses[cnt].courses == "quit" || mycourses[cnt].courses == "QUIT" || mycourses[cnt].courses == "Quit") break; //ends input when quit or QUIT is typed
  16.  
  17. cin >> mycourses[cnt].grades;
  18. cin >> mycourses[cnt].hours;
  19.  
  20. in = convert(in, cnt, aClass mycourses[]); //converts letter input into right value
  21. gpaForOneClass = calcGradePoint(in, aClass mycourses[], cnt); //gets gpa total for one class
  22. gpaSum += gpaForOneClass; //totals up gpa
  23. totalHours = totalHours + mycourses[cnt].hours; //totals up hours
  24. cnt = cnt + 1;
  25.  
  26. }
  27.  
  28. }
  29.  
  30. gpa = calcGPA(gpaSum, totalHours); //calls gpa function
  31. printHead(); //calls header function to display table
  32. sortClass(aClass mycourses[], cnt); // calls bsort function
  33. printLine(cnt, b, aClass mycourses[], gpa, totalHours);
  34.  
  35.  
  36.  
  37. cin>>cnt;
  38.  
  39. return 0;
  40. }



and here are the errors i am receiving in full...

C++ Syntax (Toggle Plain Text)
  1. 15 C:\Documents and Settings\Josh\Desktop\Lab5\cs110lab5.cpp In file included from C:/Documents and Settings/Josh/Desktop/Lab5/cs110lab5.cpp
  2. C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h In function `void sortClass(aClass*, int)':
  3. 113 C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h cannot convert `char' to `aGrade' in assignment
  4. /Documents and Settings/Josh/Desktop/Lab5/schedule.h C:\Documents and Settings\Josh\Desktop\Lab5\C At global scope:
  5. 134 C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h syntax error before `[' token
  6. C:\Documents and Settings\Josh\Desktop\Lab5\cs110lab5.cpp In function `int main()':
  7.  
  8. 31 C:\Documents and Settings\Josh\Desktop\Lab5\cs110lab5.cpp no match for 'operator>>' in 'std::cin >> mycourses[cnt].aClass::grades'
  9. error C:\Dev-Cpp\include\c++\3.3.1\bits\istream.tcc:83 candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT,
  10. 92 C:\Dev-Cpp\include\c++\3.3.1\bits\istream.tcc std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT,
  11. 16 more of these errors.
  12.  
  13. 644 C:\Dev-Cpp\include\c++\3.3.1\istream std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char,
  14. 3 more of these errors.
  15.  
  16. 74 C:\Dev-Cpp\include\c++\3.3.1\iomanip std::basic_istream<_CharT, _Traits>&
  17. 4 more of these errors.
  18.  
  19. 34 C:\Documents and Settings\Josh\Desktop\Lab5\cs110lab5.cpp syntax error before `[' token
  20. 35 C:\Documents and Settings\Josh\Desktop\Lab5\cs110lab5.cpp syntax error before `[' token
  21. 46 C:\Documents and Settings\Josh\Desktop\Lab5\cs110lab5.cpp syntax error before `[' token
  22. 47 C:\Documents and Settings\Josh\Desktop\Lab5\cs110lab5.cpp syntax error before `[' token
  23.  

There is a problem with the enum file aGrade im sure but I'm not exactly sure how to fix it...

And i dont know how to fix the" syntax error before `[' token " errors.
Any advice? Thanks.
Reputation Points: 10
Solved Threads: 3
Junior Poster
jhdobbins is offline Offline
105 posts
since Apr 2005
Apr 21st, 2005
0

Re: struct array and enum problems

Sorry, I've just taken a cursory look.
Quote originally posted by jhdobbins ...
There is a problem with the enum file aGrade im sure but I'm not exactly sure how to fix it...
Should tmp be an aGrade, should mycourses[j+1].grades be a char, or do you need to convert from a char to an aGrade?

Quote originally posted by jhdobbins ...
And i dont know how to fix the" syntax error before `[' token " errors.
Provide type information for all parameters in the function definition.
void printLine (/*???*/cnt, /*???*/b, aClass mycourses[], /*???*/gpa, /*???*/totalHour)

Quote originally posted by jhdobbins ...
(we are supposed to include our functions here this file... i know its ugly.. but its at the professors/Teaching Assistants discretion.)
My condolences. I wonder what other bad programming practices you will be taught.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Best C Compiler?
Next Thread in C++ Forum Timeline: *Pointers





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC