我無法選擇的功能從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?
}
相同的文字你怎麼知道的程序總是轉到加()? 你的功能有相同的信息提示「輸入一個數字用於添加:」。針對不同的功能有不同的信息。如「輸入一個數字來劃分」。 – 2012-07-17 02:53:15
謝謝,我解決了這個問題。此外,我想知道爲什麼當我輸入它提示的數字時,它沒有運行void print()函數。 – Bucky763 2012-07-17 02:56:43