# How do I get my calculator to work?!

Page 1 of 1

## 4 Replies - 445 Views - Last Post: 20 April 2013 - 01:03 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=319024&amp;s=fb3026e02ff2bd887c1ef16b54159afe&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 lilblaze

Reputation: 0
• Posts: 2
• Joined: 19-April 13

# How do I get my calculator to work?!

Posted 19 April 2013 - 06:30 PM

```#include <iostream>

using namespace std;
char calcOper;
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 == '+')
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;
}
```

Is This A Good Question/Topic? 0

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

### #2 #define

• Duke of Err

Reputation: 1745
• Posts: 6,205
• Joined: 19-February 09

## 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.

### #3 lilblaze

Reputation: 0
• Posts: 2
• Joined: 19-April 13

## 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.

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 #define

• Duke of Err

Reputation: 1745
• Posts: 6,205
• Joined: 19-February 09

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

Posted 19 April 2013 - 07:38 PM

Your 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 DoNotWant

Reputation: 11
• Posts: 62
• Joined: 03-November 11

## 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.