Anyone tell me why regardless of value of 'op' the four 'if' statements are processed please?
thanks
Graham
#include <iostream>
int add(int a,int b);
int subtract(int a, int b);
int multiply(int a,int b);
int divide(int a,int b);
int main()
{
using namespace std;
int numa, numb, ans, op;
cout << "simple maths program!\n";
cout << "enter first number : ";
cin >> numa;
cout << "enter second number : ";
cin >> numb;
cout << "1) add \n";
cout << "2) subtract \n";
cout << "3) divide \n";
cout << "4) multiply \n";
cin >> op;
if (op=1)
{
ans=add(numa,numb);
}
if (op=2)
{
ans=subtract(numa,numb);
}
if (op=3)
{
ans=divide(numa,numb);
}
if (op=4)
{
ans=multiply(numa,numb);
}
cout << "answer = " << ans << endl;
return 0;
}
int add(int a,int b)
{ return a+b;
}
int subtract(int a,int b)
{ return a-b;
}
int divide(int a,int b)
{ return a/b;
}
int multiply(int a,int b)
{ return a*b;
}
thanks
Graham
#include <iostream>
int add(int a,int b);
int subtract(int a, int b);
int multiply(int a,int b);
int divide(int a,int b);
int main()
{
using namespace std;
int numa, numb, ans, op;
cout << "simple maths program!\n";
cout << "enter first number : ";
cin >> numa;
cout << "enter second number : ";
cin >> numb;
cout << "1) add \n";
cout << "2) subtract \n";
cout << "3) divide \n";
cout << "4) multiply \n";
cin >> op;
if (op=1)
{
ans=add(numa,numb);
}
if (op=2)
{
ans=subtract(numa,numb);
}
if (op=3)
{
ans=divide(numa,numb);
}
if (op=4)
{
ans=multiply(numa,numb);
}
cout << "answer = " << ans << endl;
return 0;
}
int add(int a,int b)
{ return a+b;
}
int subtract(int a,int b)
{ return a-b;
}
int divide(int a,int b)
{ return a/b;
}
int multiply(int a,int b)
{ return a*b;
}