簡體   English   中英

需要幫助從類中調用函數

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM