[英]need help calling functions from class
我在从class OperateClass
选择函数时遇到了麻烦
它询问用户“你想要加,减,乘或除吗?我想:”(在主要部分)。 无论我放什么,它都会转到函数int add
。 我究竟做错了什么?
#include <iostream>
using namespace std;
int x = 1;
int number;
int total = 0;
int amt = 1;
int a;
int b;
int c;
int d;
string ans;
class OperateClass{
public:
int add(){
while(x <= 2){
cout << "Enter a number to use to add: " << endl;
cin >> a;
total = total + a;
x++;
amt++;
}
}
int subtract(){
while(x <= 2){
cout << "Enter a number to use to add: " << endl;
cin >> b;
total = total - b;
x++;
amt++;
}
}
int multiply(){
while(x <= 2){
cout << "Enter a number to use to add: " << endl;
cin >> c;
total = total * c;
x++;
amt++;
}
}
int divide(){
while(x <= 2){
cout << "Enter a number to use to add: " << endl;
cin >> d;
total = total / d;
x++;
amt++;
}
}
};
int main()
{
cout << "Do you want to add, subtract, multiply or divide? I want to : " << endl;
cin >> ans;
if(ans == "add"){
OperateClass opOper;
opOper.add();
}
else if (ans == "subtract"){
OperateClass opOper;
opOper.subtract();
}
else if (ans == "multiply"){
OperateClass opOper;
opOper.multiply();
}
else if(ans == "divide"){
OperateClass opOper;
opOper.divide();
}
}
还有,这下面的功能是否足以打印?
void print(){
cout << "Your total is: " << total << endl; Where should I call this?
}
所有方法都有相同的文本"Enter a number to use to add: "
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.