Thread: two dot operator problem HELP!

  1. #1
    UpTooLate
    Join Date
    Feb 2008
    Location
    New York
    Posts
    32

    two dot operator problem HELP!

    This program runs correct until I get to the last function with the two dot-operators in the main. The program ends without giving me what I am asking for. I want it to return the best student and then the student's name. Can someone tell me how to fix this?
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class Grade
    {
    	public:
    		void enterInfo();
    		double getWeight();
    		double getWeightedScore();
    	
    	
    	private:
    		string ID;
    		double mWeight;  
    		double mScore;
    		
    };
    
    class Student
    {
    	public:
    		void enterInfo();
    		bool AreGoodWeights();
    		void showInfo();
    		string showName();
    		double addWeightedScoresTogether();
    		
    			
    	
    	private:
    		string aName;
    		Grade mGrade1; 
    		Grade mGrade2;
    		
    	
    };
    
    class Seminar
    {
    	public:
    		void enterTheStudentsInfo();
    		void showTheFinalGrades();
    		Student getBestStudent();
    		
    	
    	private:
    		Student mStudent1;
    		Student mStudent2;
    		Student mStudent3;
    };
    
    
    int main()
    {
    	Seminar EngLit;
    	EngLit.enterTheStudentsInfo();
    	EngLit.showTheFinalGrades();
    	
    	cout << "The student with the highest final grade is: ";
    	EngLit.getBestStudent().showName();
    	
    	
    	return 0;
    }
    
    void Student::enterInfo()
    {
    	cout << "Please enter your name: ";
    	cin >> aName;
    	cout << endl;
    	tryagain:
    	mGrade1.enterInfo();
    	mGrade2.enterInfo();
    	if (mGrade1.getWeight() + mGrade2.getWeight() == 1.0)
    	{
    	}
    	else
    	{
    		cout << "Your weights don't match up. Let's try this again." << endl;
    		goto tryagain;
    	}
    }
    
    void Grade::enterInfo()
    {
    	cout << "Enter the ID of this test: ";
    	cin >> ID;
    	cout << endl;
    	cout << "What is the weight of this test?: ";
    	cin >> mWeight; // this will enter weight BEFORE it is in decimal form
    	cout << endl;
    	cout << "What is the score for this test?: ";
    	cin >> mScore;
    	cout << endl;
    }
    	
    double Grade::getWeight()
    {
    	return mWeight;
    }
    
    bool Student::AreGoodWeights()
    {
    
    	if (mGrade1.getWeight() + mGrade2.getWeight() == 1.0) // if the weights (in decimal form) are equal to 1.0
    	{
    		return true;                                 // then continue the program
    	}
    	else                   // otherwise
    	{
    		return false; // end the program
    	}
    }
    
    double Grade::getWeightedScore()
    {
    	return mScore * mWeight;
    	
    }
    	
    
    	
    void Seminar::enterTheStudentsInfo()
    {
    	mStudent1.enterInfo();
    	mStudent2.enterInfo();
    	mStudent3.enterInfo();
    }
    
    string Student::showName()
    {
    	return aName;
    }
    
    double Student::addWeightedScoresTogether()
    {
    	return mGrade1.getWeightedScore() + mGrade2.getWeightedScore();
    }
    
    void Seminar::showTheFinalGrades()
    {
    	cout << "Final Grades:" << endl;
    	cout << mStudent1.showName() << " : " << mStudent1.addWeightedScoresTogether() << endl;
    	cout << mStudent2.showName() << " : " << mStudent2.addWeightedScoresTogether() << endl;
    	cout << mStudent3.showName() << " : " << mStudent3.addWeightedScoresTogether() << endl;
    }
    
    Student Seminar::getBestStudent()
    {
    	if (mStudent1.addWeightedScoresTogether() > mStudent2.addWeightedScoresTogether() &&
    	mStudent1.addWeightedScoresTogether() > mStudent3.addWeightedScoresTogether())
    	{
    		return mStudent1;
    	}
    	else if (mStudent2.addWeightedScoresTogether() > mStudent1.addWeightedScoresTogether()
    	&& mStudent2.addWeightedScoresTogether() > mStudent3.addWeightedScoresTogether())
    	{
    		return mStudent2;
    	}
    	else if (mStudent3.addWeightedScoresTogether() > mStudent1.addWeightedScoresTogether()
    	&& mStudent3.addWeightedScoresTogether() > mStudent2.addWeightedScoresTogether())
    	{
    		return mStudent3;
    	}
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> The program ends without giving me what I am asking for.
    Perhaps the console window is just closing before you can read it? There is FAQ with detailed information, but adding this above the return 0 in main might solve the problem:
    Code:
    cin.ignore();
    cin.get();

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I see that Student::ShowName returns the name, but you don't do anything with the returned string...
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    anon is right, look how you use showName() in the showTheFinalGrades() function. You send the result to cout. But in main, you do nothing with the result.

    Actually, the showName function should have a different name, maybe getName(). If you want it to be showName, you should add the cout code to that function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Red Dot in the middle of screen?
    By kennny2004 in forum C++ Programming
    Replies: 8
    Last Post: 10-20-2006, 02:15 PM
  2. Draw dot by dot?
    By fiska in forum C# Programming
    Replies: 3
    Last Post: 10-11-2006, 09:38 AM
  3. need help making a dot bounce up and down y axis in this prog
    By redwing26 in forum Game Programming
    Replies: 10
    Last Post: 08-05-2006, 12:48 PM
  4. Drag dot over grid
    By aod in forum C# Programming
    Replies: 4
    Last Post: 05-23-2006, 11:51 AM
  5. Simple program i cant get (dot moving)
    By Lupusk9 in forum C++ Programming
    Replies: 4
    Last Post: 09-14-2004, 08:04 PM