Problem in Coding Basic Arithmetic Calculator using Functions and Switch

what is wrong with my code?
I used function and switch to make an arithmetic calculator. I wanted to use functions to their maximum that's why I used void functions with "cout" statement.
My program takes input only but does not show the output.

#include <iostream>
using namespace std;

void Addition(int x, int y)
{
cout<<x+y<<endl;

}
void Subtraction(int x, int y)
{
cout<<x-y<<endl;

}
void Multiplication(int x, int y)
{
cout<<x*y<<endl;

}
void Division(int x, int y)
{
cout<<x/y<<endl;

}

void main ()
{
int x,y,ans;
char op;
cout<<"Enter 2 Integers"<<endl;
cin>>x>>y;
cout<<"Enter an Operator from +,-,*,/"<<endl;
cin>>op;

switch(op)
{
case '+' : Addition;
break;
case '-' : Subtraction;
break;
case '*' : Multiplication;
break;
case '/' : Division;
break;
default : cout<<"Invalid Input"<<endl;
break;
}

system("pause");
}
The functions that you define at the top take two integer parameters; e.g. x and y in
void Addition(int x, int y);
So when you call them you also need to supply two integer parameters (which don't have to be called x and y); thus your switch case for addition should be
case '+' : Addition(x,y);
Sort this out for all the other operations too.

Other (more minor) issues:
- please use code tags and indentation as it makes your code more easy to read (I know this may not work on first input - in future, go back immediately and edit your post to include the code tags from the Format menu on the right)
- you need #include<cstdlib> in order to use system() with a strict compiler;
- should be int main();, not void main(); with a strict compiler;
- you might consider returning values from your functions, as you had variable ans ready and waiting to receive them in main()
- you might also want to consider using float or double instead of int type for your arithmetic variables, or your division operation may (will) run into difficulties with integer division (int divided by int must give int; in this case, by rounding toward zero).
Last edited on
Topic archived. No new replies allowed.