Fadly Multimedia's Website

This blog is a multimedia designer's guide to video, photo,programming c++, c#,html(css,js,jquery), python, audio producing works we can all share and grow together with.

Post Page Advertisement [Top]

This Calculator program uses Classes,this,const,array,if-else, do-while, QuitOption


#include <iostream>



using namespace std;




class arithmetic {


private:


double a;


double b;


double x;




public:


//constructors


arithmetic(){}


arithmetic(double, double, double);




//Functions


double userInput();


double calculate();


void display();




//using 'this' for variables that will only be declared later. "this" is a pointer to a class member only.


void setA(double a){this->a=a;}


double getA(){return a;}




void setB(double b){this->b=b;}


double getB(){return b;}




void setX(double x){this->x=x;}


double getX(){return x;}


};




double arithmetic::userInput() {


cout<< "Type in your first number: "<<endl;


cin>>a;


cout<< "Type in your second number: "<<endl;


cin>>b;




//Error Check


while(cin.good()==false) {




//report error and clear stream


cout<< "Error. Type numbers only!" << endl;


cin.clear();


cin.ignore(INT_MAX, '\n');




//get input again


cout<< "Type in your first number: "<<endl;


cin>>a;


cout<< "Type in your second number: "<<endl;


cin>>b;




}




return a;


return b;


}




double arithmetic::calculate() {




//Menu


cout<< "Choose 1 for addition: "<< endl;


cout<< "Choose 2 for subtraction: "<< endl;


cout<< "Choose 3 for multiplication: "<< endl;


cout<< "Choose 4 for division: "<< endl;




//User using array to choose the operation they want


const int size=4;


int array[size]= {1,2,3,4};


cin>>array[size];




if(array[size]==1){x=a+b; return x;}


else if(array[size]==2){x=a-b; return x;}


else if(array[size]==3){x=a*b; return x;}


else {x=a/b; return x;}




}




void arithmetic:: display() {


cout<< "Your first number is: "<<a<<endl;


cout<< "Your second number is: "<<b<<endl;


cout<< "The result is: "<<x<<endl;




}






int main() {


arithmetic result; //invoking the class


result.userInput();


result.calculate();


result.display();


//Do you want to re-run the program?

char choice;
    bool run = true;
    while(run)
    {
        // Make your calculation
        do{
   cout<<"Would you like to perform other calculation?(Y/N)"<<endl;
   cin >> choice;
   choice = tolower(choice);//Put your letter to its lower case
        
}while (choice != 'n' && choice != 'y'); {
        if(choice =='n'){
            run = false;
            break;
        }
    }
arithmetic result; //invoking the class

result.userInput();


result.calculate();


result.display();


    }



return 0;


}


© 2019 Fadly.M.H.Wychowvski

No comments:

Post a Comment

| Designed by Colorlib