我收到"无效类型 int[int]...",但这毫无意义

I'm Receiving 'Invalid Type Int[Int]...', But It's Making No Sense

本文关键字:int 毫无意义 无效 类型      更新时间:2023-10-16

我需要完成一个程序,其中您的数组包含十个测试分数。然后,您将该数组通过一个函数传递,该函数将显示十个中的最高分数。

   #include <iostream>
   using namespace std;

   void showValue(int);//Function prototype
   int main()
 {
    const int results = 10;
    double tscores[results];
    int result;
    //Get the ten test scores from the user.
    cout << "Please enter the ten test scores." << endl;
    cin >> tscores[0];
    cin >> tscores[1];
    cin >> tscores[2];
    cin >> tscores[3];
    cin >> tscores[4];
    cin >> tscores[5];
    cin >> tscores[6];
    cin >> tscores[7];
    cin >> tscores[8];
    cin >> tscores[9];
    //Now, the program should pass the arry through a
    //function to find the highest test score.
    int max = 0;
    for (int i = 0; i < results; i++)
      {
      if (result[0] > max)
            {
                 max = result[0];
            }
      }
 cout << "The highest score is " << max << endl;
 return 0;
}

这些是我得到的错误:

    Prog4r.cpp: In function ‘int main()’:
    Prog4r.cpp:37:16: error: invalid types ‘int[int]’ for array subscript
    Prog4r.cpp:39:19: error: invalid types ‘int[int]’ for array subscript

我似乎无法解决问题。任何帮助将不胜感激。

在下面的代码中,您尝试从名为 result 的变量访问索引,该变量不是数组。

if( result[0] > max )
{
    max = result[0];
}

这里有一些错误,

  1. 您想要访问名为 tscores 的数组变量。
  2. 您希望使用循环变量而不是 0。
  3. 变量max的类型应与 tscores 相同。
double max = 0.0;
for( int i = 0; i < results; ++i )
{
    if ( tscores[i] > max )
    {
        max = tscores[i];
    }
}