C++中的"Expected a statement"是什么意思

What does it mean mean by "Expected a statement" in C++

本文关键字:是什么 意思 statement Expected 中的 C++      更新时间:2023-10-16

我已经在Visual Studio中输入了C 中的IF代码,它表明,如果

我的代码是

#include <iostream>
using namespace std;
int main()
{
float marks, result;
cout << "Enter your Marks:";
cin >> marks;
if(marks>=50 && marks <= 100);
{
    cout << "Passed";
}
else if (marks < 50 && marks>=0);
{
    cout << "The grade is F ";
}
else
{
    cout << "Enter marks correctly";
}
}
output is

Enter your marks:97
passedenter marks correctly

if和否则都打印出两个语句

问题是这里

if(marks>=50 && marks <= 100);

您有不应该在那里的;。因此,;if语句的主体。这意味着"什么都不做"。结果,以下

{
    cout << "Passed";
}

...与if语句无关,将始终执行。之后就是这样:

else if (marks < 50 && marks>=0);

相同的问题,但是else无法编译,因为之前没有if。请记住,else之前的块错误不是if的一部分。相反,您想要的是:

if(marks>=50 && marks <= 100)
{
    cout << "Passed";
}
else if (marks < 50 && marks>=0)
{
    cout << "The grade is F ";
}
else
{
    cout << "Enter marks correctly";
}