Thread: Array Of Pointers With Objects

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    15

    Array Of Pointers With Objects

    I'm having some issues with a homework assignment. I've tried everything I can think of but there is just something I'm missing. Basically the assignment tells me to make a composition class with a student class that stores names and test scores and that is within a studentInformation class which takes information from an array of student objects and is able to take information from specific objects in the array and make data charts based on that information. Right now I am getting a bunch of ERROR 2228's. And that's what I'm having trouble figuring out how to fix. Now comes the big block of code. Sorry I wish it were smaller but I have no idea where to narrow it down. So here is my code thus far.

    Code:
    /************************************************
     * William Meabrod								*
     *												*
     * CIS2152 C++ Object Oriented Programming		*
     *												*
     * lab3.cpp										*
     *												*
     * This program will make a composision class	*
     *												*								*
     ************************************************/
    
    #include "studentInformation.h"
    int stop = 1;
    string lName;
    string fName;
    double fScore;
    double midScore;
    double lab1Score;
    double lab2Score;
    int main()
    {
    	studentInformation CIS2152;
    	while(stop) //continues asking for additional students until user askes the loop to stop
    	{
    		cout << "\nPlease enter student's first name(if you are finished type "
    			<< "exit for a first name): ";
    		cin >> fName;
    		if(fName!="exit")
    		{
    			cout << "\nNow enter the student's last name: ";
    			cin >> lName;
    			cout << "\nNow enter the student's final exam score: ";
    			cin >> fScore;
    			cout << "\nNow enter the student's midterm exam score: ";
    			cin >> midScore;
    			cout << "\nNow enter the student's lab1 score: ";
    			cin >> lab1Score;
    			cout << "\nNow enter the student's lab2 exam score: ";
    			cin >> lab2Score;
    			CIS2152.add(fName,lName,fScore,midScore,lab1Score,lab2Score);
    		}
    		else
    		{
    			stop = 0;
    		}
    	}
    	int input;
    	string input2;
    	stop = 1;
    	while(stop) //Continues program until user wants to exit
    	{
    		cout << "\n\n1 : Print All Students and Final Scores\n"
    			 << "2 : Print All Students and Letter Grades\n"
    			 << "3 : Print Highest Final Score\n"
    			 << "4 : Print Lowest Final Score\n"
    			 << "5 : Print Average Final Score\n"
    			 << "6 : Print All Students Final Grade Chart\n"
    			 << "0 : Exit The Program\n\n"
    			 << "Please Enter Your Choice: ";
    		cin  >> input;
    		switch(input)
    		{
    			case 1: //Print names and final scores
    			{
    				CIS2152.print();
    				system("pause"); 
    				break;
    			}
    			case 2: //Print names and grades
    			{
    				CIS2152.printGrades();
    				system("pause");
    				break;
    			}
    			case 3: //print highest score
    			{
    				cout << "The highest final score is: " << CIS2152.highestScore()<< "\n";
    				system("pause"); 
    				break;
    			}
    			case 4: //print lowest score
    			{
    				cout << "The lowest final score is: "  << CIS2152.lowestScore() << "\n";
    				system("pause");
    				break;
    			}
    			case 5: //print average score
    			{
    				cout << "The class average is: "       << CIS2152.avgScore() << "\n";
    				system("pause");
    				break;
    			}
    			case 6:
    			{
    				CIS2152.printChart();
    				system("pause");
    				break;
    			}
    			default:
    			{
    				stop = 0;
    			}
    		}
    	}
    
    	return 0;
    }
    Code:
    /************************************************
     * William Meabrod								*
     *												*
     * CIS2152 C++ Object Oriented Programming		*
     *												*
     * student.h										*
     *												*
     * This program will make a composision class	*
     *												*								*
     ************************************************/
    #include <iostream>
    using std::cout;
    using std::cin;
    #include <string>
    using std::string;
    #include <iomanip>
    using std::setw;
    
    class student
    {
    	public:
    		       student( string, string, double, double, double, double );
    		void   setFirstName(string);
    		void   setLastName(string);
    		void   setFinalScore(double);
    		void   setMidScore(double);
    		void   setLab1(double);
    		void   setLab2(double);
    		void   setClassScore();
    		string getFirstName();
    		string getLastName();
    		double getFinal();
    		double getMidScore();
    		double getLab1();
    		double getLab2();
    		double getClassScore();
    	private:
    		string firstName;
    		string lastName;
    		double finalScore;
    		double midScore;
    		double lab1;
    		double lab2;
    		double classScore;
    };
    Code:
    /************************************************
     * William Meabrod								*
     *												*
     * CIS2152 C++ Object Oriented Programming		*
     *												*
     * studentInformation.h										*
     *												*
     * This program will make a composision class	*
     *												*								*
     ************************************************/
    #include "student.h"
    class studentInformation
    {
    	public:
    		studentInformation();
    		void setArray();
    		void add(string, string, double, double, double, double);
    		void print();
    		void printGrades();
    		void printChart();
    		double highestScore();
    		double lowestScore();
    		double avgScore();
    	private:
    		student *stuInfo[30];
    		int arrayNum;
    		int A;
    		int B;
    		int C;
    		int F;
    		double totalA;
    		double totalB;
    		double totalC;
    		double totalF;
    
    };
    Code:
    /************************************************
     * William Meabrod								*
     *												*
     * CIS2152 C++ Object Oriented Programming		*
     *												*
     * student.cpp										*
     *												*
     * This program will make a composision class	*
     *												*								*
     ************************************************/
    #include "student.h"
    
    student::student(string fName, string lName, double fScore, double midTerm, double labOne,
    				 double labTwo) //contructor
    {
    	setFirstName(fName);
    	setLastName(lName);
    	setFinalScore(fScore);
    	setMidScore(midTerm);
    	setLab1(labOne);
    	setLab2(labTwo);
    	setClassScore();
    }
    void setFirstName(string fName)
    {
    	firstName = fName;
    }
    void setLastName(string lName)
    {
    	lastName = lName;
    }
    void setFinalScore(double fScore)
    {
    	finalScore = fScore;
    }
    void setMidScore(double midTerm)
    {
    	midScore = midTerm;
    }
    void setLab1(double labOne)
    {
    	lab1 = labOne;
    }
    void setLab2(double labTwo)
    {
    	lab2 = labTwo;
    }
    void   setClassScore()
    {
    	classScore = (finalScore + midScore + lab1 + lab2) / 4;
    }
    string getFirstName()
    {
    	return firstName;
    }
    string getLastName()
    {
    	return lastName;
    }
    double getFinalScore()
    {
    	return finalScore;
    }
    double getMidScore()
    {
    	return midScore;
    }
    double getLab1()
    {
    	return lab1;
    }
    double getLab2()
    {
    	return lab2;
    }
    double getClassScore()
    {
    	return classScore;
    }
    Code:
    /************************************************
     * William Meabrod								*
     *												*
     * CIS2152 C++ Object Oriented Programming		*
     *												*
     * studentInformation.cpp										*
     *												*
     * This program will make a composision class	*
     *												*								*
     ************************************************/
    #include "studentInformation.h"
    studentInformation::studentInformation() //constructor
    {
    	setArray();
    }
    void studentInformation::setArray() //sets a number representing the current array slot number
    //being used
    {
    	arrayNum = -1;
    }
    
    
    void studentInformation::add(string fName, string lName, double fScore, double midTerm, double lab1,
    							 double lab2) //adds a new student
    //to the next slot on the array list
    {
    	arrayNum++;
    	stuInfo[arrayNum] = new student(fName, lName, fScore, midTerm, lab1, lab2);
    	
    }
    
    void studentInformation::print() //prints all students and final scores
    {
    	std::cout << "\nFirst Name" << setw(13) << "Last Name" << setw(13) << "Final Score\n\n";
    	for(int i = arrayNum; i >= 0; i--)
    	{
    		std::cout << stuInfo[i].getFirstName() << setw(13) << stuInfo[i].getLastName() << setw(13) << stuInfo[i].getClassScore() << "\n";
    	}
    }
    void studentInformation::printGrades() //prints all students and letter grades
    {
    	std::cout << "First Name" << setw(13) << "Last Name" << setw(13) << "Grade\n\n";
    	for(int i = arrayNum; i >= 0; i--)
    	{
    		if(stuInfo[i].getClassScore() >= 90 && stuInfo[i].getClassScore() <= 100)
    		{
    			std::cout << stuInfo[i].getFirstName() << setw(13) << stuInfo[i].getLastName() << setw(13) << "A" << "\n";
    		}
    		else if(stuInfo[i].getClassScore() >= 80 && stuInfo[i].getClassScore() < 90)
    		{
    			std::cout << stuInfo[i].getFirstName() << setw(13) << stuInfo[i].getLastName() << setw(13) << "B" << "\n";
    		}
    		else if(stuInfo[i].getClassScore() >= 70 && stuInfo[i].getClassScore() < 80)
    		{
    			std::cout << stuInfo[i].getFirstName() << setw(13) << stuInfo[i].getLastName() << setw(13) << "C" << "\n";
    		}
    		else if(stuInfo[i].getClassScore() < 70 && stuInfo[i].getClassScore() >= 0)
    		{
    			std::cout << stuInfo[i].getFirstName() << setw(13) << stuInfo[i].getLastName() << setw(13) << "F" << "\n";
    		}
    		else if(stuInfo[i].getClassScore() > 100 || stuInfo[i].getClassScore() < 0)
    		{
    			std::cout << stuInfo[i].getFirstName() << setw(13) << stuInfo[i].getLastName() << setw(13) << "ERROR" << "\n";
    		}
    	}
    }
    
    void studentInformation::printChart()
    {
    	A = 0;
    	B = 0;
    	C = 0;
    	F = 0;
    
    	for(int i = arrayNum; i >= 0; i--)
    	{
    		if(stuInfo[i].getClassScore() == 100)
    		{
    			std::cout << stuInfo[i].getFirstName() << setw(13) << stuInfo[i].getLastName() << setw(13) << "**********" << "\n";
    			A++;
    			totalA += stuInfo[i].getClassScore();
    		}
    		if(stuInfo[i].getClassScore() >= 90 && stuInfo[i].getClassScore() <= 100)
    		{
    			std::cout << stuInfo[i].getFirstName() << setw(13) << stuInfo[i].getLastName() << setw(13) << "*********" << "\n";
    			A++;
    			totalA += stuInfo[i].getClassScore();
    		}
    		else if(stuInfo[i].getClassScore() >= 80 && stuInfo[i].getClassScore() < 90)
    		{
    			std::cout << stuInfo[i].getFirstName() << setw(13) << stuInfo[i].getLastName() << setw(13) << "********" << "\n";
    			B++;
    			totalB += stuInfo[i].getClassScore();
    		}
    		else if(stuInfo[i].getClassScore() >= 70 && stuInfo[i].getClassScore() < 80)
    		{
    			std::cout << stuInfo[i].getFirstName() << setw(13) << stuInfo[i].getLastName() << setw(13) << "*******" << "\n";
    			C++;
    			totalC += stuInfo[i].getClassScore();
    		}
    		else if(stuInfo[i].getClassScore() >= 60 && stuInfo[i].getClassScore() < 70)
    		{
    			std::cout << stuInfo[i].getFirstName() << setw(13) << stuInfo[i].getLastName() << setw(13) << "******" << "\n";
    			F++;
    			totalF += stuInfo[i].getClassScore();
    		}
    		else if(stuInfo[i].getClassScore() >= 50 && stuInfo[i].getClassScore() < 60)
    		{
    			std::cout << stuInfo[i].getFirstName() << setw(13) << stuInfo[i].getLastName() << setw(13) << "*****" << "\n";
    			F++;
    			totalF += stuInfo[i].getClassScore();
    		}
    		else if(stuInfo[i].getClassScore() >= 40 && stuInfo[i].getClassScore() < 50)
    		{
    			std::cout << stuInfo[i].getFirstName() << setw(13) << stuInfo[i].getLastName() << setw(13) << "****" << "\n";
    			F++;
    			totalF += stuInfo[i].getClassScore();
    		}
    		else if(stuInfo[i].getClassScore() >= 30 && stuInfo[i].getClassScore() < 40)
    		{
    			std::cout << stuInfo[i].getFirstName() << setw(13) << stuInfo[i].getLastName() << setw(13) << "***" << "\n";
    			F++;
    			totalF += stuInfo[i].getClassScore();
    		}
    		else if(stuInfo[i].getClassScore() >= 20 && stuInfo[i].getClassScore() < 30)
    		{
    			std::cout << stuInfo[i].getFirstName() << setw(13) << stuInfo[i].getLastName() << setw(13) << "**" << "\n";
    			F++;
    			totalF += stuInfo[i].getClassScore();
    		}
    		else if(stuInfo[i].getClassScore() >= 10 && stuInfo[i].getClassScore() < 20)
    		{
    			std::cout << stuInfo[i].getFirstName() << setw(13) << stuInfo[i].getLastName() << setw(13) << "*" << "\n";
    			F++;
    			totalF += stuInfo[i].getClassScore();
    		}
    		else if(stuInfo[i].getClassScore() >= 0 && stuInfo[i].getClassScore() < 10)
    		{
    			std::cout << stuInfo[i].getFirstName() << setw(13) << stuInfo[i].getLastName() << setw(13) << " " << "\n";
    			F++;
    			totalF += stuInfo[i].getClassScore();
    		}
    		else if(stuInfo[i].getClassScore() > 100 || stuInfo[i].getClassScore() < 0)
    		{
    			std::cout << stuInfo[i].getFirstName() << setw(13) << stuInfo[i].getLastName() << setw(13) << "ERROR" << "\n";
    		}
    	}
    	cout << "\nA: " << setw(8) << A << setw(8) << (totalA/A)
    		 << "\nB: " << setw(8) << B << setw(8) << (totalB/B)
    		 << "\nC: " << setw(8) << C << setw(8) << (totalC/C)
    		 << "\nF: " << setw(8) << F << setw(8) << (totalF/F) << "\n";
    }
    
    double studentInformation::highestScore() //returns the highest final score
    {
    	double highGrade = 0;
    	for(int i = arrayNum; i >= 0; i--)
    	{
    		if(stuInfo[i].getClassScore() > highGrade)
    		{
    			highGrade = stuInfo[i].getClassScore();
    		}
    	}
    	return highGrade;
    }
    double studentInformation::lowestScore() //returns the lowest final score
    {
    	double lowGrade = 100;
    	for(int i = arrayNum; i >= 0; i--)
    	{
    		
    		if(stuInfo[i].getClassScore() < lowGrade)
    		{
    			lowGrade = stuInfo[i].getClassScore();
    		}
    	}
    	return lowGrade;
    }
    double studentInformation::avgScore() //returns the average final score
    {
    	double totalScore = 0;
    	for(int i = arrayNum; i>=0; i--)
    	{
    		totalScore = stuInfo[i].getClassScore() + totalScore;
    	}
    	double aScore = totalScore/(arrayNum + 1);
    	return aScore;
    }
    Can anyone help?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Very nice indentation! I would personally use slightly more vertical whitespace, but that's the most nicely indented program that I've seen for a long time.

    Next time it would help if you highlighted the lines where the errors are occurring . . . unless there are far too many of them, in which case you might want to say so.

    I wasn't sure what a C2228 was, but Google quickly told me. http://www.google.ca/search?hl=en&q=...G=Search&meta=
    Compiler Error C2228 (C++)
    The operand to the left of the period (.) is not a class, structure, or union.
    So I looked at all of the places you use the member access operator (".").

    Here's the main problem.
    Code:
    student *stuInfo[30];
    Almost everywhere you use stuInfo, you use it like this.
    Code:
    stuInfo[i].getClassScore()
    stuInfo[i].getFirstName()
    However, stuInfo[i] is a pointer to a class (student *), not a class (student) -- so what you need is something like this:
    Code:
    (*stuInfo[i]).getClassScore()
    (*stuInfo[i]).getFirstName()
    Fortunately, the designers of C forsaw (is that a word? Firefox doesn't think so ...) that this would be a very common situation, and they invented an operator to deal with it, the arrow operator, for lack of a better name. These are equivalent.
    Code:
    (*stuInfo[i]).getClassScore()
    stuInfo[i]->getClassScore()
    Much better, eh?

    In other words, a project-wide search-and-replace of "stuInfo[i]." into "stuInfo[i]->" should solve most of your problems, since you seem to use i as a loop variable most of the time . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    15
    I actually just figured it out on my own. I was using dots instead of arrows to point. Thanks anyway! Sorry if I wasted anyone's time.

    EDIT: Thanks for your help! We arrived at similar solutions! Sorry I couldn't have arrived at it sooner before you found a solution for me.

  4. #4
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    dwks, I think you meant foresaw.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    What's with the big chain of if-else to count all the 'F' scores? There's no difference between an F between 40 and 50, and an F between 30 and 40. All the code inside those blocks is exactly the same.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    42
    Quote Originally Posted by brewbuck View Post
    What's with the big chain of if-else to count all the 'F' scores? There's no difference between an F between 40 and 50, and an F between 30 and 40. All the code inside those blocks is exactly the same.
    Different number of stars is printed, depending on score.

    But still, wouldn't it be nicer to split this code into functions (big if-else-if I mean)? It looks like could be easily unified, using some simple switch and maybe 1-2 separate functions. Just a suggestion though.

    Cheers,

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by mikeman118 View Post
    dwks, I think you meant foresaw.
    Yes, I did . . . .

    Something else to note:
    Code:
    		if(stuInfo[i].getClassScore() == 100)
    		{
    			// ...
    		}
    		else if(stuInfo[i].getClassScore() >= 90 && stuInfo[i].getClassScore() <= 100)
    		{
    			// ...
    		}
    		else if(stuInfo[i].getClassScore() >= 80 && stuInfo[i].getClassScore() < 90)
    		{
    			// ...
    		}
    		// ...
    All of those conditions in red are redundant (assuming getClassScore returns a number between 0 and 100 inclusive). If the second condition of one of those ifs was false, then the previous if would have been true. You can leave out every one of those second conditions and the code will still work -- as long as you have an if-elseif-elseif-else chain. (With if-if-if-if, it doesn't work.)

    I see that the range can be otherwise, because you have this:
    Code:
    else if(stuInfo[i].getClassScore() > 100 || stuInfo[i].getClassScore() < 0)
    If you put that before all other cases, you handle everything. In other words, you can use a construct like this:
    Code:
    if(x > 100 || x < 0) { }
    else if(x > 90) {}
    else if(x > 80) {}
    /* ... */
    else if(x > 10) {}
    else { /* x is from 0 to 9 inclusive */ }
    In main()'s switch(input), you can eliminate all of those calls to system("pause") and use this after the switch:
    Code:
    if(input >= 1 && input <= 6) system("pause");
    Much simpler, no? Of course, you're checking input twice, but I should imagine that's a lot better than having system("pause") in your code six times.

    Also, since every file includes student.h, directly or indirectly, and student.h has using statements which bring various objects such as cout and cin into the global namespace, you never have to use std:: in front of cout, for example. This means that in studentInformation.cpp, you can drop the std::'s from all of the std::cout's.

    Or, better yet, you can add std:: to all of the ordinary cout's and drop the using directives.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  2. array of pointers to an array pointers
    By onebrother in forum C Programming
    Replies: 2
    Last Post: 07-28-2008, 11:45 AM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. array of pointers to objects
    By xion in forum C++ Programming
    Replies: 6
    Last Post: 02-07-2005, 07:14 PM
  5. Replies: 4
    Last Post: 10-16-2003, 11:26 AM