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 << "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:
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.

1
2
3
4
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?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<iostream>

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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.