【发布时间】:2015-09-04 14:33:21
【问题描述】:
我是一个初学者,我在使用链式 if-else 语句时遇到了问题。当我运行程序并输入我选择的项目时,它总是输出“无效条目。”。为什么它在 if 语句中等于它时不执行正确的功能?
谢谢,
不要
#include <iostream>
using namespace std;
int sum(int int1, int int2 ){
return int1 + int2;
}
int difference( int int1, int int2 ){
return int1 - int2;
}
int product( int int1, int int2 ){
return int1 * int2;
}
int quotient( int int1, int int2 ){
return int1 / int2;
}
int main(){
cout << "\nWelcome to the calculator.\n\n";
cout << "Please enter two numbers.\n\n";
int a;
int b;
cin >> a >> b;
cout << "What would you like to do with these numbers?\nHere are your options: add, subtract, multiply, or divide.\n\n";
string add;
string subtract;
string multiply;
string divide;
string choice;
cin >> choice;
if( choice == add )
cout << sum( a, b );
else if ( choice == subtract )
cout << difference( a, b );
else if ( choice == multiply )
cout << product( a, b );
else if ( choice == divide )
cout << quotient( a, b );
else
cout << "Invalid entry.\n";
return 0;
}
【问题讨论】:
-
你可能应该添加一个语言标签
-
感谢您的提示,这是我的第一篇文章。
标签: c++ if-statement chained