我即将用 C++ 创建简单的计算器。它应该允许用户在两个输入的数字上选择类型和操作。所以首先,用户必须从列表中选择数字类型(显示不同的类型,如 int、double、short 等)。之后,它应该允许您编写您之前选择的两个类型的数字。最后,您需要决定要对这些数字(+、-、/、*)执行什么操作。我的问题是我不知道如何将这些输入的数字从方法带到 main(),所以我可以对其进行操作。
#include <iostream>
using namespace std;
int integer()
{
int number1;
int number2;
cout << "First number: " << endl;
cin >> number1;
cout << "Second number: " << endl;
cin >> number2;
}
double doubl()
{
double number1;
double number2;
cout << "First number: " << endl;
cin >> number1;
cout << "Second number: " << endl;
cin >> number2;
}
int main()
{
cout << "Type to choose:" << endl;
cout << "1. int" << endl;
cout << "2. double" << endl;
int choosed;
cin >> choosed;
switch(choosed) {
case 1:
integer();
break;
case 2:
doubl();
break;
default:
cout << "Error" << endl;
break;
}
cout << "What operation would like to do on this numbers?" << endl;
cout << "1. +" << endl;
cout << "2. -" << endl;
cout << "3. *" << endl;
cout << "4. /" << endl;
int result;
switch(result){ //at this point i don't know how to invoke those numbers from methods
case 1:
}
cin.get();
}
谢谢!