Thread: Need Help with Object-Based Programming with array

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    39

    Question Need Help with Object-Based Programming with array

    I cant figure out this problem i think i have everything right but i cant get it to compile, no idea where i messed up, please help if you can.

    Create a class Student. The class has member data: name and tests (array of 3 test scores). It has several functions: setName, getName, setTests, and displayResult. The function setTests will copy the test scores from the integer array argument into the class data tests. The function displayResult will first calculate the average score from the 3 test scores. It then displays student’s name, 3 test scores and the average score on the same line. You must create Student.h file to store class definition; Student.cpp file to store the class implementation code; and mid2.cpp to store the main function of the program.


    Student.h
    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    
    using namespace std;
    class Student
    {
        public:
            Student();
            void setName(string);
            void setTests(int[]);
            string getName();
    
            void displayResult();
    
        private:
                string n;
                int t[3];
    
    };

    Student.cpp
    Code:
    #include "Student.h"
    
    Student::Student()
    {
    	setName(0);
    	setTests(0);
    }
    
    void Student::setName(string name)
    {
    	n = name;
    }
    
    void Student::setTests(int tests[])
    {
    	for (int i=0;i<3;i++)
    		t[i] = tests[i];
    }
    
    string Student::getName()
    {
    	return n;
    }
    
    void Student::displayResult()
    {
    	double a;
    	
    	a = (t[0]+t[1]+t[2])/3;
    	
    	cout << "Name" << "Tests" << "Average" << endl;
    	cout << getName << t[0] << "  " << t[1] << "  " << t[2] << a << endl;
    }
    mid2.cpp
    Code:
    int main()
    {
    	Student student;
    	
    	string n;
    	int t[3];
    	
    	cout << "Enter student name: ";
    	getline(cin, n);
    	
    	student.setName(n);
    	
    	cout << "Enter student test scores: ";
    	cin >> t[0] >> t[1] >> t[2];
    	
    	student.setTests(t);
    	
    	student.displayResult();
    	
    	system("PAUSE");
    	return EXIT_SUCCESS;
    }

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    i cant get it to compile, no idea where i messed up
    But your compiler knows it. Why not listen to it. What does it say?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    39
    Sorry im kinda a noob at this :/
    for the Student.cpp i get
    32 no match for 'operator<<' in 'std::cout << ((Student*)this)->Student::getName'
    and for the mid2.cpp i get
    In function `main':
    [Linker error] undefined reference to `Student::Student()'
    [Linker error] undefined reference to `Student::setName(std::string)'
    [Linker error] undefined reference to `Student::setTests(int*)'
    [Linker error] undefined reference to `Student::displayResult()'
    ld returned 1 exit status

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    cout << getName << t[0] << " " << t[1] << " " << t[2] << a << endl;

    Don't you want to call the getName function ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    to compile your project, you need to build both cpp files together:

    g++ -o mid2 mid2.cpp Student.cpp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. looking for non-MFC based socket programming sample
    By George2 in forum Networking/Device Communication
    Replies: 1
    Last Post: 06-19-2006, 05:57 AM
  2. small programming job VCPP / Object Oriented Programming
    By calgonite in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 01-04-2006, 11:48 PM
  3. languages based off of C programming
    By scarr105 in forum C Programming
    Replies: 1
    Last Post: 03-09-2005, 01:53 PM
  4. C++ Programming - stack based calculator program
    By tinkerbelle in forum C++ Programming
    Replies: 9
    Last Post: 10-13-2003, 03:49 PM
  5. Future of European/U.S. Based Programming
    By EvBladeRunnervE in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 08-07-2003, 10:49 PM

Tags for this Thread