Problems with Comparing by Name & Comparing by Score

Objective:

read a file containing a list of students' names followed by their test scores. Define a structure to store name and score. Instead of just a vector of scores, you will now need a vector of structures. Each element in the vector will have the student's name and the average of his or her score. Your program needs to be able to sort the vector by both name and score, so you need to implement two different compare functions to pass to sort:

sort (students.begin(), students.end(), compareByName);

sort (students.begin(), students.end(), compareByScore);

sort will use the function that you provide to sort the data.

This is how function compareByName should look like:

bool compareByName(Student stu1, Student stu2) { return stu1.name < stu2.name) }

Display the scores sorted by name and score, the class average and median. No matter how the data is sorted, you should print both name and score. For example, students sorted by name:

Ana 73

Daniel 66



Problem:
I've created the compareByName and compareByScore, but my compiler says that
my compaireByScore sort has "invalid arguments." Am I missing something or
did I do something completely wrong.



#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

struct Student{
string name;
double score;
};

/******************************************************************

This function reads through the text file and calculates the class
average

*******************************************************************/
double classAverage(vector<double> numbers)
{
double averages;
int total = 1;
int g = 0;
int size = 110;
int start = 0;
Student student;

while (start < 110)
{
for(int i = g; i < size; i++)
{
student.name = g;

for (int j = 1; j < size; j++){
total += numbers[i];
}
}

averages = total / 10;

start = size;
size += 10;
g += 10;
total = 0;
}
return (averages/10);
}


/******************************************************************

This function reads through the text file and prints out the median of
the text file.

*******************************************************************/
double classMedian (vector<double> numbers)
{
int median;

std::sort (numbers.begin(), numbers.end());

if( numbers.size() % 2 == 0){
median = (numbers[numbers.size() / 2] + numbers[(numbers.size() / 2) - 1]) / 2;
} else {
median = (numbers[numbers.size() / 2]);
}

return(median);
}

bool compareByName (Student stu1, Student stu2){

std::sort (stu1.name.begin(), stu1.name.end());
std::sort (stu2.name.begin(), stu2.name.end());
int i = 0;


if (stu1[i] >= 65 && stu1[i] <= 90) {

cout << stu1[i];
}

return stu1.name < stu2.name;
}

bool compareByScore (Student stu1, Student stu2){

std::sort (stu1.score.begin(), stu1.score.end());
std::sort (stu2.score.begin(), stu2.score.end());

return stu1.score < stu2.score;
}

void printOut (vector<double>fileNumbers)
{
for (double score : fileNumbers)
{

}

cout << "\nThe class average is:" << classAverage(fileNumbers);

cout << endl;

cout << "\nThe class median is: " << classMedian(fileNumbers);

cout << endl;
}

int main()
{
ifstream fin;
string userfile;
vector<double> numbers;
vector<double> fileNumbers;
int q;

vector<Student> compareByName;
vector<Student> compareByScore;
Student student;

fin.open ("lab2.txt");
if (fin.fail())
{
while(fin.fail())
{
cout << "Input file failed to open.\n";

cout << "\nPlease enter a file name/path";

cin >> userfile;

fin.open (userfile);

if(fin)
{
while(fin >> q)
{
fileNumbers.push_back(q);
}
}

classAverage(fileNumbers);

classMedian(fileNumbers);

printOut(fileNumbers);

compareByName(student.name);

compareByScore(student.score);

std::sort (student.name.begin(), student.name.end(), compareByName);
//std::sort (student.score.begin(), student.score.end(), compareByScore);

fin.close();
}
fin.open (userfile);
}

if(fin)
{
while(fin >> q)
{
fileNumbers.push_back(q);
}
}

classAverage(fileNumbers);

classMedian(fileNumbers);

printOut(fileNumbers);

std::sort (student.name.begin(), student.name.end(), compareByName);
//std::sort (student.score.begin(), student.score.end(), compareByScore);

fin.close();
return 0;
}
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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

struct student
{
    std::string name ;
    int score = 0 ;
};

bool cmp_name( const student& a, const student& b ) { return a.name < b.name ; }
bool cmp_score( const student& a, const student& b ) { return a.score < b.score ; }

void print( const std::vector<student>& student_list )
{
    // range based loop: http://www.stroustrup.com/C++11FAQ.html#for
    for( const student& st : student_list ) std::cout << st.name << ' ' << st.score << '\n' ;
    std::cout << "\n\n" ;
}

int main()
{
    std::vector<student> students { { "athos", 24 }, { "porthos", 82 },
                                    { "aramis", 70 }, { "d'Artagnan", 37 } };
    print(students) ;

    std::cout << "sorted on name:\n---------------\n" ;
    std::sort( students.begin(), students.end(), cmp_name ) ;
    print(students) ;

    std::cout << "sorted on score:\n---------------\n" ;
    std::sort( students.begin(), students.end(), cmp_score ) ;
    print(students) ;
}

http://coliru.stacked-crooked.com/a/21827c6aa6a64ffb
Thank you very much!
Topic archived. No new replies allowed.