C.B
C.B

Reputation: 17

Struggling to sort an array of structs using an array of pointers. C++

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.

Upvotes: 0

Views: 95

Answers (2)

David Haim
David Haim

Reputation: 26496

the fact that there is struct with just 1 integer does not makes it an integer , it's still a struct. in order to compare objects (which in fact , what a struct produces) you need to overload the comparison operator , and in your case > operator.

Upvotes: 0

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385174

Sorting a container of objects by members of those objects is much easier than this.

#include <vector>
#include <algorithm>
#include <string>

struct Test
{
    Test()
       : score(0)
       , num(0)
    {}

    std::string name;
    int score;
    int num;
};

int main()
{
    const unsigned int num = 5;  // user input in your case
    std::vector<Test> v(num);

    //!! Assign values to items in `v`

    std::sort(
       v.begin(),
       v.end(),
       [](const Test& lhs, const Test& rhs) {
          return lhs.num < rhs.num;
       }
    );
}

Upvotes: 2

Related Questions