DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   struct array and enum problems (http://www.daniweb.com/forums/thread22240.html)

jhdobbins Apr 20th, 2005 7:24 pm
struct array and enum problems
 
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:

#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


#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:

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

BountyX Apr 20th, 2005 7:57 pm
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.

jhdobbins Apr 20th, 2005 9:30 pm
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. :)

jhdobbins Apr 21st, 2005 12:50 am
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.)


#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

#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...

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.

Dave Sinkula Apr 21st, 2005 11:17 am
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.


All times are GMT -4. The time now is 9:52 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC