【问题标题】:Program wont run to the end after function is called调用函数后程序不会运行到最后
【发布时间】:2016-03-10 01:56:26
【问题描述】:

我编写了一个程序来接受一个数组中的 15 个整数值,然后将该数组传递给一个函数,该函数将每个偶数索引值乘以 4。

当前程序显示初始数组,但似乎在显示修改后的数组之前就挂了。

请帮助我了解为什么程序会卡在这里!

    int main(){
    const int SIZE = 15;
    int quad[SIZE] = {};
    void quadruple(int[], const int);

    cout << "Enter 15 integer values into an array." << endl;
    for (int i = 0; i < SIZE; i++)                               // Accept 15 int values
    {
        cout << i << ": ";
        cin >> quad[i];
    }

    cout << "Before quadruple function is called: " << endl;
    for (int i = 0; i < SIZE; i++)
    {
        cout << quad[i] << " ";
    }
    cout << endl;

    quadruple(quad, SIZE);
    cout << "After even index value multiplication: " << endl;
    for (int i = 0; i < SIZE; i++)
    {
        cout << quad[i] << " ";
    }
    cout << endl;

    return 0;
}

void quadruple(int values[], const int SZ){
    for (int i = 0; i < SZ; i + 2)                // Multiply even values by 4
    {
        if ((i % 2) == 0)
        {
            values[i] = values[i] * 4;
        }
        else                                      // Keep odd values the same
        {
            values[i] = values[i] * 1;
        }
    }
}

【问题讨论】:

  • i + 2 橡皮鸭会说什么?
  • 有人会要求您使用调试器并进行橡皮鸭调试。为什么不是我。

标签: c++ arrays function for-loop


【解决方案1】:
for (int i = 0; i < SZ; i + 2)     

"i + 2" 什么都不做。

您的意思可能是“i += 2;”。

您的家庭作业是查找有关系统调试器的一些文档。按照 cmets 的建议,找到你的橡皮鸭在哪里。

【讨论】:

  • 谢谢先生,我会向我的橡皮鸭解释每一行。
猜你喜欢
  • 2016-04-06
  • 2018-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-07
  • 1970-01-01
  • 2016-01-13
相关资源
最近更新 更多