C++ 这个代码有问题吗?我能';我不能让它工作。请帮帮我

C++ 这个代码有问题吗?我能';我不能让它工作。请帮帮我,c++,function,pointers,C++,Function,Pointers,其目的是对用户输入的分数进行排序、显示和平均。 我还没弄明白。 如果你已经解决了这个问题,请帮忙。 到目前为止,我已经尝试过这个代码,但它不起作用 #include<iostream> using namespace std; void sortArr(int [], int); void getAvg(const int [], int); int main() { int *scorePtr = nullptr; int size; cout <

其目的是对用户输入的分数进行排序、显示和平均。 我还没弄明白。 如果你已经解决了这个问题,请帮忙。 到目前为止,我已经尝试过这个代码,但它不起作用

#include<iostream>
using namespace std;

void sortArr(int [], int);
void getAvg(const int [], int);

int main()
{
    int *scorePtr = nullptr;
    int size;

    cout << "Enter the number of test scores: ";
    cin >> size;
    while(size < 0)
    {
        cout << "Invalid entry. Enter the number of scores: ";
        cin >> size;
    }
    if(size == 0)
    {
        cout << "No scores. Terminating program...";
        return 0;
    }

    scorePtr = new int[size];

    cout << "Enter the scores earned on each test: ";
    for (int index = 0; index < size; index++)
    {
        cin >> scorePtr[index];
        while(scorePtr[index] < 0)
        {
            cout << "Entry not valid. Enter score #" << (index + 1) << ": ";
            cin >> scorePtr[index];
        }
    }

    sortArr(scorePtr, size);
    getAvg(scorePtr, size);

    return 0;
}

void sortArr(int *arr[], int SIZE)
{
    int startScan, minIndex;
    int *minElem;

    for(startScan = 0; startScan < (SIZE - 1); startScan++)
    {
        minIndex = startScan;
        minElem = arr[startScan];
        for(int index = startScan + 1; index < SIZE; index++)
        {
            if(*(arr[index]) < *minElem)
            {
                minElem = arr[index];
                minIndex = index;
            }
        }
        arr[minIndex] = arr[startScan];
        arr[startScan] = minElem;
    }
    cout << "Here are the scores you entered, sorted in ascending order: " << endl;
    for(int count = 0; count < SIZE; count++)
        cout << *(arr[count]) << " ";
    cout << endl;
}

void getAvg(const int arr[], int Size)
{
    double total = 0.0;
    double avg = 0.0;
    for (int index = 0; index < Size; index++)
    {
        total += arr[index];
    }
    avg = (total/Size);
    cout << "Your average score is: " << avg << endl;
}
#包括
使用名称空间std;
无效排序器(int[],int);
void getAvg(常量int[],int);
int main()
{
int*scorePtr=nullptr;
整数大小;
大小;
而(尺寸<0)
{
大小;
}
如果(大小==0)
{
cout scorePtr[索引];
while(记分器[索引]<0)
{

cout您将
sortArr
定义为将指向int的指针数组作为其参数,但您向它传递了一个实际的
int
数组。听起来后者才是您真正想要的,因此您需要重写
sortArr
以正确处理
int
数组,首先将定义更改为

void sortArr(int arr[], int SIZE) {
...
}

没有
*
。这也意味着该函数中的大部分
*
解引用都将被删除。

若要在Nate的答案上添加,请将指向数组的指针传递给
Sortar
。编译器将数组“转换”为指针

void sortArr(int* arr, int SIZE) {
...
}
还有更多更好的方法来检查输入错误

while(!(cin >> size) || size < 0 || size == 0)
{
// Error message
cin.clear();
// A huge constant
cin.ignore(999,'\n');
}
while(!(cin>>大小)| |大小<0 | |大小==0)
{
//错误消息
cin.clear();
//巨大常数
cin.忽略(999,“\n”);
}

正如其他人所指出的,您的
sortArr()声明与定义不匹配

宣言:

void sortArr(int[],int);
定义:

void sortArr(int*arr[],int SIZE){…}
如您所见,定义中说,
sortArr()
接受
int
s数组的参数,以及
int
。然而,您的定义中说,
sortArr()
接受
int*
数组的参数和
int
。编译器得出结论,它们必须是不同的函数

当编译器尝试编译时,它与您的声明一致,但找不到定义。您可以像其他人一样修复它,我也将在此处列出:

首先,将
sortArr()
的声明更改为

void sortArr(int*,int);
int[]
可以转换为
int*
,所以这就是我们需要的

接下来,转到
sortArr()
的定义,并对其进行更改。(注释解释了原因)


void sortArr(int*arr,int SIZE)//我们只需要一个指针,因为它与数组基本相同
{
int startScan,minIndex;
int minElem;//如果minElem是一个指针,它每次都会被arr覆盖,所以使用常规的int
对于(startScan=0;startScan<(大小-1);startScan++)
{
minIndex=startScan;
minElem=arr[startScan];
对于(int index=startScan+1;indexcout什么是编译器错误?我尝试四处搜索,但找不到我需要的。您需要提取一个,并将其与错误消息一起包含在问题中。作为这里的新用户,也请阅读。谢谢!它很有效!