### Create a calculator using functions

So I have been working on a program that takes two integers and calculates addition,subtraction,multiplication,division, and modular division. So far I think I have everything correct to make it run...I just can't figure out how to rewrite it using functions. Any help would be appreciated. Thank you.

#include<iostream>

using namespace std;

int main()
{

int num1, num2, selection;

cout << "Please enter an integer: ";
cin >> num1;

cout << "Please enter another integer: ";
cin >> num2;

cout << "\n\n\n";

cout << "Select the desired function:\n";
cout << "2. Subtraction\n";
cout << "3. Multiplication\n";
cout << "4. Division\n";
cout << "5. Modular Division\n";
cout << "Selection: ";
cin >> selection;

cout << "\n\n\n";

switch(selection)
{
case 1:
cout << "The sum of " << num1 << " and " << num2 << " is " << num1+num2 << endl;
break;
case 2:
cout << "The difference of " << num1 << " and " << num2 << " is " << num1-num2 << endl;
break;
case 3:
cout << "The product of " << num1 << " and " << num2 << " is " << num1*num2 << endl;
break;
case 4:
cout << "The quotient of " << num1 << " and " << num2 << " is " << num1/num2 << endl;
break;
case 5:
cout << "The remainder of " << num1 << " and " << num2 << " is " << num1%num2 << endl;
break;
default:
cout << "Invalid selection\n";
}
return 0;
}

Put the math in different functions, e.g.

 ``1234`` ``````case 1: AddNum(num1,num2) OR std::cout << "SUM: " << AddNum(num1,num2) //return int ``````

And so on for the rest of the cases.
Ok I think i'm getting it. The only problem I'm having is declaring the funtions I'm not sure where to place them.
So this is what I have so far, but I'm getting errors that the functions were not declared. Did I declare them wrong or just put them in the wrong place?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263`` ``````#include using namespace std; AddNum(int,int); SubNum(int,int); MultNum(int,int); DiviNum(int,int); ModNum(int,int); int main() { int num1, num2, selection; cout << "Please enter an integer: "; cin >> num1; cout << "Please enter another integer: "; cin >> num2; cout << "\n\n\n"; cout << "Select the desired function:\n"; cout << "1. Addition\n"; cout << "2. Subtraction\n"; cout << "3. Multiplication\n"; cout << "4. Division\n"; cout << "5. Modular Division\n"; cout << "Selection: "; cin >> selection; cout << "\n\n\n"; switch(selection) { case 1: AddNum(num1,num2); cout << "The sum of " << num1 << " and " << num2 << " is " << num1+num2 << endl; break; case 2: SubNum(num1,num2); cout << "The difference of " << num1 << " and " << num2 << " is " << num1-num2 << endl; break; case 3: MultNum(num1,num2); cout << "The product of " << num1 << " and " << num2 << " is " << num1*num2 << endl; break; case 4: DiviNum(num1,num2); cout << "The quotient of " << num1 << " and " << num2 << " is " << num1/num2 << endl; break; case 5: ModNum(num1,num2); cout << "The remainder of " << num1 << " and " << num2 << " is " << num1%num2 << endl; break; default: cout << "Invalid selection\n"; } return 0; }``````
You have the function prototypes, but not the actual functions that contain the operations.

 ``123456789101112131415161718192021`` ``````int AddNum(int,int); int SubNum(int,int); int MultNum(int,int); double DiviNum(int,int); int ModNum(int,int); int main() { ..... } int AddNum(int num1, int num2) { ... } int SubNum(int num1, int num2) { ... } ``````

and so on.

remember to include the return type on the prototypes and definitions
Last edited on
Yep that fixed it! Thank you so much I appreciate the help!
Topic archived. No new replies allowed.