条件语句求值错误

Conditional statement evaluating wrong

本文关键字:错误 语句 条件      更新时间:2023-10-16

根据下面的程序,如果donuts = 1,它将把0分配给donuts,但如果我运行它,它显示0被分配给donuts。请告诉我我在看什么。因为我怎么看这个代码甜甜圈应该等于12,因为甜甜圈应该属于其他代码:

#include <iostream> // Header file to enable console I/O
#include <string>   // Header file to enable string
#include <iomanip>  // enables maniplution of io strem
using namespace std;
// Begin main Function Definition
int main()
{
int donuts = 10;
if (donuts = 1)
{
    donuts = 0;
}
else
{
    donuts += 2;
}
cout<< donuts;
system("PAUSE");
return 0;
}
// End of main function
if (donuts = 1)

应该是:

if (donuts == 1)
        //^^^

您需要使用逻辑相等的==或赋值=