I've been trying to think of the easiest way to calculate the average and display the results.
The two outputs that I am missing would be the average part of the program. When the program is ran, it should display
"Danika Patrick averaged a score of 120 in 11 minutes and 10 seconds."
"Jeff Gordon averaged a score of 105 in 11 minutes and 0 seconds."
+ the int main cout that is already included in my code.
Any suggestions on what you guys would do would work. I'm supposed to follow the step below:
2. USING THE OVERLOADED + OPERATOR, find the average score and time for each player and print out which
performer had the lowest total time, and which performer had the highest total score.
#include <iostream>
#include <string>
using namespace std;
class RacingData
{
public:
RacingData();
RacingData(string, int, int, int);
void setData(string newName, int newScore, int newMinutes, int newSeconds);
void getData(string &_name, int &_score, int &_minutes, int &_seconds);
void printData() const;
RacingData operator+(RacingData &other) const;
private:
string name;
int score, minutes, seconds;
};
RacingData::RacingData() { name = " "; score = minutes = seconds = 0; }
RacingData::RacingData(string newName, int newScore, int newMinutes, int newSeconds) { name = newName; score = newScore; minutes = newMinutes; seconds = newSeconds; }
void RacingData::setData(string newName, int newScore, int newMinutes, int newSeconds)
{
name = newName;
score = newScore;
minutes = newMinutes;
seconds = newSeconds;
}
void RacingData::getData(string &_name, int &_score, int &_minutes, int &_seconds)
{
_name = name;
_score = score;
_minutes = minutes;
_seconds = seconds;
}
void RacingData::printData() const
{
cout << name << " scored a total of " << score << " points in a total of " << minutes << " minutes and " << seconds << " seconds." << endl;
}
RacingData RacingData::operator+(RacingData &other) const
{
string dummyString;//holds a name of the obj
int otherScore, otherMinutes, otherSeconds; // holds values of all 3 data members of object other
RacingData newObject; //holds data
other.getData(dummyString, otherScore, otherMinutes, otherSeconds);//gets all 3 data members
newObject.setData(name, score + otherScore, minutes + otherMinutes, seconds + otherSeconds);
//add the total of the implicit obj to the explicit obj other and save the total to obj newObject
//make sure any other excessive minutes gets rolled over to hours
newObject.getData(dummyString, otherScore, otherMinutes, otherSeconds);
if (otherSeconds > 59)
{
otherMinutes = otherMinutes + (otherSeconds / 60);
otherSeconds = otherSeconds % 60;
newObject.setData(name, otherScore, otherMinutes, otherSeconds);
}
return newObject; //returns to total
}
int main()
{
//declares all six objects
RacingData car1, car2, car3, car4, car5, car6;
//declare two totals and winner objects
RacingData danikaTotalData, jeffTotalData, winner;
//sets values
car1.setData("Danika Patrick", 185, 11, 20);
car2.setData("Danika Patrick", 103, 11, 30);
car3.setData("Danika Patrick", 73, 12, 40);
car4.setData("Jeff Gordon", 155, 10, 10);
car5.setData("Jeff Gordon", 127, 11, 15);
car6.setData("Jeff Gordon", 34, 12, 35);
// add the total data
danikaTotalData = car1 + car2 + car3;
jeffTotalData = car4 + car5 + car6;
//display winners accordingly
cout << "The performer with the highest score was: "; danikaTotalData.printData();
cout << "The performer with the lowest time was: "; jeffTotalData.printData();
winner;
return 0;
}