can you help me fix this code to make it better and more acceptable as per coding standards and such. im trying to get a good grasp of classes in c++

#include <iostream>

using namespace std; 




class Calculator
{
        private: 
          int number1; 
          int number2 ; 
          
        public: 
          
          Calculator setCalcNumbers(int input1 , int input2 )
          {
          number1 = input1; 
          number2 = input2; 
          }
          int addNumber()
          {
              return number1 + number2;
          }
          int subtractNumber()
          {
              return number1 - number2;
          }
          int divideNumber()
          {
              return number1 / number2;
          }
          int multiplyNumber()
          {
              return number1 * number2;
          }
        
};

int main()
{
    int numberInput1 = 0; 
    cout << "Enter number 1: "; 
    cin >> numberInput1; 
    
    
    int numberInput2 = 0;
    cout << "Enter number 2: "; 
    cin >> numberInput2;
    
    Calculator t; 
    t.setCalcNumbers(numberInput1, numberInput2);
    
    char userOperationChoice; 
    cout << "which operation would you like to perform? "
         << " , enter M for Multiplication, D for Division, A for addition or S for Subtraction:" << endl;
         
    cin >> userOperationChoice; 
    char a,d,m,s;
    switch (userOperationChoice) 
    {
    
    case 'a'  : 
         t.addNumber();
         cout << "the total is: " << t.addNumber() << endl; 
         break; 
         
    case 's':
         t.subtractNumber(); 
         cout << "the total is: " << t.subtractNumber() << endl; 
         break ; 
    case 'd': 
         t.divideNumber();
         cout << "the total is: " << t.divideNumber() << endl; 
         break; 
    case 'm':
         t.multiplyNumber();
         cout << "the total is: " << t.multiplyNumber() << endl; 
         break; 

    }        
         
   
    
    system("pause"); 
    return 0 ; 
}

Recommended Answers

All 4 Replies

#include <iostream>

using namespace std; 




class Calculator
{
private: 
int number1; 
int number2 ; 

public: 

Calculator setCalcNumbers(int input1 , int input2 )
{
number1 = input1; 
number2 = input2; 
}
int addNumber()
{
return number1 + number2;
}
int subtractNumber()
{
return number1 - number2;
}
int divideNumber()
{
return number1 / number2;
}
int multiplyNumber()
{
return number1 * number2;
}

};

int main()
{
int numberInput1 = 0; 
cout << "Enter number 1: "; 
cin >> numberInput1; 


int numberInput2 = 0;
cout << "Enter number 2: "; 
cin >> numberInput2;

Calculator t; 
t.setCalcNumbers(numberInput1, numberInput2);

char userOperationChoice; 
cout << "which operation would you like to perform? "
<< " , enter M for Multiplication, D for Division, A for addition or S for Subtraction:" << endl;

cin >> userOperationChoice; 
char a,d,m,s;
switch (userOperationChoice) 
{

case 'a' : 
t.addNumber();
cout << "the total is: " << t.addNumber() << endl; 
break; 

case 's':
t.subtractNumber(); 
cout << "the total is: " << t.subtractNumber() << endl; 
break ; 
case 'd': 
t.divideNumber();
cout << "the total is: " << t.divideNumber() << endl; 
break; 
case 'm':
t.multiplyNumber();
cout << "the total is: " << t.multiplyNumber() << endl; 
break; 

} 



system("pause"); 
return 0 ; 
}

Completed the end brace for you ;)

Each time you open a parenthesis, indent one 'tab' button... it makes it much MUCH easier to read and modify; also, add comments with the '//' prefix, they'll help enormously when youy program grows in size. Also, put a lines space between each method... it becomes very difficult to read, very quickly when you can't tell where something ends and another thing begins again.

Hope this helps. :)

Member Avatar for embooglement

Also, the line "char a,d,m,s;" isn't needed, because you never use any of those variables. the switch statement below it isn't using those variables, it's using the character constants 'a', or 'd', etc. So those four chars could be left out completely.

Just polished it up a little, and fixed a few things. Take a look it will definitely help you to compare your original to this :

#include <iostream>
using namespace std; 



class Calculator{
private: 
	int number1; 
	int number2 ; 
public: 
	Calculator(){
		number1 = number2 = 0;
	}
	Calculator(int input1 , int input2 ){
		number1 = input1; 
		number2 = input2; 
	}

	int addNumber(){
		return number1 + number2;
	}
	int subtractNumber(){
		return number1 - number2;
	}	
	int divideNumber(){
		if(number2 != 0)
			return number1 / number2;
		else return 0; 
	}
	int multiplyNumber(){
		return number1 * number2;
	}

};

int main(){
	
	int numberInput1 = 0; 
	cout << "Enter number 1: "; 
	cin >> numberInput1; 

	int numberInput2 = 0;
	cout << "Enter number 2: "; 
	cin >> numberInput2;


	Calculator t(numberInput1, numberInput2);

	char userOperationChoice; 
	cout << "which operation would you like to perform? ";
	cout << " , enter M for Multiplication, D for Division, A for addition or S for Subtraction:" << endl;


	cin >> userOperationChoice; 	

	switch (userOperationChoice) {

		case 'a' : 					
					cout << "the total is: " << t.addNumber() << endl; 
					break; 

		case 's':
					cout << "the total is: " << t.subtractNumber() << endl; 
					break ; 

		case 'd': 
					cout << "the total is: " << t.divideNumber() << endl; 
					break; 

		case 'm':		
					cout << "the total is: " << t.multiplyNumber() << endl; 
					break; 
	}

	cin.ignore( 256,'\n');
	cin.get();
	return 0 ; 

}
Member Avatar for embooglement

One more thing that might be worthwhile is to check for lower case and capital versions of each letter, so the user can put in "m" or "M" for multiplication, etc.

This would be easy to do with your switch statement, because the case's always fall through. Therefore you could do something like this:

switch (userOperation)
{
    case 'M':
    case 'm':
        // multiplication code
    break;

    case 'A':
    case 'a':
        // addition code
    break;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.