While循环和if/else语句工作不正常
While loop and if/else statement not working correctly
我遇到的问题是,当我运行代码时,它会正确添加,但当运行if
或else
语句时,什么都不会发生。它总是刚刚结束。
#include <iostream>
using namespace std;
int main()
{
int firstNumber;
int secondNumber;
int sum;
char restart = 1;
while (restart == 1)
{
cout<<"Welcome to My Calculator!nn";
cout<<"What is the first number you would like to add?nn";
cin>>firstNumber;
cout<<"What is the second number you would like to add?nn";
cin>>secondNumber;
cout<<"Wonderful! Getting together your result now....nn";
sum = firstNumber + secondNumber;
cout<< sum<<endl;
cout<<"If you have another question, just enter 1, if not press 2!nn";
cin>>restart;
if (restart == 1)
{
cout<<"No problem!nn";
}
else
{
cout<<"Goodbye!";
}
}
return 0;
}
char restart = 1;
if (restart == 1)
需要
char restart = '1';
if(restart == '1')
1和"1"之间存在差异。如果要比较char
,请使用''
标记。
此外,您还应该始终初始化int
的
这是因为C++不会自动将其设置为零。所以,你应该自己初始化它:
int sum = 0;
一个未初始化的变量有一个随机数,如654654、-5454等。(如果它在读取时没有调用未定义的行为(
相关文章:
- C++嵌套的 If 语句中,它无法按预期工作
- if-else 语句仅按特定顺序工作,我不知道为什么
- C++-循环中的If语句工作不正常
- switch-case语句如何在Arduino/C++中工作
- While循环和if/else语句工作不正常
- 我的 if 语句无法正常工作
- scanf() 语句中"%*[^n]"的格式字符串指示什么?分配抑制器 (*) 和否定扫描集 ([^) 如何协同工作?
- 如果带有字符数组的语句无法正常工作
- 为什么查找 + 插入的组合比单个插入语句工作得更快
- C 开关语句无法正常工作
- 返回指针的语句 - 解释它是如何工作的以及为什么
- C 如果与我期望的语句或操作员无法正常工作
- 为什么我的开关语句无法正常工作
- 我的 IF 语句中存在问题,否则无法正常工作
- If/Else 语句在 c++ 中无法正常工作
- 嵌套的If-else语句可能无法正常工作
- C++ 中的正则表达式语句未按预期工作
- C++ 如果 Else 语句无法正常工作
- C++if/else-if语句工作不正常
- 如果在没有()的Arduino中用函数作为条件的语句工作正常