【问题标题】:Why does C++ give me this error? How to say if an array of ints is either odd or even?为什么 C++ 会给我这个错误?如何判断一个整数数组是奇数还是偶数?
【发布时间】:2015-12-07 04:45:42
【问题描述】:

这应该是用户在数组中输入整数然后调用显示它们是偶数还是奇数的函数的代码。然后它计算有多少是偶数或奇数(我还没有得到,因为我被困在这里并且我不断收到这个错误)。

“int [20]”和“int”类型的无效操作数到二进制“operator%”

#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;

void getValue(int[], int);//function prototype

int main()
{
    const int ARRAY_SIZE = 20;
    int numbers[ARRAY_SIZE];

    cout<<fixed<<showpoint<<setprecision(1);

    //get ints from user
    getValue(numbers, ARRAY_SIZE);

     if ( numbers % 2== 0 )
        // if the integer when divided by 2 has no remainder then it's even.
        cout << numbers << " is even "<<endl;
    else
        // the integer is odd
        cout << numbers << " is odd "<<endl;

    return 0;
}

void getValue(int numbers[], int ARRAY_SIZE)
{
    //loop counter
    int index;

    //get each value
    for(index = 0; index <= ARRAY_SIZE - 1; index++)
    {
        cout<< "Enter an integer " << (index + 1)<< " : ";
        cin>> numbers[index];
    }
}

【问题讨论】:

  • 那么如果用户提供“3 4 5”,程序应该打印什么?

标签: c++ arrays function


【解决方案1】:
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;

void getValue(int [], int);//function prototype

int main()
{
    const int ARRAY_SIZE = 20;
    int numbers[ARRAY_SIZE];

    cout << fixed << showpoint << setprecision(1);

    //get ints from user
    getValue(numbers, ARRAY_SIZE);

    //I added this FOR loop.
    for (int i = 0; i < ARRAY_SIZE - 1; i++)
    {
        //I also added the [i] so that the number is outputted as well
        if (numbers[i] % 2 == 0)

            // if the integer when divided by 2 has no remainder then it's even.

            // and here as well
            cout << numbers[i] << " is even "
            << endl;
        else

            // the integer is odd

            cout << numbers[i] << " is odd "
            << endl;
    }
    return 0;

}

void getValue(int numbers[], int ARRAY_SIZE)
{
    //loop counter
    int index;

    //get each value
    for (index = 0; index <= ARRAY_SIZE - 1; index++)
    {
        cout << "Enter an integer " << (index + 1) << " : ";
        cin >> numbers[index];
    }
}

我在输出中添加了一个 for 循环和数组括号。

【讨论】:

    【解决方案2】:

    您需要遍历您的数组,只需添加一个循环并检查是否 (numbers[index] % 2 == 0) 以上答案应该会给你正确的结果。

    【讨论】:

      【解决方案3】:

      您可以对固定大小的数组使用范围 for 循环:

      for (int value : numbers)
      {
          if (value % 2 == 0)
          {
              cout << value << " is even.\n";
          }
          else
          {
              cout << value << " is odd.\n";
          }
      }
      

      【讨论】:

        【解决方案4】:

        首先,这在 C++ 中是非法的:

        const int ARRAY_SIZE = 20;
        int numbers[ARRAY_SIZE];
        

        你需要声明

        int numbers[20];
        

        改为。

        第二,

        if ( numbers % 2== 0 )
        

        numbers 是一个包含 20 个值的数组。使用 % 运算符到底是什么意思?您可能需要一个循环来检查数组中的每个 int。

        for (int i = 0; i < 20; ++i)
        {
          if ( numbers[i] % 2== 0 )
            ...
          else
            ...
        }
        

        【讨论】:

        • 好的,我现在明白了!我之所以这样是因为我认为由于用户输入它必须是空白的-谢谢我理解
        • const int ARRAY_SIZE = 20; int numbers[ARRAY_SIZE]; 是合法的(尝试编译它不会出错),正如其声明的const 允许用于初始化静态数组。
        • const int ARRAY_SIZE = 20; int numbers[ARRAY_SIZE]; 在 C 中是非法的。在 C++ 中是完全合法的。
        猜你喜欢
        • 1970-01-01
        • 2021-02-09
        • 2013-11-05
        • 1970-01-01
        • 1970-01-01
        • 2022-01-22
        • 1970-01-01
        • 2015-05-31
        • 1970-01-01
        相关资源
        最近更新 更多