Sorting help, PLEASE (sorting a struct array)

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
Last edited on
std::sort ( rivals.numArr, rivals.numArr + 9 );
That's a bad joke Yay295. How would you run such a sort without the object specified? (rivals[].numArr[]) is the correct syntax and it is not sorting the numbers to which each rivals scores are. . . : (
Oops, I didn't notice that rivals was an array.

1
2
for ( int i = 0; i < 7; ++i )
    sort ( rivals[i].numArr, rivals[i].numArr + 9 );


edit: I'm taking a longer look at your code. You aren't doing what I thought you were doing, so I'll update this once I figure out what you're doing.
Last edited on
That doens't work either. This sorting for a struct is much more complicated and probably needs some manual written function of a sort I'm thinking.
I don't know why you were doing some of the things you were doing, so I changed them. It works now, though it doesn't look like your code anymore.
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
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>

struct file_cab
{
    std::string name;
    double num, numArr[9];
};

int main ( )
{
    std::string file = "scores.txt";

    std::ifstream read ( file );
    
    int numNum, x, y;


    read >> numNum;
        
    std::cout << "Number of Contestants: " << numNum << '\n';


    std::vector<file_cab> rivals ( numNum );


    for ( x = 0; x < numNum; ++x ) 
    {
        read >> rivals[x].name >> rivals[x].num;

        std::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];
        }

        std::sort ( rivals[x].numArr, rivals[x].numArr + 9 );

        for ( y = 0; y < 9; ++y ) std::cout << rivals[x].numArr[y] << ' ';


        std::cout <<"\nScore for " << rivals[x].name << ": " << scoreB * rivals[x].num << '\n';
    }

    std::cout << '\n';
}
Last edited on
Thank you! This has solved more than what I was asking. The idea of creating a vector for the contestants was much better than the objects declared for the struct that I had. I realize that the x's and y's were gettng mixed up in the for loops also. The code would execute; but now I realize it was my logic that was flawing the program. What you have provided and modified is perfect.
I am still learning programming, and sometimes things just get mixed up. You made it so simple. I have much to learn.
I did not know you can use a struct in angle brackets to declare what data type the vector is (or do < > brackets stand for class?). This right here "vector<file_cab> rivals" totally blew my mind and made me feel completely obsolete when it comes to programming. How am I ever going to master this skill? How did you learn how to program?
Last edited on
Topic archived. No new replies allowed.