用 C++ 编写的程序忽略 if 和 else 语句
Programs written in C++ ignoring if and else statements
我是 C++ 的新手。我上周开始自学 C++,到目前为止,我还没有遇到任何实际问题。
我使用 Microsoft Visual Studio 2017,但我在 if 和 else 语句方面遇到了问题。你看,我正在制作这个非常有限的计算器。基本上,该程序为您提供 4 种基本数学运算,您可以通过输入 1、2、3 或 4 来选择要用于计算的运算。然后,它 运行 是另一个程序,您可以计算。 (例如:2代表减法,如果你输入2,它会运行减法计算器)
这是程序的代码。
#include "stdafx.h"
#include "iostream"
using namespace std;
int main(){
int add = 1;
int sub = 2;
int mul = 3;
int div = 4;
cout << "Extremely Limited C++ Calculator: Enter a number between one and
four to start calculating and press enter."
<< '\n'
<< "LEGEND"
<< '\n' << "1 = Addition"
<< '\n' << "2 = Subtraction"
<< "\n" << "3 = Multiplication"
<< '\n' << "4 = Division"
<< '\n' << "Operation: ";
if (cin >> add) {
system("start C:\CalculatorApps\addition.exe");
return 0;
}
if (cin >> sub) {
system("start C:\CalculatorApps\subtraction.exe");
return 0;
}
if (cin >> mul) {
system("start C:\CalculatorApps\multiplication.exe");
return 0;
}
if (cin >> div) {
system("start C:\CalculatorApps\division.exe");
return 0;
}
}
所以我完成了 addition.exe 和 subtraction.exe,但问题是无论我输入什么数字,它总是 运行 addition.exe。在减法计算器中,我尝试让用户选择是否要减去 2 个以上的数字,但这也没有成功,因为它忽略了 if 语句。我也有一次在减法计算器和主程序上都有一个 else 语句,它会把你带到显示文本的计算器上,上面写着他们输入的数字不是一个有效的选择,并输入一个有效的选择,但即使这样也被忽略了由程序。现在,也许我在互联网上看得不够仔细,但我找不到能帮助我的人。如果你知道答案,请用我能理解的语言告诉我(毕竟我 是 新手),或者请 link 我回答另一个已经回答的问题将解决我的问题。
先谢谢你了!
cin
是 class istream
的对象,表示标准输入流。它对应于 cstdio
流 stdin
。运算符 >>
为流重载 return 对同一流的引用。流本身可以通过转换运算符在 boolean
条件下评估为 true
或 false
。
cin 提供格式化流提取。操作cin >> x;
where "x
" is an int 如果输入非数字值将失败。所以:
if(cin>>x)
如果您输入字母而不是数字,将 return 为假。
由于您总是输入数字,因此您的第一个语句 if(cin>>sum)
始终为真。因此,您的其他语句将被跳过。
我是 C++ 的新手。我上周开始自学 C++,到目前为止,我还没有遇到任何实际问题。 我使用 Microsoft Visual Studio 2017,但我在 if 和 else 语句方面遇到了问题。你看,我正在制作这个非常有限的计算器。基本上,该程序为您提供 4 种基本数学运算,您可以通过输入 1、2、3 或 4 来选择要用于计算的运算。然后,它 运行 是另一个程序,您可以计算。 (例如:2代表减法,如果你输入2,它会运行减法计算器) 这是程序的代码。
#include "stdafx.h"
#include "iostream"
using namespace std;
int main(){
int add = 1;
int sub = 2;
int mul = 3;
int div = 4;
cout << "Extremely Limited C++ Calculator: Enter a number between one and
four to start calculating and press enter."
<< '\n'
<< "LEGEND"
<< '\n' << "1 = Addition"
<< '\n' << "2 = Subtraction"
<< "\n" << "3 = Multiplication"
<< '\n' << "4 = Division"
<< '\n' << "Operation: ";
if (cin >> add) {
system("start C:\CalculatorApps\addition.exe");
return 0;
}
if (cin >> sub) {
system("start C:\CalculatorApps\subtraction.exe");
return 0;
}
if (cin >> mul) {
system("start C:\CalculatorApps\multiplication.exe");
return 0;
}
if (cin >> div) {
system("start C:\CalculatorApps\division.exe");
return 0;
}
}
所以我完成了 addition.exe 和 subtraction.exe,但问题是无论我输入什么数字,它总是 运行 addition.exe。在减法计算器中,我尝试让用户选择是否要减去 2 个以上的数字,但这也没有成功,因为它忽略了 if 语句。我也有一次在减法计算器和主程序上都有一个 else 语句,它会把你带到显示文本的计算器上,上面写着他们输入的数字不是一个有效的选择,并输入一个有效的选择,但即使这样也被忽略了由程序。现在,也许我在互联网上看得不够仔细,但我找不到能帮助我的人。如果你知道答案,请用我能理解的语言告诉我(毕竟我 是 新手),或者请 link 我回答另一个已经回答的问题将解决我的问题。 先谢谢你了!
cin
是 class istream
的对象,表示标准输入流。它对应于 cstdio
流 stdin
。运算符 >>
为流重载 return 对同一流的引用。流本身可以通过转换运算符在 boolean
条件下评估为 true
或 false
。
cin 提供格式化流提取。操作cin >> x;
where "x
" is an int 如果输入非数字值将失败。所以:
if(cin>>x)
如果您输入字母而不是数字,将 return 为假。
由于您总是输入数字,因此您的第一个语句 if(cin>>sum)
始终为真。因此,您的其他语句将被跳过。