Arithmetic using pointers

Im using dev c++ and Im trying to make a program that uses pointers to add, subtract, multiply and divide. So far here is what I've got:

/*

*/
/*

Function: This program asks the user to input two number in which it will
calculate sum, difference, multiplication and division using basic
arithmetic (+,-,*,/) It will use pointers to pass the values inputed
by the user by reference.
*/

#include <stdio.h>
#include <stdlib.h>

void add( int *num1, int *num2); /* prototype with pointer definition */
void subtract(int *num1, int *num2); /* prototype with pointer definition */
void multip(int *num1, int *num2);/* prototype with pointer definition */
void division(int *num1, int *num2);/* prototype with pointer definition */

int main( void )

{ /*begins the main function */
int firstNum; /* this will be the first number the user will input */
int secNum; /* this will be the second number the user will input */



/* We first ask the user to input the first and second number */
printf("please enter the first number: "); /* Prompt */
scanf("%d", &firstNum); /*reads an integer*/


printf("please enter the second number: ");
scanf("%d", &secNum); /*reads an integer*/

/* Loop WHILE, user enters 0, keep asking user to enter an integer greater than ZERO*/
while(secNum == 0){ /* begins the while loop */
printf("please enter an integer number greater than 0 : ");
scanf("%d", &secNum); /*reads an integer */
} /* ends the while loop */


/*Pass address of firstNum and secNum to add */
add( &firstNum, &secNum);
printf("The Sum of the two numbers is: %d \n",add);

/*Pass address of firstNum and secNum to subtract */
subtract( &firstNum, &secNum);
printf("The Difference of the two numbers is: %d \n",subtract);

/*Pass address of firstNum and secNum to multip */
multip( &firstNum, &secNum);
printf("The Product of the two numbers is: %d \n",multip);

/*Pass address of firstNum and secNum to division */
division( &firstNum, &secNum);
printf("The Quotient of the two numbers is: %d \n",division);

system("PAUSE");

return 0; /* indicates succeful termination*/

}/* terminates the main function */



/*definitiion of the function add*/
void add(int *num1, int *num2)
{
*add = (*num1)+(*num2); /* Will return the sum */
}
/*definitiion of the function subtract*/
void subtract(int *num1, int *num2, int *answer)
{
*subtract = (*num1)-(*num2); /* Will return the difference */
}
/*definitiion of the function multip*/
void multip(int *num1, int *num2, int *answer)
{
*multip = (*num1)*(*num2); /* Will return the Product */
}
/*definitiion of the function division*/
void division(int *num1, int *num2, int *answer)
{
*division = (*num1)/(*num2); /*Will return the quotient*/
}





Note: I defined a result variable to place the answer in . But This is not what I was suppose to do.. Apparently I can only have two pointer prototypes.
If any one knows what Im missing I could really use somehelp. Thanks!
Last edited on
Your code reminds me of pascal.

In pascal, the way you return values from a function is to "set the function name equal
to the return value." For example,


{ Ok, my pascal syntax is rusty. I can't remember where the semicolons go or if there
should be commas separating the parameters...}
function add( a: integer; b : integer ) : integer;
begin
add := a + b;
end;


In C/C++, you use the "return" keyword:

1
2
3
4
int add( int a, int b )
{
    return a + b;
}


hey , I have already tried to use this but Im still not getting any results.
this is what I did:

/*

Function: This program asks the user to input two number in which it will
calculate sum, difference, multiplication and division using basic
arithmetic (+,-,*,/) It will use pointers to pass the values inputed
by the user by reference.
*/

#include <stdio.h>
#include <stdlib.h>

void add( int *num1, int *num2); /* prototype with pointer definition */
void subtract(int *num1, int *num2); /* prototype with pointer definition */
void multip(int *num1, int *num2);/* prototype with pointer definition */
void division(int *num1, int *num2);/* prototype with pointer definition */

int main( void )

{ /*begins the main function */
int firstNum; /* this will be the first number the user will input */
int secNum; /* this will be the second number the user will input */



/* We first ask the user to input the first and second number */
printf("please enter the first number: "); /* Prompt */
scanf("%d", &firstNum); /*reads an integer*/


printf("please enter the second number: ");
scanf("%d", &secNum); /*reads an integer*/

/* Loop WHILE, user enters 0, keep asking user to enter an integer greater than ZERO*/
while(secNum == 0){ /* begins the while loop */
printf("please enter an integer number greater than 0 : ");
scanf("%d", &secNum); /*reads an integer */
} /* ends the while loop */


/*Pass address of firstNum and secNum to add */
add( &firstNum, &secNum);
printf("The Sum of the two numbers is: %d \n",add );

/*Pass address of firstNum and secNum to subtract */
subtract( &firstNum, &secNum);
printf("The Difference of the two numbers is: %d \n",subtract);

/*Pass address of firstNum and secNum to multip */
multip( &firstNum, &secNum);
printf("The Product of the two numbers is: %d \n",multip);

/*Pass address of firstNum and secNum to division */
division( &firstNum, &secNum);
printf("The Quotient of the two numbers is: %d \n",division );

system("PAUSE");

return 0; /* indicates succeful termination*/

}/* terminates the main function */



/*definitiion of the function add*/
void add(int *num1, int *num2)
{
return = *num1 + *num2; /* Will return the sum */
}
/*definitiion of the function subtract*/
void subtract(int *num1, int *num2, int *answer)
{
return = (*num1)-(*num2); /* Will return the difference */
}
/*definitiion of the function multip*/
void multip(int *num1, int *num2, int *answer)
{
return = (*num1)*(*num2); /* Will return the Product */
}
/*definitiion of the function division*/
void division(int *num1, int *num2, int *answer)
{
return = (*num1)/(*num2); /*Will return the quotient*/
}

I keep recieving errors. Thank you for your help.
You need to assign the return value to something. For example:
1
2
3
4
5
6
7
8
9
10
int f(){
	return 18;
}

int main(){
	int a=0;
	a=f();
	//a==18
	return 0;
}
Also, look at the difference between your functions and mine.

Yours:

1
2
3
4
void add(int *num1, int *num2)
{
    return = *num1 + *num2; 
}


Mine:

1
2
3
4
int add( int a, int b )
{
    return a + b;
}


Spot the differences. (pointer differences are irrelevant).
I guess we've given up on [code] tags then?

You need to be able to return a value. I'm not sure if int* add will work, maybe it will.

Please use code tags in future. All you have to do is click the # button on the right of the screen and paste your code in between the [code] and [/code] tags...
Topic archived. No new replies allowed.