Reputation: 1
I have written a basic program that collects student names and answers and automatically scores them. At the end I'd like to sort the scores in descending order with the corresponding names. I understand how to sort the scores but not in combination with the student names. This is what I have so far.
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//declare variables
char choice;
string studentName;
vector<char> answers;
vector<string> names;
int getStudents();
int getQuestions();
//calls function to get number of questions
int questions = getQuestions();
//Get answers
for (int i = 0; i < questions; ++i) {
cout << "What is the answer for question " << i + 1 << endl;
cin >> choice;
answers.push_back(choice);
}
//Get number of students
int students = getStudents();
//Get student names
for (int i = 0; i < students; i++) {
cout << "Student " << i + 1 << ", what is your name?" << endl;
cin >> studentName;
names.push_back(studentName);
}
int score = 0;
char studentAnswer;
vector<char> userAnswer;
vector<float> finalScore;
//gets student answers
for (int i = 0; i < students; i++) {
score = 0;
for (int j = 0; j < questions; j++) {
cout << names[i] << ", what is your answer for question " << j + 1 << "?" << endl;
cin >> studentAnswer;
userAnswer.push_back(studentAnswer);
if (userAnswer[i*questions+j] == answers[j])
score = score + 1;
}
finalScore.push_back(score);
}
//outputs scores
std::sort(finalScore.begin(), finalScore.end());
for (int i = 0; i < students; i++) {
cout << names[i] << " scored " << finalScore[i] << " out of " << questions <<
" or " << (finalScore[i] / questions) * 100 << "%" << endl;
}
system("pause");
return 0;
}
//function to get number of questions
int getQuestions()
{
int questions;
cout << "How many questions are there?" << endl;
cin >> questions;
return questions;
}
//function to get number of students
int getStudents()
{
int students;
cout << "How many students are there?" << endl;
cin >> students;
return students;
}
Right now, it sorts the scores in descending orders but the names that are output with the scores are incorrect.
Upvotes: 0
Views: 287
Reputation: 217
To keep the spirit of what you are doing, you can make a vector of pairs, here is a code example:
#include<vector>
#include<string>
#include<iostream>
#include<algorithm>
int main()
{
std::vector< std::pair<double,std::string> > my_student_list; // first element is the grade, second is the name
std::pair<double,std::string> student1(2.,"name1"),student2(2.,"name2"),student3(1.,"name3");
my_student_list.push_back(student1);
my_student_list.push_back(student2);
my_student_list.push_back(student3);
std::sort(my_student_list.begin(),my_student_list.end()); // sort by grade
// loop over the vector to print the sorted list
for(std::vector< std::pair<double,std::string> >::iterator iterator = my_student_list.begin(); iterator != my_student_list.end(); iterator++){
// iterator->second is the name, and iterator->first the associated grade
std::cout << iterator->second << " " << iterator->first << std::endl;
}
return 0;
}
Upvotes: 0
Reputation: 1811
You need to somehow 'connect' student name with his/her score. A very easy and straightforward way is to create a struct, i.e.
typedef struct Student
{
string student_name_;
float student_score_;
} Student;
Next you need to define a Compare function (look here for an example: http://www.cplusplus.com/reference/algorithm/sort/) so that you can use std:sort
to sort a vector of Student
s. Your Compare function could like like this:
bool myCompareFunction ( Student a, Student b)
{
return (a.student_score_ < b.student_score_);
}
Upvotes: 1