【问题标题】:C++ warning: for increment expression has no effect [-Wunused-value]C++ 警告:对于增量表达式无效 [-Wunused-value]
【发布时间】:2020-11-02 00:47:25
【问题描述】:

在我的代码中,我让用户输入下限、上限和步长值。 c2 是下限+步长。摄氏度从下限开始。

    for (int x = c2; x <= upperLimit; x + step){
            celsius = celsius + step;
            cout << celsius;
        }

为什么我会收到 for increment 表达式无效的警告?

此外,这个 for 循环现在是无限的,即使它应该在上限处停止。这与警告有关吗?

【问题讨论】:

  • x + step 什么都不做。你的意思是x += stepx = x + step

标签: c++ loops for-loop warnings increment


【解决方案1】:

您需要x += step 来更新x 的值。像这样:

for (int x = c2; x <= upperLimit; x += step) {
    celsius += step;
    cout << celsius;
}

你有x + step,它计算总和但对结果没有任何作用。因此警告它没有效果。

循环是无限的,因为x 从未改变,因此从未超过upperLimit

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多