Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

anyone help with simple prog please

Status
Not open for further replies.

gjpollitt

Programmer
Nov 8, 2005
4
GB
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;
}
 
you are assigning the value to op in each if statement.

use == for comparison

If somethings hard to do, its not worth doing - Homer Simpson
 
ah, thanks

Im used to VB and moving to C++, hence the '=' mistake.

thanks again

Graham
 
Which is why some people write:

code
if ( 3 == fred )
{
do something of great significance here
}
/code

rather than

code
if ( fred == 3 )
{
do something of great significance here
}
/code

because the first one generates an error at compile time if you leave out a "="...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top