4 Replies - 287 Views - Last Post: 20 April 2013 - 01:03 AM Rate Topic: -----

#1 lilblaze  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 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;
}


Is This A Good Question/Topic? 0
  • +

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

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1431
  • View blog
  • Posts: 4,967
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 lilblaze  Icon User is offline

  • New D.I.C Head

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

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

Posted 19 April 2013 - 07:17 PM

View Post#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.
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1431
  • View blog
  • Posts: 4,967
  • 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?
Was This Post Helpful? 0
  • +
  • -

#5 DoNotWant  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 59
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1