need help with this C++ code

Discussion in 'OT Technology' started by notwist, Apr 7, 2005.

  1. notwist

    notwist Active Member

    Joined:
    Nov 8, 2004
    Messages:
    5,738
    Likes Received:
    0
    #include <iostream.h>

    using namespace std;

    typedef class Student{
    public:
    int id[5];
    float Grades[5];
    int numGrades;

    Student()
    {
    id[5]=0;
    Grades[5]=0;
    numGrades=0;
    }
    public:
    void Student::enterID()
    {
    cout << "Enter student ID: ";
    cin >> id;
    }

    void Student::enterGrade()
    {
    cout << "Enter grade: ";
    cin >> Grades[numGrades];
    numGrades+=1;
    }

    void Student::compute()
    {
    int i=0;
    float average,total;

    while(i<numGrades){
    total+=Grades;
    i++;
    }
    average=total/numGrades;

    cout << "Student ID: " << id;
    cout << "\nAverage grade: " << average;
    }
    };

    int main(void)
    {
    Student A,B;
    }

    thsi program is supposed to take in the values indicated in the class and calculate the average grade and print it out as well as the ID number. i realize it's not done yet but even when i try compiling it now (i believe it should work) i get some errors. does anything look wrong?
     
  2. notwist

    notwist Active Member

    Joined:
    Nov 8, 2004
    Messages:
    5,738
    Likes Received:
    0
    yes i fixed that INT/FLOAT warning.
    The program compiles now but it seems that it is not storing each grade. Heres the code:

    Code:
        class Student{
        public:
        int id;
           int numGrades;
           float Grades[5];
          
          Student()
          { 
        	numGrades=0;
        	float Grades[5]={0,0,0,0,0};
          }
          ~Student(){}
          
          void enterID()
          {
        
        	cout << "\nEnter student ID number: ";
        	cin >> id;
          }  
          
          void enterGrade()
          {
        	int in;
        	cout << "\nEnter grade: ";
        	cin >> Grades[numGrades + 1];
        	numGrades += 1;
          }
        	 
          void computeAvg()
          {
        	int i=0;
        	float total=0;
        	float avg;
        
        	while(i<numGrades){
        	  total = total + Grades[i];
        	  i++;
        	}
        
        	avg=total/numGrades;
        
        	cout << "\nStudent ID: " << id;
        	cout << "\nAverage grade is " << avg;
          }
        };
        
    it just spits out garbage values. i'm not sure what the problem is. :wtc:
    hmm.. can't seem to get it to display nicely.
     
  3. notwist

    notwist Active Member

    Joined:
    Nov 8, 2004
    Messages:
    5,738
    Likes Received:
    0
    it always was printing the id. just the grades were screwy.
     
  4. notwist

    notwist Active Member

    Joined:
    Nov 8, 2004
    Messages:
    5,738
    Likes Received:
    0
    bu tthanks anyway.
     
  5. notwist

    notwist Active Member

    Joined:
    Nov 8, 2004
    Messages:
    5,738
    Likes Received:
    0
    aha got it!
     

Share This Page