function for four basic arithmetic ... - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# function for four basic arithmetic ...

Posts: 25Member
Dear all,

I have a half-ready program to use the four basic (add, subtract, multiply, divide) arithmetic . I would need a little support to make it work.

Any help is appreciated,

attis

[code]
#include

int calcInt(int nOne, int nTwo, int nflag)
{
int nRet = 1;

if(1 == nflag){
printf(" %d
", nOne+nTwo);
nRet = 1;
}else if(2 == nflag){
printf(" %d
", nOne-nTwo);
nRet = 1;
}else if(3 == nflag){
printf(" %d
", nOne*nTwo);
nRet = 1;
}else if(4 == nflag){
printf(" %d
", nOne/nTwo);
nRet = 1;
}else{
nRet = 0;
}
return nRet;
}

int main(void)
{
int nRet = 0;

int calcInt = 1;
int nOne = 10;
int nTwo = 5;

if( 0 == calcInt){
printf("Failed!
");
}

return nRet;
}

[/code]

• Posts: 2,444Member
: Dear all,
:
: I have a half-ready program to use the four basic (add, subtract,
: multiply, divide) arithmetic . I would need a little support to make
: it work.

I made a change in blue

[code]

int main(void)
{
int nRet = 0;

[color=Green]/* You need to learn about how you pass parameters
to a function You don't need those: */
/* int calcInt = 1;
int nOne = 10;
int nTwo = 5; */[/color]

if( 0 == [color=blue]calcInt(10, 5, 1)[/color] ){
printf("Failed!
");
}
return nRet;
}
[/code]

And if I may make a suggestion (and if I don't I'm going to make it anyway :P): change "int nFlag" parameter to "char operator".
And then use '+', '-', '*' and '/' instead of 1-4:
[code]
int calcInt(int nOne, int nTwo, char operator)
{
switch (operator)
{
case '+':