Help with C++ Homework

This is a discussion on Help with C++ Homework within the C++ Programming forums, part of the General Programming Boards category; Don't worry, I'm not asking you to do it for me. If it makes you feel better you could just ...

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

    Help with C++ Homework

    Don't worry, I'm not asking you to do it for me. If it makes you feel better you could just point me to the right line and have me figure it out. But this thing is making my head hurt so I have to ask. I was assigned a homework assignment to make a student information class that allows a user to enter in a bunch of names and grades and get them back as an average, max, and min as well as print out all students with percentage grades and print out all students with letter grades. I've gotten pretty much all of it done but when I went to compile it I get the dreaded linking errors. LNK 2005 to be specific. It says my methods are already defined and of course because it is a linking error it doesn't point me to where I need to change my code. So please spare me the migranes and help me out here... Here is my code...

    Code:
    //studentInformation.h
    #include <iostream>
    using std::cout;
    using std::cin;
    #include <string>
    using std::string;
    #include <iomanip>
    using std::setw;
    class studentInformation
    {
    	public:
    		
    		void add(string, string, double);
    		void print();
    		void printGrades();
    		double highestScore();
    		double lowestScore();
    		double avgScore();
    	private:
    		string firstName[40];
    		string lastName[40];
    		double finalScore[40];
    		int arrayNum;
    
    };
    Code:
    //studentInformation.cpp
    #include "studentInformation.h"
    
    int arrayNum = 0;
    
    
    void studentInformation::add(string fName, string lName, double fScore)
    {
    	firstName[arrayNum]  = fName;
    	lastName[arrayNum]   = lName;
    	finalScore[arrayNum] = fScore;
    	arrayNum++;
    }
    
    void studentInformation::print()
    {
    	std::cout << "First Name" << setw(13) << "Last Name" << setw(13) << "Final Score\n\n";
    	for(int i = arrayNum; i < 0; i--)
    	{
    		std::cout << firstName[i] << setw(13) << lastName[i] << setw(13) << finalScore[i];
    	}
    }
    void studentInformation::printGrades()
    {
    	std::cout << "First Name" << setw(13) << "Last Name" << setw(13) << "Grade\n\n";
    	for(int i = arrayNum; i < 0; i--)
    	{
    		if(finalScore[i] >= 90 && finalScore[i] < 100)
    		{
    			std::cout << firstName[i] << setw(13) << lastName[i] << setw(13) << "A";
    		}
    		else if(finalScore[i] >= 80 && finalScore[i] < 90)
    		{
    			std::cout << firstName[i] << setw(13) << lastName[i] << setw(13) << "B";
    		}
    		else if(finalScore[i] >= 70 && finalScore[i] < 80)
    		{
    			std::cout << firstName[i] << setw(13) << lastName[i] << setw(13) << "C";
    		}
    		else if(finalScore[i] < 70 && finalScore[i] >= 0)
    		{
    			std::cout << firstName[i] << setw(13) << lastName[i] << setw(13) << "F";
    		}
    		else if(finalScore[i] > 100 || finalScore[i] < 0)
    		{
    			std::cout << firstName[i] << setw(13) << lastName[i] << setw(13) << "ERROR";
    		}
    	}
    }
    
    double studentInformation::highestScore()
    {
    	double highGrade = 0;
    	for(int i = arrayNum; i < 0; i--)
    	{
    		if(finalScore[i] > highGrade)
    		{
    			highGrade = finalScore[i];
    		}
    	}
    	return highGrade;
    }
    double studentInformation::lowestScore()
    {
    	double lowGrade = 100;
    	for(int i = arrayNum; i >= 0; i--)
    	{
    		
    		if(finalScore[i] < lowGrade)
    		{
    			lowGrade = finalScore[i];
    		}
    	}
    	return lowGrade;
    }
    double studentInformation::avgScore()
    {
    	double totalScore = 0;
    	for(int i = arrayNum; i<0; i--)
    	{
    		totalScore = finalScore[i] + totalScore;
    	}
    	double aScore = totalScore/(arrayNum + 1);
    	return aScore;
    }
    Code:
    //lab1.cpp
    #include "studentInformation.cpp"
    int stop = 1;
    string lName;
    string fName;
    double fScore;
    int main()
    {
    	studentInformation CIS2152;
    	while(stop)
    	{
    		cout << "Please enter student's first name, last name "
    			<< "each seperated by line breaks (if you are finished type "
    			<< "exit for a first name): ";
    		cin >> fName;
    		cin >> lName;
    		cin >> fScore;
    		if(fName!="exit")
    		{
    			CIS2152.add(fName,lName,fScore);
    		}
    		else
    		{
    			stop = 0;
    		}
    	}
    	int input = 1;
    	while(input)
    	{
    		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"
    			 << "0 : Exit The Program\n\n"
    			 << "Please Enter Your Choice: ";
    		cin  >> input;
    		switch(input)
    		{
    			case 1:
    			{
    				CIS2152.print();
    				break;
    			}
    			case 2:
    			{
    				CIS2152.printGrades();
    				break;
    			}
    			case 3:
    			{
    				cout << "The highest final score is: " << CIS2152.highestScore();
    				break;
    			}
    			case 4:
    			{
    				cout << "The lowest final score is: "  << CIS2152.lowestScore();
    				break;
    			}
    			case 5:
    			{
    				cout << "The class average is: "       << CIS2152.avgScore();
    				break;
    			}
    			default:
    			{
    				input = 0;
    			}
    		}
    	}
    
    	return 0;
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Code:
    #include "studentInformation.cpp"
    This is wrong. Never include a .cpp file. Include the corresponding .h file instead and be sure both .cpp files are compiled, either through your IDE, makefile, or manually when you compile at the command line.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    15
    I am amazed at how often it is something so simple. Thanks a lot!

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Indeed.

    BTW, it's nice to see someone ask a legitimate question regarding homework. Thanks for actually doing the work and asking help where it was really needed.

    And, of course, welcome to cboard.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 03:39 PM
  2. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 03:16 PM
  3. Homework
    By kermi3 in forum Windows Programming
    Replies: 5
    Last Post: 09-15-2001, 11:48 AM
  4. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 01:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21