【问题标题】:Is there a problem with this code? I can't get it to work. Help me PLEASE [closed]这段代码有问题吗?我无法让它工作。请帮帮我[关闭]
【发布时间】:2021-04-10 15:17:42
【问题描述】:

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

#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;
}

我从这段代码中得到的只是编译器错误。 有没有人有什么建议?请和谢谢。

【问题讨论】:

  • 什么是编译器错误?
  • 我试着四处搜索,但找不到我需要的东西。
  • 您需要提取 minimal reproducible example 并将其与错误消息一起包含在您的问题中。作为这里的新用户,也可以使用tour 并阅读How to Ask

标签: c++ function pointers


【解决方案1】:

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

宣言:

void sortArr(int [], int);

定义:

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

如您所见,定义说sortArr() 接受ints 和int 数组的参数。但是,您的定义说sortArr() 采用int*s 和int 的数组的参数。编译器得出结论,它们一定是不同的函数。

当编译器尝试编译时,它与您的声明一致,但找不到定义。你可以按照其他人所做的来修复它,我也会在这里列出:

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

void sortArr(int*, int);

int[] 可以转换为int*,所以这就是我们需要的。

接下来,转到sortArr() 的定义,并更改它。 (cmets解释原因)


void sortArr(int* arr, int SIZE) //we only need a pointer, as it is basically the same thing as an array
{
    int startScan, minIndex;
    int minElem; //if minElem was a pointer, it would get overwritten every time by arr, so use a regular int

    for (startScan = 0; startScan < (SIZE - 1); startScan++)
    {
        minIndex = startScan;
        minElem = arr[startScan];
        for (int index = startScan + 1; index < SIZE; index++)
        {
            if (arr[index]/*No need for dereferencer here, because indexing already dereferences.*/ < 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] << " "; //Again, no dereferencing here because we are using the [] operator
    cout << endl;
}

我已经针对多个案例测试了此代码,并且它有效。希望这会有所帮助!

【讨论】:

  • 谢谢!它有效!
【解决方案2】:

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

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

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

【讨论】:

    【解决方案3】:

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

    void sortArr(int* arr, int SIZE) {
    ...
    }
    

    还有更精细的方法来检查输入错误。随心所欲地使用

    while(!(cin >> size) || size < 0 || size == 0)
    {
    // Error message
    cin.clear();
    // A huge constant
    cin.ignore(999,'\n');
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-15
      • 2021-08-12
      • 1970-01-01
      • 1970-01-01
      • 2021-01-04
      • 2021-07-30
      • 2015-03-31
      • 1970-01-01
      相关资源
      最近更新 更多