I'm trying to do multiple calculations on same 2 integer variables and display their results separately. It works fine if I already set values to the variables. But if I try to take input from users then it gives me random numbers instead in the output. Here is my code in which I try to take the input/values from the user.
#include<iostream>
using namespace std;
class calculation
{
public:
calculation()
{
int num1 = 0;
int num2 = 0;
cout << "Write your numbers" << endl;
cin >> num1>>num2;
}
int sum()
{
return num1 num2;
}
int sub()
{
return num1-num2;
}
int mul()
{
return num1*num2;
}
int div()
{
return num1/num2;
}
int mod()
{
return num1%num2;
}
void displayMessage()
{
cout<<sum()<<endl<<sub()<<endl<<mul()<<endl<<div()<<endl<<mod();
}
private:
int num1,num2;
};
int main()
{
calculation obj;
obj.displayMessage();
system("Pause");
return 0;
}
The output is not correct and it just gives me random numbers instead.
But if I do it with pre set values using this code:
#include<iostream>
using namespace std;
class calculation
{
public:
calculation()
{
num1=10;
num2=5;
}
int sum()
{
return num1 num2;
}
int sub()
{
return num1-num2;
}
int mul()
{
return num1*num2;
}
int div()
{
return num1/num2;
}
int mod()
{
return num1%num2;
}
void displayMessage()
{
cout<<sum()<<endl<<sub()<<endl<<mul()<<endl<<div()<<endl<<mod();
}
private:
int num1,num2;
};
int main()
{
calculation obj;
obj.displayMessage();
return 0;
}
Then it gives the correct output.
CodePudding user response:
#include<iostream>
using namespace std;
class calculation
{
public:
calculation()
{
num1 = 0;
num2 = 0;
cout << "Write your numbers" << endl;
cin >> num1>>num2;
}
int sum()
{
return num1 num2;
}
int sub()
{
return num1-num2;
}
int mul()
{
return num1*num2;
}
int div()
{
return num1/num2;
}
int mod()
{
return num1%num2;
}
void displayMessage()
{
cout<<sum()<<endl<<sub()<<endl<<mul()<<endl<<div()<<endl<<mod();
}
private:
int num1,num2;
};
int main()
{
calculation obj;
obj.displayMessage();
system("Pause");
return 0;
}
You are redeclaring a local scope in the constructor. remove the int num1 and int num2 to just num1 = 0; num2 = 0;
Also consider moving the user input outside of constructor to the main and have a constructor that takes in 2 integers.
calculation(int num1, int num2)
{
this->num1 = num1;
this->num2 = num2;
}
CodePudding user response:
In your constructor:
calculation()
{
int num1 = 0;
int num2 = 0;
cout << "Write your numbers" << endl;
cin >> num1>>num2;
}
You have created two local variables names num1
and num2
. They have the same names as your member variables, but they are not the same. They are new variables.
You read values into those variables, and then they are immediately destroyed when the constructor exits.
This change will use the member variables, instead of creating new variables.
calculation()
{
num1 = 0;
num2 = 0;
cout << "Write your numbers" << endl;
cin >> num1 >> num2;
}
You could also optionally use this->
to be explicit that you intend to change member variables.
calculation()
{
this->num1 = 0;
this->num2 = 0;
cout << "Write your numbers" << endl;
cin >> this->num1 >> this->num2;
}