#include <iostream>
usingnamespace std;
void sum(int num1, int num2);
void difference(int num1, int num2);
void product(int num1, int num2);
void qoutient(int num1, int num2);
int main()
{
int num1;
int num2;
cout<<"Enter your first number : ";
cin>>num1;
cout<<"Enter your second number : ";
cin>>num2;
}
void sum(int num1, int num2)
{
cout<<"The result is : "<<num1+num2;
}
void difference(int num1, int num2)
{
cout<<"The result is : "<<num1-num2;
}
void product(int num1, int num2)
{
cout<<"The result is : "<<num1*num2;
}
void qoutient(int num1, int num2)
{
cout<<"The result is : "<<num1/num2;
}
#include <iostream>
usingnamespace std;
void sum(int num1, int num2);
void difference(int num1, int num2);
void product(int num1, int num2);
void qoutient(int num1, int num2);
int main()
{
int num1;
int num2;
cout<<"Enter your first number : ";
cin>>num1;
cout<<"Enter your second number : ";
cin>>num2;
sum(num1, num2);
difference(num1, num2);
product(num1, num2);
qoutient(num1, num2);
}
void sum(int num1, int num2)
{
cout<<"The result is : "<<num1+num2<<endl;
}
void difference(int num1, int num2)
{
cout<<"The result is : "<<num1-num2<<endl;
}
void product(int num1, int num2)
{
cout<<"The result is : "<<num1*num2<<endl;
}
void qoutient(int num1, int num2)
{
cout<<"The result is : "<<num1/num2<<endl;
}
}
Edit: Thanks chipp
Edit Edit: I really should run my code before posting, this time it works, I am sure of it, I hope
#include <iostream>
usingnamespace std;
void sum(int num1, int num2);
void difference(int num1, int num2);
void product(int num1, int num2);
void qoutient(int num1, int num2);
int main()
{
int num1;
int num2;
cout<<"Enter your first number : ";
cin>>num1;
cout<<"Enter your second number : ";
cin>>num2;
sum(num1, num2);
difference(num1, num2);
product(num1, num2);
qoutient(num1, num2);
}
void sum(int num1, int num2)
{
cout<<"The result is : "<<num1+num2;
}
void difference(int num1, int num2)
{
cout<<"The result is : "<<num1-num2;
}
void product(int num1, int num2)
{
cout<<"The result is : "<<num1*num2;
}
void qoutient(int num1, int num2)
{
cout<<"The result is : "<<num1/num2;
}
the new problem is that it wont stop. kindly try it guys. please?
@xhtmlx it did show the whole result. it suppose to stop in sum right? oh wait. should i put a switch in it?
No, you called the four functions (difference, sum, product, quotient). You mean the console is closing before you can see the results, correct? Simply add something like 'std::cin.get()' to the end of main.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
int main()
{
int num1;
int num2;
cout<<"Enter your first number : ";
cin>>num1;
cout<<"Enter your second number : ";
cin>>num2;
sum(num1, num2);
difference(num1, num2);
product(num1, num2);
qoutient(num1, num2);
cin.get();
return 0;
}
You should also cast 'num1' and 'num2' as doubles when dividing the two. If you don't then the two numbers will be rounded down and there will be no decimal point displayed.
1 2 3 4
void qoutient(int num1, int num2)
{
cout<<"The result is : "<<static_cast<double>(num1)/static_cast<double>(num2);
}
or the 'C-styled' cast:
1 2 3 4
void qoutient(int num1, int num2)
{
cout<<"The result is : "<<(double)num1/(double)num2;
}
@nicole
What's happening is that you're calling all four functions after all of your input. The issue with this is that each one of them will execute their code, return to main, then move down to the next function call. In turn, you're adding, subtracting, multiplying, and dividing every time. You had the right idea with the switch. This will allow you to manually select which function you want to call. A quick example:
#include <iostream>
usingnamespace std;
void sum(int num1, int num2);
void difference(int num1, int num2);
void product(int num1, int num2);
void qoutient(int num1, int num2);
int main()
{
int num1;
int num2;
char op;
cout<<"Enter your first number : ";
cin>>num1;
cout<<"Enter your second number : ";
cin>>num2;
cout<<"Enter operation: ";
cin>>op;
switch(op) {
case('+'): // if op = '+'
sum(num1, num2);
break;
case('-'): // if op = '-'
difference(num1, num2);
break;
case('/'): // if op = '/'
qoutient(num1, num2);
break;
case('*'): // if op = '*'
product(num1, num2);
break;
}
return 0;
}
void sum(int num1, int num2)
{
cout<<"The result is : "<<num1+num2;
}
void difference(int num1, int num2)
{
cout<<"The result is : "<<num1-num2;
}
void product(int num1, int num2)
{
cout<<"The result is : "<<num1*num2;
}
void qoutient(int num1, int num2)
{
cout<<"The result is : "<<num1/num2;
}