Calculator program using Functions

The required Functions in the program are:
1. showMenu
2. getNumbers;
3. getOption
4. addNumber
5. divideNumber;
6. subtractNumber;
7. multiplyNumber;
8. displayResults;

So far I am able to run this program using one function only(addNumber). If I add other functions, they all show the same results(sum). How can i make the program to show different results for different options. Here is my code below. Thanks for the help. FYI- we begin learning functions last night in the class.

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>

using namespace std;

 

void showMenu ();
int getOption();
int getNumbers();
int addNumbers();
int divideNumbers();
int subtractNumbers();
int multiplyNumbers();
void displayResults();
 

void showMenu ()
{
cout<<" 1. Select 1 to add two numbers."<<endl;
cout<<" 2. Select 2 to divide two numbers. " <<endl;
cout<<" 3. Select 3 to subtract two number. " <<endl;
cout<<" 4. Select 4 to multiply two number. " <<endl;
}

 

int getOption (int selection)
{
cin>>selection;
return selection;
}


void getNumbers(int& num1, int& num2)
{
cout<<"Enter two numbers: ";
cin>>num1 >>num2;
}

 
int addNumbers(int num1, int num2, int& sum)
{
sum=num1+num2;
return sum;
}

 
int divideNumbers(int num1, int num2, int& sum)
{
sum=num1/num2;
return sum;
}


int subtractNumbers(int num1, int num2, int& sum)
{
    sum=num1-num2;
    return sum;
}


int multiplyNumbers(int num1, int num2, int& sum)
{
    sum=num1 * num2;
    return sum;
}

 

void displayResults(int num1, int num2, int& sum)
{
    cout<<num1 << " + " <<num2 << " = " <<sum <<endl;
}

 

int main ()
{
int a=0;
int num1;
int num2;
int Result;
 
showMenu();
getOption(a);
getNumbers(num1, num2);
addNumbers(num1, num2, Result);
divideNumbers(num1, num2, Result);
subtractNumbers(num1, num2, Result);
multiplyNumbers(num1, num2, Result);
displayResults(num1, num2, Result);

system("pause");
}


You dont need to prototype your functions if their above main. also use cin.get(); instead of system pause
Last edited on
I like that you are really using as many functions as possible but you might want to learn about the overhead that occurs every time a function is called.

You shouldn't be afraid to use lots of functions but it's always a good idea to try and logically group them together. You could easily combine "get option" and "get numbers" into one function and it would still be easy to manage. Since your calculator operations are rather trivial you might even consider merging them all into a switch statement that uses the menu number to perform the correct operation.

Finally, the reason why your program is acting funny is because you aren't using the menu choice to call the correct function. You are calling every function in sequential order everytime.

Here try running this code to see why you are getting your number.

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
void showMenu ();
int getOption();
int getNumbers();
int addNumbers();
int divideNumbers();
int subtractNumbers();
int multiplyNumbers();
void displayResults();
 

void showMenu ()
{
cout<<" 1. Select 1 to add two numbers."<<endl;
cout<<" 2. Select 2 to divide two numbers. " <<endl;
cout<<" 3. Select 3 to subtract two number. " <<endl;
cout<<" 4. Select 4 to multiply two number. " <<endl;
}

 

int getOption (int selection)
{
cin>>selection;
return selection;
}


void getNumbers(int& num1, int& num2)
{
cout<<"Enter two numbers: ";
cin>>num1 >>num2;
}

 
int addNumbers(int num1, int num2, int& sum)
{
sum=num1+num2;
cout << sum << endl;
return sum;
}

 
int divideNumbers(int num1, int num2, int& sum)
{
sum=num1/num2;
cout << sum << endl;
return sum;
}


int subtractNumbers(int num1, int num2, int& sum)
{
    sum=num1-num2;
cout << sum << endl;
    return sum;
}


int multiplyNumbers(int num1, int num2, int& sum)
{
    sum=num1 * num2;
cout << sum << endl;
    return sum;
}

 

void displayResults(int num1, int num2, int& sum)
{
    cout<<num1 << " + " <<num2 << " = " <<sum <<endl;
}

 

int main ()
{
int a=0;
int num1;
int num2;
int Result;
 
showMenu();
getOption(a);
getNumbers(num1, num2);
addNumbers(num1, num2, Result);
divideNumbers(num1, num2, Result);
subtractNumbers(num1, num2, Result);
multiplyNumbers(num1, num2, Result);
displayResults(num1, num2, Result);

}








Whew!
Finally, I was able to put this together. Could you please go through this real quick and advise me on any enhancement in the code. The code works now perfectly, with the exception that if the user enters any alph-letters. I tried to put a condition in the while statement, but then the program quits, rather than looping.

Once again, Thanks for the Help. Your suggestions really helped. Here is my code below that works now.

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130



#include<iostream>
using namespace std;



void showMenu();
int getOption(int selection);
void getNumbers(int& num1, int&num2);
int addNumbers(int num1, int num2);
int divideNumbers(int num1, int num2);
int subtractNumbers(int num1, int num2);
int multiplyNumbers(int num1, int num2);
void displayResults();

int main ()
{
	
	displayResults();
	
	
	/*system("pause");*/
	return 0;
}


void showMenu()
{
	cout<<"Enter---" <<endl;
	cout<<" 1: To add two numbers."<<endl;
cout<<" 2: To divide two numbers. " <<endl;
cout<<" 3: To subtract two number. " <<endl;
cout<<" 4: To multiply two number. " <<endl;
cout<<"99: To quit the program." <<endl;
}

int getOption(int selection)
{
	cin>>selection;
	return selection;
}

void getNumbers(int& num1, int&num2)
{
	cout<<"Enter two numbers: ";
	cin>>num1 >>num2;
}

int addNumbers(int num1, int num2)
{
	return num1+num2;

}

int divideNumbers(int num1, int num2)
{
	return num1/num2;
}

int subtractNumbers(int num1, int num2)
{
	return num1 - num2;
}

int multiplyNumbers(int num1, int num2)
{
	return num1 * num2;
}



void displayResults()
{

	int num1, num2, sum, choice;

	do
	{
		showMenu();
		cin>>choice;
		cout<<endl;
		

		switch (choice)
		{
		case 1:
			getNumbers(num1, num2);
			sum=addNumbers(num1, num2);
			cout<<num1 <<" + " <<num2 <<" = " <<sum <<"\n\n";
			system("pause");
			system("cls");
			break;

		case 2:
			getNumbers(num1, num2);
			sum=divideNumbers(num1, num2);
			cout<<num1 <<" / " <<num2 <<" = " <<sum <<"\n\n";
			system("pause");
			system("cls");
			break;

		case 3:
			getNumbers(num1, num2);
			sum=subtractNumbers(num1, num2);
			cout<<num1 <<" - " <<num2 <<" = " <<sum <<"\n\n";
			system("pause");
			system("cls");
			break;

		case 4:
			getNumbers(num1, num2);
			sum=multiplyNumbers(num1, num2);
			cout<<num1 <<" * " <<num2 <<" = " <<sum <<"\n\n";
			system("pause");
			system("cls");
			break;

		default:
			cout<<"Invalid input. Try again!" << "\n\n";
			system("Pause");
			
		
		}
	} while (choice == 1 || choice == 2 || choice ==3 || choice == 4);
}


Topic archived. No new replies allowed.