So I'm trying to sort an array of structs by using an array of pointers. I'm trying to sort the struct based on the int score member of the Test struct. I'm getting a bunch of errors and I think they are all related to something specific that I am doing wrong. (Or I just clearly don't have the slightest conceptual grasp about how this all works.)
Here are the errors: Screenshot of List of Errors
And here is the code:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
struct Test;
void selectionSort(int num, struct Test* sortArray[]);
int main()
{
const int NO_ERRRORS = 0;
int num, scoreIn;
string nameIn;
cout << "Please provide the number of test scores you would " << endl
<< "like to average and sort." << endl << endl
<< "Please limit your request to 5-20 tests: ";
cin >> num;
while ((num < 5) || (num > 20))
{
cout << "Invalid entry. Please enter an integer between 5-20: ";
cin >> num;
}
cout << endl;
Test* tests = new Test[num];
Test** sortArray = new Test*[num];
cout << "Please enter first names only with no spaces." << endl << endl;
for (int index = 0; index < num; index++)
{
cout << "Please enter the name of student " << (index + 1) << ": ";
cin >> nameIn;
cout << "Enter the test score: ";
cin >> scoreIn;
while (scoreIn < 0)
{
cout << "Invalid entry. Please enter a positive integer: ";
cin >> scoreIn;
}
cout << endl;
((*(tests + index)) = { nameIn, scoreIn }); //IntelliSense: no operator "=" matches operands. Operand types are Test = {..}
sortArray[index] = &tests[index];
}
selectionSort(num, sortArray);
for (int count = 0; count < num; count++)
cout << (sortArray[count]->score) << " ";
cout << endl;
for (int count = 0; count < num; count++)
cout << (sortArray[count]->name) << " ";
cout << endl;
delete[] tests;
cin.ignore(cin.rdbuf()->in_avail(), '\n');
cout << endl << "Press only the 'Enter' key to exit program: ";
cin.get();
return NO_ERRRORS;
}
void selectionSort(int num, struct Test* sortArray[])
{
int minIndex;
Test *minElem;
for (int scan = 0; scan < (num - 1); scan++)
{
minIndex = scan;
minElem = sortArray[scan];
for (int index = scan + 1; index < num; index++)
{
if ((*(sortArray[index])) < (*minElem)) //IntelliSense: no operator "<" matches operands. Operand types are Test < Test
{
minElem = sortArray[index];
minIndex = index;
}
}
sortArray[minIndex] = sortArray[scan];
sortArray[scan] = minElem;
}
}
struct Test
{
int num = num;
Test()
{
name = "";
score = 0;
}
string name;
int score;
};
Obviously I'm not asking anyone to do my work for me. But could someone maybe point me in the right direction...conceptually? Maybe point out where I go wrong first and cause the cascade of errors?
Any help very much appreciated.
Edit: I messed up and forgot to mention the constraints I am working in. 1. Must use a selection sort.
int num = num;what is this ? You can just sort this oneTest* tests = new Test[num];instead of havingsortArray. Try changing theselectionSort()tovoid selectionSort(int num, Test* tests)and then work on your sort logic with that.sortArraywas to protect the original array from any possible access/ changes. But you're right. Omitting it would probably be a lot easier.