# C++: A very simple calculator



## The Sparrow (Feb 28, 2005)

Hello to all members...
this a very simple code for a program that works like a very simple calculator which add, subtract, multiply and divide.
if you would please take a look at it and tell me if there is something wrong... or another more appropriate way to do it.

this my code:-
#include<iostream>
int main ()
{
int x,y;
int sum,sub,mult,divid;
int code;
cout<<"Please Enter Your Two Values:";
cin>>x>>y;
cout<<"The code to add is 1"<<endl;
cout<<"The code to subtract is 2"<<endl;
cout<<"The code to multiply is 3"<<endl;
cout<<"The code to divide is 4"<<endl;
cout<<"Please give me the code?"<<endl;
cin>>code;
for (int i=1;i>-1;++i){
switch (code){
case 1:
sum=x+y;
cout<<"Sum="<<sum<<endl;
break;
case 2:
sub=x-y;
cout<<"Sub="<<sub<<endl;
break;
case 3:
mult=x*y;
cout<<"Mult="<<mult<<endl;
break;
case 4:
divid=x/y;
cout<<"Divid="<<divid<<endl;
break;
default:
cout<<"Invalid Data"<<endl;
}
cout<<"Please Enter Your Two Values:";
cin>>x>>y;
cout<<"Please give me the code?"<<endl;
cin>>code;
}
char stop;
cin>>stop;
return 0;
}

I hope that I didn't bother you but I really want to learn the C++ programming 

The Sparrow


----------



## Anne Troy (Feb 14, 1999)

I changed your title to hopefully attract the right kind of programmers.  Good luck!


----------



## gab00n (Feb 24, 2005)

It's ok for a simple calculator. If you really want to learn how the computer works you should try make a reverse polish notation calculator. You will use a stack for this kind of calculator so it will be a good learning experience for you.

Here is an example of a postfix expression: 23 4 + 67 85 + / 
in infix it would be (23 + 4) / (67 + 85)
When you encounter a value you push it onto the stack, when you get to the + you will have to use top and pop to store the value in a temp val and delete it off the stack to get next val, repeat. Then finally perform calculation and push answer onto the stack again. With a stack the first value in is the last value out. 

I have the code from a previous assignment so if you don't know how something works i'll show you how i did it.


----------



## The Sparrow (Feb 28, 2005)

Dear "Dreamboat" thank you for passing by...

Dear "gab00n" Thank you for your concern... and YES I would love to see the code from your previous assignment.

The Sparrow


----------



## The Sparrow (Feb 28, 2005)

Dear "gab00n" I also have another code that stands for the idea of Recursion...
so if you would please take a look at it...

#include <iostream>
using namespace std;

int factorial(int n)
{
if (n>1){
return n*factorial(n-1);
}
else {
return 1;
}
}

int main ( )
{
int n;
int fact;

cout<<"Enter a number:";
cin>>n;

fact=factorial;
cout<<n<<"!="<<fact<<endl;
return 0;
}

so the main idea of this code is to find the factorial for a particular number which is entered by the user... then a function will be called
this function is a recursive function right?
so it will keep executing until all the value pops out from the stack...
am I right?
if I was wrong, please correct me

The Sparrow


----------



## gab00n (Feb 24, 2005)

Well there is no stack involved here and yes it is a recursive function because it calls upon itself inside the function. The function should return the final value of a sequence of calculations.


----------



## The Sparrow (Feb 28, 2005)

thank you dear "gab00n" for the quick reply... 

The Sparrow


----------



## MrFixItMan (Mar 13, 2005)

For anyone, like me, that wants to learn a programming language. what would be a good one to start with, and where can I find it?


----------



## gab00n (Feb 24, 2005)

I would say Python. www.python.org


----------



## MrFixItMan (Mar 13, 2005)

@gab00n
Thank you...


----------

