0

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.

3
  • int num = num; what is this ? You can just sort this one Test* tests = new Test[num]; instead of having sortArray. Try changing the selectionSort() to void selectionSort(int num, Test* tests) and then work on your sort logic with that. Commented Mar 16, 2015 at 9:43
  • What's your compiler and working environment ? If you have c++11 compliant compiler then @Lightness Races in Orbit 's answer is the way to handle objects in container and sort them. Commented Mar 16, 2015 at 10:05
  • @Jagannath: My idea behind using the sortArray was to protect the original array from any possible access/ changes. But you're right. Omitting it would probably be a lot easier. Commented Mar 17, 2015 at 6:19

2 Answers 2

2

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;
       }
    );
}
Sign up to request clarification or add additional context in comments.

3 Comments

I'm going to go out on a limb here : OP is doing school work => his professor wants him to implement selection sort. +1 anyway. it's good reference material.
@FélixCantournet: If the OP has constraints then he should state them, clearly, in the question.
@FélixCantournet: Yeah, I messed up and forgot to mention that I need to use the selection sort.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.