DISCLAIMER: I know it's not practical to use two vectors, but that's our assignment.
Here's my prompt:
In this program, we are going to input the name and score of 100 students from a file named student.txt. This file has been provided to you. You have to use two vector variables, one to store the student names, and another to store the student scores. Further, modify the selectionSort function to sort the student information based on the score in ascending order. Finally, display the sorted student information on the screen by using cout. For example, let us assume, the following is the content of the student.txt file (in this case, we have only 4 value pairs).
Jeff 77
Charles 99
Richard 67
Sina 79
Then, the output of the program would be
Charles 99
Sina 79
Jeff 77
Richard 67
Here's my code:
//Name
//This program will read and sort names and grades from a file using functions and vectors
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <vector>
using namespace std;
//Function prototype
void selectionSort(vector<int>& vector_values);
int main()
{
ifstream infile;
infile.open("student.txt");
if (infile.fail() == false)
{
vector<string> all_names;
vector<int> all_scores;
string name;
int score;
while (infile >> name >> score) // read one name and one score
{
all_names.push_back(name); // add that name to vector
all_scores.push_back(score); // add that score to vector
int count = 0;
const int max = 1;
selectionSort(all_scores);
while (count < max)
{
count++;
cout << name << " " << score << endl;
}
}
}
else
{
cout << "Could not open the file." << endl;
}
return 0;
}
void selectionSort(vector<int>& vector_values)
{
for (unsigned pass = 0; pass < vector_values.size(); pass++)
{
int minimum = vector_values[pass];
int minimum_index = pass;
for (unsigned index = pass + 1; index < vector_values.size(); index++)
{
if (minimum > vector_values[index])
{
minimum = vector_values[index];
minimum_index = index;
}
}
int temp = vector_values[minimum_index];
vector_values[minimum_index] = vector_values[pass];
vector_values[pass] = temp;
}
}
Here's my problem:
The code compiles just fine and shows the names and their corresponding score, however, it seems that my sorting function is doing absolutely nothing. The names are displaying in the same order they do in the file originally. I have placed the call in every location that I thought it would go, and nothing changed. I am now concerned that the placement of the call isn't the issue, but the entire function.