I am new to C and C++, but I wrote this calculator in C++. It compiles okay on borland 5.5, and can do addition, subtraction, multiplication, and division of two numbers. If you type any characters except numbers, +,-,*,/,or e, there will be lots of beeping noises and error messages!

Please post any ways I can improve this code or my programming in general!
(Additions such as square roots, etc. will be appreciated)
*****Planned changes: Add a constant for pi, add squared and square root, allow calculation of roman numerals.*****

``````/*
Program: Fett Calculator

File:C:\borland\bcc55\bin\cpp\calc

Function: Calculator, very simple

Author: Mister-Fett

Revision: Version 1.0, distributed 9/17/04
*/
#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);
}
}
}``````

## Featured Replies in this Discussion

• >>this calculator won't work Then why did you bother to post that crap?

Works fine for me, just had to remove the comma in the calc(choice,); part, but all out, great job.

hey itz a really cool program..i guess u put in a lot of effort to make it..
Keep it up.

looking good! there is a way to make it simpler with a function pointer but ill let u figure it out :)

I am also like you , just learning C++ and I am impressed at your efforts, but I think one obvious lapse is that your division function didn't take care of divide by zero error which is quite common.
Take care

10
CLS
nomainwin
`Nice try but there were some mistakes, `anyway it is very good.If you want to get `deeper into C++ it is better to start learning `some Liberty basic (www.libertybasic.com) it `will help oyu improve your skills in all not visual languages.

PRINT "Nice calculator !!!"
INPUT "equal " ; sere
dim = sere
End

Looks good mate, could be little more neat but non-the-less good,

i amde a better program though i am a noob. it also detects mathematical errors like division by zero etc...

here is the link to my Calculator program made in C, http://rapidshare.com/files/299372874/CALCUL_1.EXE and here is the source code http://rapidshare.com/files/299374659/calculator.CPP

and here is the source code i paste here

``````#include <stdio.h>
#include <conio.h>
#include <math.h>
void main (void)
{
float first, second, result;
char usershit;

printf("type the first number: ");
scanf("%f", &first);
printf("select an operation, select any from +,-,*,/ : ");
usershit=getche();
printf("\ntype the second number: ");
scanf("%f", &second);

switch(usershit)
{
case '+':
printf("the sum is %f", first+second);
printf("\nA simple C program by salman.");
getche();
break;

case'-':
printf("the solution is %f", first-second);
printf("\nA simple C program by salman.");
getche();
break;

case '*':
printf("the product is %f", first*second);
printf("\nA simple C program by salman.");
getche();
break;

case '/':
if (second<=0)
{
printf("division of a number by zero is illegal");
getche();-
}
else
printf("the solution is %f", first/second);
printf("\nA simple C program by salman.");
getche();
break;

default:
printf("the request you made is not supported/known by this program.");
printf("\nA simple C program by salman.");
getche();
break;

}

}``````

hiiiiiii plz i need calculator code with struture i mean like normal calculator plz help me out

this is a program of a simle caculator

#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<stdio.h>
#include<stdlib.h>

int main()
{
//calculator
clrscr();
float a,b,c;
int ch;

cout<<"1.addition , 2.substraction , 3.multiplication , 4.division ,5.exit"<<
"\n" ;
cout<<"enter the choice"<<"\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"enter the number for addition=" <<"\n";
cin>>a>>b;
c=(a+b);
break;
case 2:
cout<<"enter the number for subtraction"<<"\n";
cin>>a>>b;
c=(a-b);
cout<<"subtraction of two number is=\t"<<c<<"\n";
break;
case 3:
cout<<"enter the number for multiplication"<<"\n";
cin>>a>>b;
c=(a*b);
cout<<"multiplication of two number is=\t"<<c<<"\n";
break;
case 4:
cout<<"enter the two number for division\t"<<"\n";
cin>>a>>b;
c=(a/b);
cout<<"division of the two number is="<<c<<"\n";
break;
case 5:
exit(0);
break;

default:

cout<<"option entered by you is not valid"<<"\n";
break;

getch();
break;
}

thx for code dani...its working but i also need struture of calculator with it..i mean it should look like orignal calculator on screen and it should also perform same function like in calculator...plz help out..

this calculator won't work
#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;
int main(int nMumberofArgs, char* pszArgs[])
{
char operation;
float int1=0;
float int2=0;
cout<<"This is a four operation calculator.\n"
<<"Have Fun!"<<endl;
cout<<"Enter operation: ";
cin>>operation;
switch(operation)
{
case '+':
cout<<"Enter first number: ";
cin>>int1;
cout<<"Enter second number: ";
cin>>int2;
case '-':
case '*':
case '/':
}
system("PAUSE");
return 0;
}

>>this calculator won't work
Then why did you bother to post that crap?

here is an easy calculator

``````#include <cmath>
#include <iostream>

using namespace std;

int main()
{
int repeat = 1;
do{
char q;
cout<<"<M>For multiplication"<<endl;
cout<<"<D>For division"<<endl;
cout<<"<S>For subtraction"<<endl;
cin>>q;

if (q == 'M'|| q == 'm')
{
int a;
int b;
system("cls");
cout<<"Multiplication"<<endl;
cout<<"First number:"<<endl;
cin>>a;
cout<<"Second number:"<<endl;
cin>>b;
cout<<a<<" x "<<b<<" = "<<a*b<<endl;
}
else if (q == 'D'|| q == 'd')
{
int c;
int d;
system("cls");
cout<<"Division"<<endl;
cout<<"First number:"<<endl;
cin>>c;
cout<<"Second number:"<<endl;
cin>>d;
cout<<c<<" / "<<d<<" = "<<c/d<<endl;
}
else if (q == 'A'|| q == 'a')
{       int e;
int f;
system("cls");
cout<<"First number:"<<endl;
cin>>e;
cout<<"Second number:"<<endl;
cin>>f;
cout<<e<<" + "<<f<<" = "<<e+f<<endl;
}
else if (q == 'S'|| q == 's')
{
int g;
int h;
system("cls");
cout<<"Subtraction"<<endl;
cout<<"First number:"<<endl;
cin>>g;
cout<<"Second number:"<<endl;
cin>>h;
cout<<g<<" - "<<h<<" = "<<g-h<<endl;
}
else
cout<<"Choose the letter right!"<<endl;

repeat = repeat + 1;

}
while (repeat <= 99);

system("PAUSE");
return 0;
}``````

Try this, the minimalist!

``````#include <iostream>
using namespace std ;

void main()
{
int response;
int choose;
int num1, num2 ;
do
{
system ("cls") ;
cout << "Enter Two Numbers: " << endl ;
cin >> num1 >> num2 ;
cout << "Do you want to: " << endl;
cout << "1. Add." << endl;
cout << "2. Subtract." << endl;
cout << "3, Multiply." << endl;
cout << "4. Divide." << endl;
cout << "Specify Operation: " ;
cin >> choose;

switch(choose)
{
case 1: cout << num1 + num2 << endl ; break ;
case 2: cout << num1 - num2 << endl ; break ;
case 3: cout << num1 * num2 << endl ; break ;
case 4: cout << num1 / num2 << endl ; break ;
default: cout << "Invalid Operation" << endl ;
}
cout << "Press 1 To Continue, 0 to Exit: " ;
cin >> response ;
}
while ( response ) ;

system ("pause") ;
}``````

Here is a very simple calculator that allows you to input 3 integers if you wish:

``````//This program made by: DarK_DemoN 12/31/2010

#include <iostream>
#include <windows.h>

using namespace std;

int main()
{

restart:
int a;
int b;
int c;
cout << "Please enter a number: " << endl;
cin >> a;
cout << "Please enter another number: " << endl;
cin >> b;

char x;
cout << "Would you like to enter a 3rd number? [y/n]: " << endl;
cin >> x;

if (x == 'y' || 'Y') {
int c;
cin >> c;

cout << a << " + " << b << " + " << c << " = " << a+b+c << endl;
cout << a << " - " << b << " - " << c << " = " << a-b-c << endl;
cout << a << " * " << b << " * " << c << " = " << a*b*c << endl;
cout << a << " / " << b << " / " << c << " = " << a/b/c << endl;
}

else if (x == 'n' || 'N') {
cout << a << " + " << b << " = " << a+b << endl;
cout << a << " - " << b << " = " << a-b << endl;
cout << a << " * " << b << " = " << a*b << endl;
cout << a << " / " << b << " = " << a/b << endl;
}
else {
cout << "Invalid entry. Please try again" << endl;
goto restart;
}

cout << '\n' << endl;

system("PAUSE");
return 0;
}``````

As you can see if the user inputs and invalid character after being asked they would like to input a third number it will display the "Please try again" request and go back and start over.

``if (x == 'y' || 'Y')``

is not correct

``if(x=='y' || x=='Y')``

is. The first one is taking the OR of what amounts to two true entities (since 'y' and 'Y' are non-zero values) and comparing that to x. Same for the 'N' scenario.

You should change your goto statement construct into a do/while loop.

Yeh i just found that problem thank you for catching it. I didnt actually code this into my IDE i just wrote out all the code into my post but if i had compiled i would have caught that mistake.

I had a do/while loop in this code at first but then thought that if there were any beginning programers coming and looking at this thread they might not know what it does and a goto statement is pretty self explanatory.

Thanks

You