#include <iostream> using namespace std; char calcOper; void addition(); void subtraction(); void multiplication(); void division(); //I was instructed to create a calculator rogram with 4 subfunctions (add, sub, mult, div) //I must run this entire program through a loop that will execute until the user no longer desires to compute via the calculator. int main() { do{ cout << "Please enter a simple operator (Ex. + - * /): "; cin >> calcOper; if(calcOper == '+') addition(); else if(calcOper == '-') subtraction(); else if(calcOper == '*') multiplication(); else if(calcOper == '/') division(); }while(); void addition(float a, float b, float total) { cout << "Enter two integers (separated by a single space): "; cin >> a, b; total = a + b; cout << "The sum is: " << total << endl; } void subtraction(float& a, float& b, float& total) { cout << "Enter two integers (separated by a single space): "; cin >> a, b; total = a - b; cout << "The difference is: " << total << endl; } void multiplication(float& a, float& b, float& total) { cout << "Enter two integers (separated by a single space): "; cin >> a, b; total = a * b; cout << "The product is: " << total << endl; } void division(float& a, float& b, float& total) { cout << "Enter two integers (separated by a single space): "; cin >> a, b; total = a / b; cout << "The quotient is: " << total << endl; }

# How do I get my calculator to work?!

Page 1 of 1## 4 Replies - 618 Views - Last Post: 20 April 2013 - 01:03 AM

### #1

# How do I get my calculator to work?!

Posted 19 April 2013 - 06:30 PM

##
**Replies To:** How do I get my calculator to work?!

### #2

## Re: How do I get my calculator to work?!

Posted 19 April 2013 - 07:09 PM

Have you been told to write the functions the way you have?

I would have probably thought of reading a number, operator and number in the loop.

I would have probably thought of reading a number, operator and number in the loop.

### #3

## Re: How do I get my calculator to work?!

Posted 19 April 2013 - 07:17 PM

#define, on 19 April 2013 - 07:09 PM, said:

Have you been told to write the functions the way you have?

I would have probably thought of reading a number, operator and number in the loop.

I would have probably thought of reading a number, operator and number in the loop.

We have been instructed to use sub functions. The logic behind my code was to use if/else statements to decipher what mathematical operator will be used to calculate the two numbers that have been input.

### #4

## Re: How do I get my calculator to work?!

Posted 19 April 2013 - 07:38 PM

Your

If you wish to run your code you don't need any parameters in you function.

Functions (I)

Are you getting any errors?

*if*statements are fine, they call the function associated with the operator. If you ask for the numbers as well you can pass the numbers to the sub-function which will perform the correct mathematical operation.If you wish to run your code you don't need any parameters in you function.

29 void addition() 30 {

Functions (I)

Are you getting any errors?

### #5

## Re: How do I get my calculator to work?!

Posted 20 April 2013 - 01:03 AM

void addition(float a, float b, float total) { cout << "Enter two integers (separated by a single space): "; cin >> a, b; total = a + b; cout << "The sum is: " << total << endl; }

If you have declared your add function like this, you have to call it like this.

addition(firstArgumentToBePassed, secondArgumentToBePassed, etc, etc...);

As you call it now, you could just change you functions.

void addition() { int a, b, total = 0; cout << "Enter two integers (separated by a single space): "; cin >> a, b; total = a + b; cout << "The sum is: " << total << endl; }

Don't write "Enter two integers", and then use floats BTW.

Page 1 of 1