# Making a calculator using functions

Page 1 of 1

## 2 Replies - 16990 Views - Last Post: 18 May 2009 - 05:35 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=105682&amp;s=617cade69e09422302ee64d0b6bcfc9a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 chelitoz_4

• New D.I.C Head

Reputation: 0
• Posts: 1
• Joined: 17-May 09

# Making a calculator using functions

Posted 17 May 2009 - 08:37 PM

Hi. I was trying to make a calculator using functions. The functions Include; add, multiplications, subtract, division, raise to power, square root, roots of polynomial of second degree, square root, average, and obviously a quit code to finish the application. The calculator, after choosing an options, it must ask the user if they want to make a new computation, if user enters NO, the calculator should end. I CANT USE CMATH LIBRARY... Please help me!...I started the code, but I dont know how to ask the user to use a option (add, multiply etc). I started it like this...

``` #include <iostream>
using namespace std;
double func, Sum(), Mult(), Subt(), Div(), Raise(), Roots(), Square(), Avg(), Quit;
int main()
{
double result;
cout<<"  ***Welcome to my calculator*** "<<endl;
cout<<"Select one of the following options: "<<endl;
cout<<"\t 2- Multiplication"<<endl;
cout<<"\t 3- Subtract"<<endl;
cout<<"\t 4- Division"<<endl;
cout<<"\t 5- Raise to power"<<endl;
cout<<"\t 6- Roots of Polynomial of Second Degree"<<endl;
cout<<"\t 7- Square Root"<<endl;
cout<<"\t 8- Average"<<endl;
cout<<"\t 9- Quit"<<endl;
cout<<"Enter a option: ";
cin>>func;
result=Mult();
cout<<"Result is "<<result<<endl;
system("pause");
return 0;
}
double Mult()
{

int n, count;
double total=1,num;
cout<<"How many numbers do you want to calculate: ";
cin>>n;
for (count=0;count<n;count++)
{
cout<<"Enter a value: ";
cin>>num;
total= total*num;
}
}

```

Im making the functions on different source files. I make the multiply and add. And I have no idea how to make this. I hope you can help me..

Is This A Good Question/Topic? 0

## Replies To: Making a calculator using functions

### #2 xCraftyx

• D.I.C Regular

Reputation: 11
• Posts: 258
• Joined: 13-September 08

## Re: Making a calculator using functions

Posted 17 May 2009 - 08:41 PM

What i usually do for menus like this is use a switch statement to manage the choices. As for the multiple computations part, use a while loop that will continue to execute unless the user chooses not to perform another computation (either through a boolean test or a break statement)

### #3 AGRAC393

Reputation: 3
• Posts: 248
• Joined: 13-May 09

## Re: Making a calculator using functions

Posted 18 May 2009 - 05:35 AM

chelitoz_4, on 17 May, 2009 - 07:37 PM, said:

Hi. I was trying to make a calculator using functions. The functions Include; add, multiplications, subtract, division, raise to power, square root, roots of polynomial of second degree, square root, average, and obviously a quit code to finish the application. The calculator, after choosing an options, it must ask the user if they want to make a new computation, if user enters NO, the calculator should end. I CANT USE CMATH LIBRARY... Please help me!...I started the code, but I dont know how to ask the user to use a option (add, multiply etc). I started it like this...

``` #include <iostream>
using namespace std;
double func, Sum(), Mult(), Subt(), Div(), Raise(), Roots(), Square(), Avg(), Quit;
int main()
{
double result;
cout<<"  ***Welcome to my calculator*** "<<endl;
cout<<"Select one of the following options: "<<endl;
cout<<"\t 2- Multiplication"<<endl;
cout<<"\t 3- Subtract"<<endl;
cout<<"\t 4- Division"<<endl;
cout<<"\t 5- Raise to power"<<endl;
cout<<"\t 6- Roots of Polynomial of Second Degree"<<endl;
cout<<"\t 7- Square Root"<<endl;
cout<<"\t 8- Average"<<endl;
cout<<"\t 9- Quit"<<endl;
cout<<"Enter a option: ";
cin>>func;
result=Mult();
cout<<"Result is "<<result<<endl;
system("pause");
return 0;
}
double Mult()
{

int n, count;
double total=1,num;
cout<<"How many numbers do you want to calculate: ";
cin>>n;
for (count=0;count<n;count++)
{
cout<<"Enter a value: ";
cin>>num;
total= total*num;
}
}

```

Im making the functions on different source files. I make the multiply and add. And I have no idea how to make this. I hope you can help me..

Hello,

I have a simple form of a calculator but I don't know if it will help you in your case! I didn't use cmath, so that may help. If all fails, then mabey it will give you an idea.
```#include <cstdlib>
#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
double num;
double num2;
char choice;
for (;;){
do {
cout<<"Please choose an option by entering the number, press q to quit\n";
cout<<"\n";
cout<<"2 - Subtraction\n";
cout<<"3 - Division\n";
cout<<"4 - Multiplication\n";
cout<<"\n";
cout<<"\n";
cin>>choice;
} while ( choice < '1' || choice > '7' && choice != 'q');
if (choice == 'q') break;
switch (choice) {
case '1':
cout<<"Please enter a number\n";
cout<<"\n";
cin>>num;
cout<<"Another number to be added\n";
cout<<"\n";
cin>>num2;
cout<<num + num2;
cout<<"\n";
break;
case '2':
cout<<"Please enter a number\n";
cout<<"\n";
cout<<"\n";
cin>>num;
cout<<"Another number to be subtracted\n";
cout<<"\n";
cout<<"\n";
cin>>num2;
cout<<num - num2;
cout<<"\n";
break;
case '3':
cout<<"Please enter a number\n";
cout<<"\n";
cout<<"\n";
cin>>num;
cout<<"Another one to be divided\n";
cout<<"\n";
cout<<"\n";
cin>>num2;
cout<<num / num2;
cout<<"\n";
break;
case '4':
cout<<"Please enter a number\n";
cout<<"\n";
cout<<"\n";
cin>>num;
cout<<"Another one to be multiplied\n";
cout<<"\n";
cout<<"\n";
cin>>num2;
cout<<"\n";
cout<<"\n";
cout<<"\n";
cout<<"The answer is ";
cout<<num * num2;
cout<<"\n";
cout<<"\n";
break;
}

}
return 0;

}

```