### Calculator program using Functions

The required Functions in the program are:
2. getNumbers;
3. getOption
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.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697`` ``````#include 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."<>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<
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.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091`` ``````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."<>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<

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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130`` `````` #include 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---" <>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<
Topic archived. No new replies allowed.