Make a calculator in C++

Here is how to make a calculator in C++ language.

[sourcecode language='cpp']
#include
#include void main()
{
int num1,num2,res;
char ch,choice,count;
count=0;
z:
cout<<"*******************************************************************************"< cout<<"Press * for multiplication, + for addition, - subtraction, / division"< cout<<"*******************************************************************************"< cin>>ch; count++;
switch(ch)
{
case ‘+’:
cout<<"/////////////////////////"< cout<<"Enter two numbers"< cout<<"/////////////////////////"< cin>>num1>>num2;
res=num1+num2;
cout<<"Your query returned"<<" "< break;

case '-':
cout<<"/////////////////////////"< cout<<"Enter two numbers"< cout<<"/////////////////////////"< cin>>num1>>num2;
res=num1-num2;
cout<<"Your query returned"<<" "< break;

case '*':
cout<<"/////////////////////////"< cout<<"Enter two numbers"< cout<<"/////////////////////////"< cin>>num1>>num2;
res=num1*num2;
cout<<"Your query returned"<<" "< break;

case '/':
m:
cout<<"/////////////////////////"< cout<<"Enter two numbers"< cout<<"/////////////////////////"< cin>>num1>>num2;
if(num2==0)
{
cout<<"Sorry, please enter a value which is not zero"< goto m;
}
res=num1/num2;
cout<<"Your query returned"<<" "< break;
default:
cout<<"Please select anyone option"<

};
if(count==3)
{cout<<"You already used the program thrice...";
exit(0);}
cout<<"::::::::::::::::::::::::::::::::::::::::"< cout<<"Do you want to continue? (Press Y/N)"< cout<<"::::::::::::::::::::::::::::::::::::::::"< cin>>choice;
if((choice==’Y')||(choice==’y'))
{goto z;}
}
[/sourcecode]

 

This program also has an inbuilt limitation of limiting the usage upto three times only.

  • Anonymous

    nice

    • ab

      wats this … no oops concpet used here …

      • http://www.gyanguru.org Gyanguru

        This was the base of what you’ll do using classes. You can easily tweak the program if you know about classes

  • ab

    it will only work for ints wat abt float ?? u can use templates … for calculating int, float results…

    • http://www.gyanguru.org Gyanguru

      Exactly!
      This was just to make students think of the various possibilities one can try to make the calculator more accurate. You did just that! :)

      Good job :)

  • Khogosonu420

    Thank s

    • Earl

      thanks

  • Zshan63

    marvelous

  • JESUS CHRIST

    You Screwed it all up. lolz jks, but shouldnt it go *, /, +, -, to follow B.E.D.M.A.S form? other than taht simple concern, really a great guide xD

    • http://www.gyanguru.org Gyanguru

      Nice one :)…..
      Actually, this guide was meant to show how one can use “switch” to make a calculator based program. So, didn’t go into much details :)

  • Marzieh

    tank s but it s very easy.

  • Pravseh

    great but I have shorter method of making a calculator

    • http://www.gyanguru.org Gyanguru

      Please help us on same, it would be useful for our other friends on the site

  • Gokuldev 30

    nyc 1
     

  • Sandeep

    thanks buddy for helping me……………………..

  • Liju philip

    its good but try some short steps so anyone can understand and avail it.

  • Kadmiom

    thanks … 

  • Jabedatm

    tnx

  • Krishna Keshan

    I was looking for a program that would constantly take values and operate upon them. Any ideas? Something with Arrays?