| | |
struct array and enum problems
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2005
Posts: 105
Reputation:
Solved Threads: 3
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:
and here is my .cpp
and here are the ridiculous amount errors i get:
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)
#include<iostream> // all the classes I could ever need for this program #include<iomanip> // and a few extra just incase. #include<cmath> #include<cstdlib> #include<string> using namespace std; enum aGrade {a, b, c, d, f, w, i}; struct aClass { string courses; aGrade grades; int hours; float GP; }; aClass mycourses; void printHead () // this section will display the heading for the output table { cout<<"\n\nList of the Courses"<<endl<<"\n Class Hours Grade \n"; cout<<"-----------------------------------"<<endl; } aGrade convert(enum aGrade in, //OUT: in aClass mycourses) //IN: grades { switch (mycourses.grades) //switch stmt to get all the letters into the enum catagories { case 'a':case 'A': in = a; break; case 'b':case 'B': in = b; break; case 'c':case 'C': in = c; break; case 'd':case 'D': in = d; break; case 'f':case 'F': in = f; break; case 'w':case 'W': in = w; break; case 'i':case 'I': in = i; break; } return in; } int calcGradePoint(enum aGrade in, aClass mycourses) //This function is used to calculate the totalgpa for one class { if(in == a) return 4 * mycourses.hours; if(in == b) return 3 * mycourses.hours; if(in == c) return 2 * mycourses.hours; if(in == d) return 1 * mycourses.hours; if(in == f) return 0 * mycourses.hours; if(in == w) return 0 * mycourses.hours; if(in == i) return 0 * mycourses.hours; } void sortClass (aClass mycourses) //IN: cnt //This bubblesort is used to put the courses into alphabetical order { int i = cnt, j = 0, k = 1, temper; string temp; char tmp; while (i >= 0 && k) { k = 0; for (j = 0; j <= i; j++) if (mycourses.courses[j] > mycourses.courses[j+1]) { temp = mycourses.courses[j]; mycourses.courses[j] = mycourses.courses[j+1]; mycourses.courses[j+1] = temp; temper = mycourses.hours[j]; mycourses.hours[j] = mycourses.hours[j+1]; mycourses.hours[j+1] = temper; tmp = mycourses.grades[j]; mycourses.grades[j] = mycourses.grades[j+1]; mycourses.grades[j+1] = tmp; k = 1; } i--; } } float calcGPA(const float gpaSum, //IN: gpaSum const float totalHours ) //IN: totalHours //This function is used to calculate overall gpa = totalofgpa / totalhours { float gpa; gpa = float (gpaSum / totalHours); return gpa; } void printLine (cnt, b, aClass mycourses, gpa, totalHour) { for (int b = 1; b <= cnt; b++) //puts the information into the table { cout<<setw(12)<<mycourses.courses<<setw(9)<<mycourses.hours<<setw(10)<<mycourses.grades<<endl; } cout<<"\nThe total GPA is "<<gpa<<" and you attempted "<<totalHours<<" hours."<<endl; }
and here is my .cpp
C++ Syntax (Toggle Plain Text)
#include "schedule.h" int main(){ //Calls main function int cnt = 0; //Variables used char gpaForOneClass; float gpa = 0.0, totalHours = 0.0, gpaSum = 0.0; aGrade in; while (cnt < 10) { while (1) { cout<<"\nEnter a course, the letter grade received, and the credit hours. "<<endl; cin >> mycourses.courses; if (mycourses.courses == "quit" || mycourses.courses == "QUIT" || mycourses.courses == "Quit") break; //ends input when quit or QUIT is typed cin >> mycourses.grades; cin >> mycourses.hours; in = convert(in, aClass mycourses); //converts letter input into right value gpaForOneClass = calcGradePoint(in, aClass mycourses, cnt); //gets gpa total for one class gpaSum += gpaForOneClass; //totals up gpa totalHours = totalHours + mycourses.hours; //totals up hours cnt = cnt + 1; } } gpa = calcGPA(gpaSum, totalHours); //calls gpa function printHead(); //calls header function to display table sortClass(aClass mycourses, cnt); // calls bsort function printLine(cnt, b, aClass mycourses, gpa, totalHours); cin>>cnt; return 0; }
and here are the ridiculous amount errors i get:
C++ Syntax (Toggle Plain Text)
15 C:\Documents and Settings\Josh\Desktop\Lab5\cs110lab5.cpp In file included from C:/Documents and Settings/Josh/Desktop/Lab5/cs110lab5.cpp C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h In function `void sortClass(aClass)': 87 C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h `cnt' undeclared (first use this function) 87 C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h (Each undeclared identifier is reported only once for each function it appears 100 C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h cannot convert `std::string' to `char' in assignment 102 C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h invalid types `int[int]' for array subscript 103 C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h invalid types `int[int]' for array subscript 103 C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h invalid types `int[int]' for array subscript 104 C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h invalid types `int[int]' for array subscript 106 C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h invalid types `aGrade[int]' for array subscript 107 C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h invalid types `aGrade[int]' for array subscript 107 C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h invalid types `aGrade[int]' for array subscript 108 C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h invalid types `aGrade[int]' for array subscript /Documents and Settings/Josh/Desktop/Lab5/schedule.h C:\Documents and Settings\Josh\Desktop\Lab5\C At global scope: 129 C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h syntax error before `,' token C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h In function `void printLine(...)': 136 C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h `gpa' undeclared (first use this function) 136 C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h `totalHours' undeclared (first use this function) C:\Documents and Settings\Josh\Desktop\Lab5\cs110lab5.cpp In function `int main()': 35 C:\Documents and Settings\Josh\Desktop\Lab5\cs110lab5.cpp no match for 'operator>>' in 'std::cin >> mycourses.aClass::grades' error C:\Dev-Cpp\include\c++\3.3.1\bits\istream.tcc:83 candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, 92 C:\Dev-Cpp\include\c++\3.3.1\bits\istream.tcc std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, 101 C:\Dev-Cpp\include\c++\3.3.1\bits\istream.tcc std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, That same error.... 19 more times then.. 74 C:\Dev-Cpp\include\c++\3.3.1\iomanip std::basic_istream<_CharT, _Traits>& that same error....4 more times 38 C:\Documents and Settings\Josh\Desktop\Lab5\cs110lab5.cpp syntax error before `)' token 39 C:\Documents and Settings\Josh\Desktop\Lab5\cs110lab5.cpp syntax error before `,' token 50 C:\Documents and Settings\Josh\Desktop\Lab5\cs110lab5.cpp syntax error before `,' token 51 C:\Documents and Settings\Josh\Desktop\Lab5\cs110lab5.cpp syntax error before `,' token
Last edited by BountyX; Apr 20th, 2005 at 7:46 pm. Reason: Code Tags
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.
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.
A Hacker's Mind:
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
•
•
Join Date: Apr 2005
Posts: 105
Reputation:
Solved Threads: 3
•
•
•
•
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.
•
•
Join Date: Apr 2005
Posts: 105
Reputation:
Solved Threads: 3
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.)
and here is my main program... .cpp
and here are the errors i am receiving in full...
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.
C++ Syntax (Toggle Plain Text)
#include<iostream> // all the classes I could ever need for this program #include<iomanip> // and a few extra just incase. #include<cmath> #include<cstdlib> #include<string> using namespace std; enum aGrade {a, b, c, d, f, w, i}; struct aClass { string courses; aGrade grades; int hours; float GP; }; int cnt = 0; //Variables used char gpaForOneClass; float gpa = 0.0, totalHours = 0.0, gpaSum = 0.0; aGrade in; aClass mycourses[10]; void printHead () // this section will display the heading for the output table { cout<<"\n\nList of the Courses"<<endl<<"\n Class Hours Grade \n"; cout<<"-----------------------------------"<<endl; } aGrade convert(enum aGrade in, int cnt, //OUT: in aClass mycourses[]) //IN: grades { switch (mycourses[cnt].grades) //switch stmt to get all the letters into the enum catagories { case 'a':case 'A': in = a; break; case 'b':case 'B': in = b; break; case 'c':case 'C': in = c; break; case 'd':case 'D': in = d; break; case 'f':case 'F': in = f; break; case 'w':case 'W': in = w; break; case 'i':case 'I': in = i; break; } return in; } int calcGradePoint(enum aGrade in, aClass mycourses[]) //This function is used to calculate the totalgpa for one class { if(in == a) return 4 * mycourses[cnt].hours; if(in == b) return 3 * mycourses[cnt].hours; if(in == c) return 2 * mycourses[cnt].hours; if(in == d) return 1 * mycourses[cnt].hours; if(in == f) return 0 * mycourses[cnt].hours; if(in == w) return 0 * mycourses[cnt].hours; if(in == i) return 0 * mycourses[cnt].hours; } void sortClass (aClass mycourses[], int cnt) //IN: cnt //This bubblesort is used to put the courses into alphabetical order { int i = cnt, j = 0, k = 1, temper; string temp; char tmp; while (i >= 0 && k) { k = 0; for (j = 0; j <= i; j++) if (mycourses[j].courses > mycourses[j+1].courses) { temp = mycourses[j].courses; mycourses[j].courses = mycourses[j+1].courses; mycourses[j+1].courses = temp; temper = mycourses[j].hours; mycourses[j].hours = mycourses[j+1].hours; mycourses[j+1].hours = temper; tmp = mycourses[j].grades; mycourses[j].grades = mycourses[j+1].grades; mycourses[j+1].grades = tmp; k = 1; } i--; } } float calcGPA(const float gpaSum, //IN: gpaSum const float totalHours ) //IN: totalHours //This function is used to calculate overall gpa = totalofgpa / totalhours { float gpa; gpa = float (gpaSum / totalHours); return gpa; } void printLine (cnt, b, aClass mycourses[], gpa, totalHour) { for (int b = 1; b <= cnt; b++) //puts the information into the table { cout<<setw(12)<<mycourses[b].courses<<setw(9)<<mycourses[b].hours<<setw(10)<<mycourses[b].grades<<endl; } cout<<"\nThe total GPA is "<<gpa<<" and you attempted "<<totalHours<<" hours."<<endl; }
and here is my main program... .cpp
C++ Syntax (Toggle Plain Text)
#include "schedule.h" int main(){ //Calls main function while (cnt < 10) { while (1) { cout<<"\nEnter a course, the letter grade received, and the credit hours. "<<endl; cin >> mycourses[cnt].courses; if (mycourses[cnt].courses == "quit" || mycourses[cnt].courses == "QUIT" || mycourses[cnt].courses == "Quit") break; //ends input when quit or QUIT is typed cin >> mycourses[cnt].grades; cin >> mycourses[cnt].hours; in = convert(in, cnt, aClass mycourses[]); //converts letter input into right value gpaForOneClass = calcGradePoint(in, aClass mycourses[], cnt); //gets gpa total for one class gpaSum += gpaForOneClass; //totals up gpa totalHours = totalHours + mycourses[cnt].hours; //totals up hours cnt = cnt + 1; } } gpa = calcGPA(gpaSum, totalHours); //calls gpa function printHead(); //calls header function to display table sortClass(aClass mycourses[], cnt); // calls bsort function printLine(cnt, b, aClass mycourses[], gpa, totalHours); cin>>cnt; return 0; }
and here are the errors i am receiving in full...
C++ Syntax (Toggle Plain Text)
15 C:\Documents and Settings\Josh\Desktop\Lab5\cs110lab5.cpp In file included from C:/Documents and Settings/Josh/Desktop/Lab5/cs110lab5.cpp C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h In function `void sortClass(aClass*, int)': 113 C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h cannot convert `char' to `aGrade' in assignment /Documents and Settings/Josh/Desktop/Lab5/schedule.h C:\Documents and Settings\Josh\Desktop\Lab5\C At global scope: 134 C:\Documents and Settings\Josh\Desktop\Lab5\schedule.h syntax error before `[' token C:\Documents and Settings\Josh\Desktop\Lab5\cs110lab5.cpp In function `int main()': 31 C:\Documents and Settings\Josh\Desktop\Lab5\cs110lab5.cpp no match for 'operator>>' in 'std::cin >> mycourses[cnt].aClass::grades' error C:\Dev-Cpp\include\c++\3.3.1\bits\istream.tcc:83 candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, 92 C:\Dev-Cpp\include\c++\3.3.1\bits\istream.tcc std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, 16 more of these errors. 644 C:\Dev-Cpp\include\c++\3.3.1\istream std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, 3 more of these errors. 74 C:\Dev-Cpp\include\c++\3.3.1\iomanip std::basic_istream<_CharT, _Traits>& 4 more of these errors. 34 C:\Documents and Settings\Josh\Desktop\Lab5\cs110lab5.cpp syntax error before `[' token 35 C:\Documents and Settings\Josh\Desktop\Lab5\cs110lab5.cpp syntax error before `[' token 46 C:\Documents and Settings\Josh\Desktop\Lab5\cs110lab5.cpp syntax error before `[' token 47 C:\Documents and Settings\Josh\Desktop\Lab5\cs110lab5.cpp syntax error before `[' token
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.
Sorry, I've just taken a cursory look. 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?
Provide type information for all parameters in the function definition.
My condolences. I wonder what other bad programming practices you will be taught.
•
•
•
•
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...
•
•
•
•
Originally Posted by jhdobbins
And i dont know how to fix the" syntax error before `[' token " errors.
void printLine (/*???*/cnt, /*???*/b, aClass mycourses[], /*???*/gpa, /*???*/totalHour)
•
•
•
•
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.)
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Similar Threads
- how to declare array of struct (C#)
- Using a struct array (C++)
- array problems (C++)
- struct dynamic 2d array alloc (C)
Other Threads in the C++ Forum
- Previous Thread: Best C Compiler?
- Next Thread: *Pointers
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






