好的,所以我实际上已经用 C++ 编程了很长一段时间了,但我目前对一些可能非常明显的事情感到困惑。为了好玩,我决定写一个基本的计算器。加法,减法,乘法,除法,一大堆。正如您在下面看到的,我有一个名为 selection 的 int 变量,用于查找 1、2、3 或 4。一旦选择,它将调用相应的函数。但是,我决定我希望能够随时键入“帮助”来显示帮助。我怎样才能做到这一点?我知道我可以简单地选择一个字符串,但我觉得这只会给问题贴上创可贴(对未来的问题没有帮助)。我想随时获得“帮助”。但是,使用另一个 if() 语句来捕获“帮助”
请帮助我,我确信这很简单,但由于某种原因我无法弄清楚!
#include <iostream>
int firstnum;
int secondnum;
int multiplication(){
std::cout << "Multiplication chosen. Please enter first number." << std::endl;
std::cin >> firstnum;
std::cout << "Please enter second number." << endl;
std::cin >> secondnum;
std::cout << "Your answer is: " << firstnum * secondnum << "." << std::endl;
}
int division(){
std::cout << "Division chosen. Please enter first number." << std::endl;
std::cin >> firstnum;
std::cout << "Please enter second number." << std::endl;
std::cin >> secondnum;
std::cout << "Your answer is: " << firstnum / secondnum << "." << std::endl;
}
int addition(){
std::cout << "Addition chosen. Please enter first number." << std::endl;
std::cin >> firstnum;
std::cout << "Please enter second number." << std::endl;
std::cin >> secondnum;
std::cout << "Your answer is: " << firstnum + secondnum << "." << std::endl;
}
int subtraction(){
std::cout << "Subtraction chosen. Please enter first number." << std::endl;
std::cin >> firstnum;
std::cout << "Please enter second number." << std::endl;
std::cin >> secondnum;
std::cout << "Your answer is: " << firstnum - secondnum << "." << std::endl;
}
int main(){
int choice;
std::cout << "Calculator." << std::endl;
std::cout << "Multiplication: 1. Division: 2. Addition: 3. Subtraction: 4. Help: help." << std::endl;
std::cin >> choice;
if(choice == 1){
multiplication();
}
if(choice == 2){
division();
}
if(choice == 3){
addition();
}
if(choice == 4){
subtraction();
}
////if the user types "help" it will show help.
return 0;
}