Need help with descending order scores.

Instructions:
Assume that you are reading from a data file. Each line contains a student’s last name and a test score. Write a program to print out the names and scores in descending order of test scores.

I know how to read the file. I just don't know how to make the names and scores in descending order of test scores. I also don't know how to read the file until all the names/scores are read.

here is an example, but there could be more names in the datafile, i don't know how many there will be.

Input Data

Smith 87
Jones 76
Redd 56
Green 96
Blue 73

Output

Green 96
Smith 87
Jones 76
Blue 73
Redd 56

please help me! thanks.
I would suggest to use std::forward_list with value type std::pair<std::string, size_t>.And then to apply member function sort.
Last edited on
I really have no idea what that means.
As an example

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
#include <iostream>
#include <forward_list>
#include <utility>
#include <string>

int main()
{
	std::forward_list<std::pair<std::string, size_t>> l;

	l.push_front( std::make_pair( std::string( "Smith" ), 87 ) );
	l.push_front( std::make_pair( std::string( "Jones" ), 76 ) );
	l.push_front( std::make_pair( std::string( "Redd" ), 56 ) );
	l.push_front( std::make_pair( std::string( "Green" ), 96 ) );
	l.push_front( std::make_pair( std::string( "Blue" ), 73 ) );

	l.sort( []( const std::pair<std::string, size_t> &a, 
		        const std::pair<std::string, size_t> &b )
	        {
			return ( ( a.second > b.second ) );
	        } );

	for ( const auto &p : l ) std::cout << p.first << '\t' << p.second << std::endl;
	std::cout << std::endl;
		
} 
thanks for the example. this is what i have got so far:

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
#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void selsort(int a[], int size);
void swap(string, string &s1,string &s2);
void swap (int &i1, int &i2);

int main()
{
ifstream inFile;
ofstream outFile;
inFile.open("C:beyoncefan\\My Documents\\C++\\orderscores.txt");
outFile.open("C:beyoncefan\\My Documents\\C++\\output13.txt");
int Score, i=0;
std::string Name;
inFile >> Name >> Score;
std::string Names[30];
int Scores[30];
std::cin >> i;
while(std::cin >> Name >> Score) {
    Names[i] = Name;
    Scores[i] = Score;
    ++i;
}
int NumberOfStudents = i;
for(int i = 0; i < NumberOfStudents; i++) {
    for(int j = i; j < NumberOfStudents; j++) {
        if (Scores[i] < Scores[j])
        {
        std::swap(Names[i], Names[j]);
        std::swap(Scores[i], Scores[j]);
    }
}
inFile.close();
outFile.close();
system("pause");
return 0;
}}
does this look good. its not doing the output correct. I get blank output file.
Topic archived. No new replies allowed.