Click here to Skip to main content
15,891,902 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i am trying write a code but this code not enough. how i use if and call functions.

What I have tried:

C++
#include<stdio.h>

int Addition(int A, int B);
int Multiplication(int A, int B);
int Division(int A, int B);
int Subtraction(int A, int B);

int main()
{
	float A,B,entered,Addition,Multiplication;
	
		printf("\nPlease enter First Number (Non-Zero Value): ");
		scanf("%f",&A);
		printf("\nPlease enter Second Number (Non-Zero Value): ");
		scanf("%f",&B);
	
	
	printf("Which math operation do you want to run?\n----------------------------------------");
	printf("\n1) Addition\n2) Multiplication\n3) Division\n4) Subtraction\n----------------------------------------");
	printf("\nPlease enter 1 for Addition, 2 for Multiplication,\n3 for Division, 4 for Subtraction: ");
	scanf("%f", &entered);

	return 0;
}

int Addition(int A, int B)
{
	int result;
	result=A+B;
	return result; 
}
int Multiplication(int A, int B)
{
	int result;
	result=A*B;
	return result;	
}
int Division(int A, int B)
{
	int result;
	result=A/B;
	return result;	
}
int Subtraction(int A, int B)
{
	int result;
	result=A*B;
	return result;	
}
Posted
Updated 14-Mar-17 10:08am
v2
Comments
[no name] 14-Mar-17 14:48pm    
You mean you are calling functions all over the place and suddenly you don't know how to call a function? How does that happen?

Don't use a float for menu selection: use an integer instead, and use %d as the scanf format. And either pass floats to your methods - and return a float - or use integers there too.
Then:
C++
if (entered == 1) 
   {
   printf("%d", Addition(A, B));
   }
else if (entered == 2)
   {
   printf("%d", Multiplication(A, B));
   }

...
 
Share this answer
 
Comments
Albert Holguin 14-Mar-17 16:19pm    
Except there's a major negative in pulling in IO from a user as integers for the purpose of doing math... you'll immediately lose precision from a floating point input. Your results also don't always match the input types, integer division often results in non-integer value (i.e 3/2!=1, 3/2==1.5).
Advice: do yourself a favor and stop doing this, stop doing personal projects until you know enough programming with C++.
What you do is like "putting the cart before the horse".
Quote:
how i use if and call functions.

This is C++ 101 learning classe level. You do not even realize that you are already calling functions in your code.
You could as well try to learn to drive a car by winning a Formula 1 race when you don't even know how to use the breaks. Nobody try it because it don't work.

Learn C/c++ properly by reading a reference book or following tutorials. And do related exercises, there are here to allow you to practice on what you have just learned.

Here is links to references books on C and C++ by the authors of the languages. Note than C is the ancestor of C++, so knowing C is always useful with C++.
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]

C++ Programing Language[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900