此(C )代码有什么问题吗?如果是这样,有人可以告诉我我在哪里出错

Is there anything wrong with this (c++)code? if so can anybody tell me where I went wrong?

本文关键字:在哪里 告诉我 出错 如果 代码 什么 问题      更新时间:2023-10-16

我是C 的新手,正在练习。我使用CodeBlocks IDE。

#include <iostream>
using namespace std;
int main() {
    double f;
    double m;
    int counter;
    counter = 0;
    for (f = 1.0, f <= 100.0, f++) { // error: expected primary-expression before ')'
        m = f / 3.28;
        cout << f << " feet is " << m << " meters!n done";
        counter++;
        if (counter == 10) {
            cout << "n";
            counter = 0;
        }
    }
    cin.ignore();
    cin.get();
    return 0;
}

每次我将其放在IDE中时,我会收到以下错误:

error: expected primary-expression before ')' token

有人可以将我指向正确的方向吗?

您需要用semicolons而不是逗号分开for语句中的子句。
另外,声明for内的循环变量是清洁的:

for(double f=1.0; f<=100.0; f++) {
    ...
}

您的循环语句被逗号错误地分开。使它们半彩:

for (f = 1.0; f <= 100.0; f++) {
}

for循环需要在语句之间的半隆,因此应该是:

for(f=1.0; f<=100.0; f++)

return 0;

之后您也缺少}

您的语句不正确:

for(f=1.0, f<=100.0, f++) 

应该是:

for(f = 1.0; f <= 100.0; f += 1) // for(initial value; continuation condition; increment)

本质上,您需要将这些逗号更改为分号。