I am trying to sort a vector from an array. Right now, I am having difficulty saving the vector from the struct array (numArr). I have commented out portions that seem semi funtional; although, I cannot get the vector to output the sorted numbers to each contestants name where they belong, sorted. I am thinking that maybe a sort function would be easier, though I have not had the need to write such before when sorting an array. Can someone who is very familiar with the logic of sorting please help me? This program took a bit of thought and I am at the last stretch of it. All I need to do is sort the array (rivals.numArr).
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
|
#include <iostream>
#include <fstream>
#include <array>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;
struct file_cab
{
char name[10];
double num;
double numArr[9];
//vector <double> vecX[9];
}rivals[7];
int main ()
{
ifstream read;
string file=("scores.txt");
read.open(file.c_str());
int numNum=0;
double scoreB=0.0;
int x;
int y;
//vector<double>::const_iterator z;
for(x=0; x<100; x++)
{
read>>numNum;
if (numNum>0)
{
cout<<"Number of Contestants: "<<numNum;
cout<<endl;
break;
}
}
for (x=0; x<numNum; x++)
{
read>>rivals[x].name>>rivals[x].num;
cout<<'\n'<<rivals[x].name<< " "<<rivals[x].num<<" ";
scoreB=0.0;
for (y=0; y<9; y++)
{
read>>rivals[y].numArr[y];
scoreB+=rivals[y].numArr[y];
/*rivals[x].vecX[x].push_back(rivals[x].numArr[x]);
sort(rivals[x].vecX[x].begin(), rivals[x].vecX[x].end());*/
cout<<rivals[y].numArr[y]<<" ";
}
scoreB=scoreB*rivals[x].num;
cout<<'\n'<<"Score for "<<rivals[x].name<<": "<<scoreB;
cout<<endl;
}
cout<<endl;
system("pause");
return 0;
}
|
Here is the text file I am using...("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