gpt4 book ai didi

c++ - 需要帮助从类中调用函数

转载 作者:行者123 更新时间:2023-11-28 03:34:23 25 4
gpt4 key购买 nike

我无法从 class OperateClass 中选择函数

它询问用户“你想加、减、乘还是除?我想:”(在 main 中)。无论我输入什么,它都会转到函数 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?

}

最佳答案

所有方法的文本都相同 “输入要用于添加的数字:”

关于c++ - 需要帮助从类中调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11515365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com