# How to make a calculator using C++

Page 1 of 1

## 4 Replies - 27569 Views - Last Post: 22 September 2010 - 05:33 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=191434&amp;s=0f5cf372b327fafca477ce0758f9ad77&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jayar_chaos900

Reputation: 0
• Posts: 2
• Joined: 19-September 10

# How to make a calculator using C++

Posted 22 September 2010 - 12:41 AM

```#include <iostream.h>
#include <conio.h>
int mc(int x, int y) //Multiply two numbers
{
cout <<"\n\n"<< x <<" times "<< y <<" equals ";
return (x*y);
}
int ac(int a, int B)/> //Add two numbers
{
cout <<"\n\n"<< a <<" plus "<< b <<" equals ";
return (a+B)/>;
}
int sc(int z, int c) //Subtract two numbers
{
cout <<"\n\n"<< z <<" minus "<< c <<" equals ";
return (z-c);
}
int dc(int o, int t) //Divide two numbers
{
cout <<"\n\n"<< o <<" divided by "<< t <<" equals ";
return (o/t);
}
void calc(char choice)
{
int on,tw,thr;
if (choice == '+') //This whole block checks what the user wants to calculate, and refers to the proper routine to calculate it.
{
cout<<"You selected "<<choice<<". Please enter two numbers,\nsepperated by spaces,";
cout<<"that you want to add."<<endl;//print instructions for the user
cin>>on;//Get the value of variable on
cin>>tw;//Get the value of variable tw
thr=ac(on,tw);//Get the sum of on and tw, and assign that value to thr
cout<<thr<<"\n\n\n\aThanks for using my calculator!";//Print a thank you message
}
else if (choice =='-')
{
cout<<"You selected "<<choice<<". Please enter two numbers,\nsepperated by spaces, that you want to subtract."<<endl;
cin>>on;
cin>>tw;
thr=sc(on,tw);
cout<<thr<<"\n\n\n\aThanks for using my calculator!";
}
else if (choice =='*')
{
cout<<"You selected "<<choice<<". Please enter two numbers,\nsepperated by spaces, that you want to multiply."<<endl;
cin>>on;
cin>>tw;
thr=mc(on,tw);
cout<<thr<<"\n\n\n\aThanks for using my calculator!";
}
else if (choice =='/')
{
cout<<"You selected "<<choice<<". Please enter two numbers,\nsepperated by spaces, that you want to divide."<<endl;
cin>>on;
cin>>tw;
thr=dc(on,tw);
cout<<thr<<"\n\n\n\aThanks for using my calculator";
}
else
{
cin>>choice;
calc(choice,);
}
}
void main()
{
clrscr();
int one, two, three;
char choice;
while (choice != 'e')
{
cout<<"\nPlease enter +,-,*, or / and then two numbers,\nsepperated by spaces, that you wish to\nadd,subtract,multiply,or divide.\nType e and press enter to exit.";
cin>>choice;
if (choice != 'e')
{
calc(choice);
}
}
}

```

This post has been edited by no2pencil: 22 September 2010 - 01:27 AM
Reason for edit:: Added code tags

Is This A Good Question/Topic? 0

## Replies To: How to make a calculator using C++

### #2 janotte

• code > sword

Reputation: 990
• Posts: 5,141
• Joined: 28-September 06

## Re: How to make a calculator using C++

Posted 22 September 2010 - 12:46 AM

Welcome to DIC!

( a ) Delete all your code.
( b ) Get a fresh copy of the code with formatting in place from your editor / IDE.
( c ) Paste the formatted code between code tags like this
( d ) Use the "Preview Post" button to check it's all good.
( e ) Use the "Submit Modified Post" button to finish the editing.

( a ) Does your code compile?
( b ) Any errors or warnings? If there are then share them with us.
( c ) Is the program producing any output?
( d ) How is the actual output different to what you want / expect? Give details and, ideally, examples.

### #3 anonymouscodder

Reputation: 126
• Posts: 710
• Joined: 01-January 10

## Re: How to make a calculator using C++

Posted 22 September 2010 - 04:32 AM

```int ac(int a, int B)/> //Add two numbers
{
cout <<"\n\n"<< a <<" plus "<< b <<" equals ";
return (a+B)/>;
}
```

"B" is different then "b".

```calc(choice,);
```

Extra comma.

• Saucy!

Reputation: 6208
• Posts: 23,953
• Joined: 23-August 08

## Re: How to make a calculator using C++

Posted 22 September 2010 - 04:54 AM

The capital B is a bug in the system related to smilies.

### #5 anonymouscodder

Reputation: 126
• Posts: 710
• Joined: 01-January 10

## Re: How to make a calculator using C++

Posted 22 September 2010 - 05:33 AM

JackOfAllTrades, on 22 September 2010 - 07:54 AM, said:

The capital B is a bug in the system related to smilies.