First, I would like to thank Chervil and Yay295 for helping me get this far with a program that has otherwise made me feel defeated at ever trying to become a programmer. I have referenced the templates for finding the largest or highest value of an array; however when working with vector objects I am stuck... again. How might one go about comparing two objects member values (e.g. rivals[x].score)? My goal and final piece of this program is to have the winner or the rival object with the highest score to be assigned to a variable so that I can output it (e.g. rivals[variable].name<<" has won the competition with the score of "<<rivals[variable].score;). I have tried max_element and cannot get it to work. I could write an entirely too long if statement, though that leaves me feeling like I have not learned anything more. Please, if anyone could give me an idea of the algorithm or logic needed to complete this, I would be most grateful.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
struct file_cab
{
string name;
double num, numArr[9];
double score;
};
int main ( )
{
string file = "scores.txt";
ifstream read (file);
int numNum, x, y;
vector<file_cab>::iterator z;
read >> numNum;
cout << "Number of Contestants: " << numNum << '\n';
vector<file_cab> rivals (numNum);
for (x=0; x<numNum;++x)
{
read >> rivals[x].name >> rivals[x].num;
cout << '\n' << rivals[x].name << ' ' << rivals[x].num << ' ';
double scoreB=0;
for (y=0;y<9;++y )
{
read >> rivals[x].numArr[y];
scoreB += rivals[x].numArr[y];
}
double min=rivals[x].numArr[0];
double max=rivals[x].numArr[0];
for (int i=0; i<9; ++i)
{
if(rivals[x].numArr[i]<min)
min=rivals[x].numArr[i];
if(rivals[x].numArr[i]>max)
max=rivals[x].numArr[i];
}
scoreB=scoreB-min;
scoreB=scoreB-max;
rivals[x].score=scoreB;
rivals[x].score=rivals[x].score * rivals[x].num;
sort (rivals[x].numArr, rivals[x].numArr + 9);
for (y=0; y<9; ++y ) cout << rivals[x].numArr[y] << ' ';
cout<<"\nScore for "<<rivals[x].name<<": "<<rivals[x].score<<'\n'<<min<<" "<<max<<" ";
}
cout << '\n';
system("pause");
return 0;
}
|
the text file below is what I am using with this program: "scores.txt"
7
Anne 2.0 8.5 8.5 9.0 9.0 9.0 9.5 8.5 8.0 9.5
Sarah 1.6 7.5 8.5 8.0 8.0 7.0 9.0 8.5 8.5 8.0
Deborah 2.3 9.0 9.0 9.5 10.0 10.0 9.5 9.5 9.5 9.5
Kathryn 2.4 9.0 9.0 9.0 9.5 9.5 9.5 9.0 8.0 8.5
Martha 2.7 9.0 9.0 9.5 9.5 9.0 8.5 8.5 8.5 9.5
Elizabeth 2.9 8.0 8.0 7.5 8.5 8.5 8.0 8.0 7.5 8.5
Tina 2.5 8.5 8.5 8.5 8.5 8.5 8.5 8.5 8.5 8.5