How can I use STL sort() on a pointer to an array? (C++)

  Kiến thức lập trình

I’m doing an assignment for a class right now, which asks us to create a new array based on user input and then use STL sort from a pointer of the array. I am getting errors while doing that, however.

here is my full code:

#include <iostream>
#include <iomanip>
//#include <string>
#include <cctype>
//#include <cmath>
//#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

int numberOfScores;
double *testScore; // Pointer to dynamically allocated array of scores
double ave; // Average score

void getScores();

int main(){
    getScores();
    sort(testScore.begin(), testScore.end());
    cout << showpoint << fixed << setprecision(2);
    for (int i = 0; i < sizeof(testScore); i++){
        cout << *(testScore + i) << endl;
    }
    return(0);
}

void getScores(){
    cout << "How many scores are you entering?n"; 
    cin >> numberOfScores;
    while (numberOfScores <= 0){
        cout << "please enter a valid input n";
        cin.clear();
        cin.ignore(1000,'n');
        cin >> numberOfScores;
    }

    testScore = new double[numberOfScores];
    for (int i = 0; i < numberOfScores; i++){
        cout << "Please enter a score:n";
        cin >> testScore[i];
        while (testScore[i] < 0 || testScore[i] > 100){
            cout << "please enter a valid input n";
            cin.clear();
            cin.ignore(1000,'n');
            cin >> testScore[i];
        }  
    }
}

the error I get from using sort():

expression must have class type but it has type “double *”

New contributor

Ivan Sepulveda is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT