在预期中,IF 语句
While expected, IF Statement
if (Mileage > 0) do
{
calculateMileage();
cout << "The cost of shipment over " << setprecision(2) << Mileage << " miles is 234" << variableShippingCost << ".";
cout << "n n";
system("pause"); //to hold the output screen
return(0);
}
else
{
cout << "n ERROR: The distance should be a positive value.";
system("pause"); //to hold the output screen
return(0);
}
我不知道为什么,但Visual Studio 12在其他地方带来了一个错误,说它需要一段时间。我之前做过很多语句,而且在这个程序中工作正常,所以谁能帮助我理解为什么在这种情况下它不高兴?
正确的语法是:
if (...)
{...} else {...}
使用if
和
do {...}
while (...);
使用do...while
时。
C/C++ 中没有if() do
语句!
你在if
之后有一个do
,所以编译器期望在do
块之后有一个while
。
if (Mileage > 0)
{
do
{
calculateMileage();
//etc...
} while (something);
}
else
{
//etc...
}
或
if (Mileage > 0) // no `do` here
{
calculateMileage();
//etc...
}
else
{
//etc...
}
不要
使用do.那是一段时间的循环,使用的正确语法是
do{
...code here...
} while(some condition is true)
你想要的是
if (Mileage > 0) //there is an implicit then here no need to do anything here
{
calculateMileage();
cout << "The cost of shipment over " << setprecision(2) << Mileage << " miles is 234" << variableShippingCost << ".";
cout << "n n";
system("pause"); //to hold the output screen
return(0);
} //<<<------if you really wanted to use the do (which you shouldnt) put a while here.
else
{
cout << "n ERROR: The distance should be a positive value.";
system("pause"); //to hold the output screen
return(0);
}
因为你做错了!C++有 if-else 语句和 do-while 语句。 do
期望while
跟随自己,同时while
可以独立使用。同样,if
可以独立使用,但else
期望先if
。
相关文章:
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- If语句在c++中被忽略
- 比较if语句中的数组值和int值
- 以在Qt中的IF语句中设置时间延迟
- Craps游戏问题,忽略if语句
- c++:定义if语句中的模板
- C++嵌套的 If 语句中,它无法按预期工作
- 如果条件不相关,我应该更喜欢两个 if 语句而不是 if-else 语句吗?
- 需要有关此 if 语句的帮助
- 转换器练习:跳过 if 语句和 if 语句
- 避免在 C++ 中的 if 语句中递增
- 如果仍然不满足要求,如何使 if 语句重复?
- do while 循环中的 if 语句以 yes 或 no 结尾
- 为什么在此C++代码中触发此 if 语句?
- 如何基于平台在制作文件中制作if语句?
- 在 if 语句中使用 GetKeyState 时,它会跳过条件
- C++ If 语句,从其他语句重复