Thread: Need advice classes. Had swine flu and missed school for two weeks =(

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    7

    Need advice classes. Had swine flu and missed school for two weeks =(

    Ok, since this is my first post here I guess I better explain some things =)

    Well I have had swine flu for the past two weeks and in those two weeks I missed a bunch of my C++ classes in school. Turns out we covered structures and classes in that time, which was bad for me to miss because from what I can tell they are both extremely important in C++.

    I managed to do most of my homework I missed, but I'm still stuck on two problems. The first is this:

    1.) Construct a class named Student consisting of an integer student ID number, an array of five double-precision grades, and an integer representing the total number of grades entered. The constructor for this class should initialize all Student data members to 0. Included in the class should be member functions to: 1.) enter a student ID number, 2.) enter a single test grade and update the total number of grades entered, and 3.) compute an average grade and display the student ID followed by the average grade.

    so far for this problem I have:

    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    
    class Student
    {
    	private:
    		int id_number;
    		double grade[5];
    		int count;
    		double average;
    	public:
    		int enterID();
    		void enterGrade();
    		void display();
    };
    
    Student::enterID()
    {
    	std::cout << "Enter student ID number: ";
    	cin >> xx;
    	id_number = xx;
    	
    	return;
    }
    
    Student::enterGrade()
    {
    	std::cout << "Enter a grade: ";
    	for (count = 0; count < 5; count++)
    	{
    		cin >> grade[count];
    	}
    	return;
    }
    
    Student::display();
    {
    	for (count = 0; count < 5; count++)
    	{
    		average += grade[i];
    		if (count = 5)
    			average = (average / 5);
    	}
    	
    	std::cout << "Student ID: " << id_number << std::endl;
    	std::cout << "Average: " << average << std:: endl;
    
    return;
    }
    
    int main()
    {
    	int i = 0;
    	
    	Student a;
    	
    	for (i = 0; i < 5; i++)
    	{
    		a.enterID();
    		a.enterGrade();
    		a.display();
    	}
    return 0;
    }
    and obtaining these errors when compiling it:

    Code:
    class2.cpp:19: error: ISO C++ forbids declaration of `enterID' with no type
    class2.cpp: In member function `int Student::enterID()':
    class2.cpp:21: error: `cin' undeclared (first use this function)
    class2.cpp:21: error: (Each undeclared identifier is reported only once for each
     function it appears in.)
    class2.cpp:21: error: `xx' undeclared (first use this function)
    class2.cpp:24: error: return-statement with no value, in function returning 'int
    '
    class2.cpp: At global scope:
    class2.cpp:28: error: ISO C++ forbids declaration of `enterGrade' with no type
    class2.cpp:28: error: prototype for `int Student::enterGrade()' does not match a
    ny in class `Student'
    class2.cpp:14: error: candidate is: void Student::enterGrade()
    class2.cpp:28: error: `int Student::enterGrade()' and `void Student::enterGrade(
    )' cannot be overloaded
    class2.cpp: In member function `int Student::enterGrade()':
    class2.cpp:32: error: `cin' undeclared (first use this function)
    class2.cpp:34: error: return-statement with no value, in function returning 'int
    '
    class2.cpp: At global scope:
    class2.cpp:37: error: expected constructor, destructor, or type conversion befor
    e ';' token
    class2.cpp:38: error: expected unqualified-id before '{' token
    class2.cpp:38: error: expected `,' or `;' before '{' token
    class2.cpp:67:2: warning: no newline at end of file
    Can anyone tell me whats wrong and what I'm missing? Or at least point me in the right direction?

    Thank you so much.

    (I'll post the structures problem after I find my source file lol)

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Your modified code

    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    
    using namespace std;
    
    class Student
    {
    private:
      int id_number;
      double grade[5];
      int count;
      double average;
    public:
      void enterID();
      void enterGrade();
      void display();
    };
    
    void Student::enterID()
    {
      int xx = 0;
      std::cout << "Enter student ID number: ";
      cin >> xx;
      id_number = xx;
    
      return ;
    }
    
    void Student::enterGrade()
    {
      std::cout << "Enter a grade: ";
      for (count = 0; count < 5; count++)
        {
          cin >> grade[count];
        }
      return;
    }
    
    void Student::display()
    {
      for (int count = 0; count < 5; count++)
        {
          average += grade[count];
          if (count = 5)
            average = (average / 5);
        }
    
      std::cout << "Student ID: " << id_number << std::endl;
      std::cout << "Average: " << average << std:: endl;
    
      return;
    }
    
    int main()
    {
      int i = 0;
    
      Student a;
    
      for (i = 0; i < 5; i++)
        {
          a.enterID();
          a.enterGrade();
          a.display();
        }
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    If you were having swine flu its very bad to hear that if not please don't try to fool the forum members to take help

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    7
    Thank you so much. That works.

    Can you also explain to me what this part of the problem means?:

    ...The constructor for this class should initialize all Student data members to 0...

    And yeah I did have swine flu and it sucked hard haha. I tried to do school work when I could because there would be like 2-4 hours a day I would feel better and think I was ok, then I'd just crash again. Haha.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    in the default constructor with the initializer list just pass 0 to all the student data variables

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by RockyMarrone View Post
    If you were having swine flu its very bad to hear that if not please don't try to fool the forum members to take help
    Meanwhile, you did his homework-- that makes a lot of sense.

Popular pages Recent additions subscribe to a feed