This is my computer science project,we were told to make any software we wish to make,so I thought about making a calculator,below is the code,but it doesn't seem to be functioning well,could anyone help and tell me what went wrong(I am using borland cpp 4.5 compiler,this program uses getch() to read the keystrokes)
#include<iostream.h>
#include<conio.h>
#include<math.h>
int perm=0; //if perm is zero the function enter is allowed
int ex=0; //if ex is 1,program is triggered to exit
float nmb; //stores formed number from enter()
char op; //stores the operator key from getch to use in switch case later
int display()
{
//clrscr();
cout<<"\n\t)*********************(";
cout<<"\n\t| 1 | 2 | 3 | c[e] |";
cout<<"\n\t-----------------------";
cout<<"\n\t| 4 | 5 | 6 | + |";
cout<<"\n\t-----------------------";
cout<<"\n\t| 7 | 8 | 9 | 0 | = |";
cout<<"\n\t-----------------------";
cout<<"\n\t| [m]c | [p]i | ^ |";
cout<<"\n\t-----------------------";
cout<<"\n\t| - | * | / | E |";
cout<<"\n\t----------------- [x] |";
cout<<"\n\t| | i |";
cout<<"\n\t| (esc) t |";
cout<<"\n\t)*********************(";
cout<<"\nenter value->";
}
int enter()
{
display();
unsigned long int num;
int x=0,p=0,dcp=0,limit=0,n=0,temp;
int opt[9]={42,43,45,47,61,94,112,101,109};
while(x!=1)
{
n=getch(); //stores the value from getch
if((n>=48 && n<=57) && limit<9)
{
limit++;
temp=n-48;
cout<<temp;
}
if(n==46 && p==0)
{
p=1;
cout<<(char)n;
}
if((n==88 || n==120) || n==27)
ex=1; //escape trigger
if(p==1 && limit<9)
dcp++; //decimal places counter
for(int i=0;i<9;i++)
{
if(n==opt[i]) //exit from function if a operator is entered
{
x=1;
clrscr();
display();
cout<<(char)n;
op=(char)n; //stores operator in global variable
}
}
num=num*10+temp; //forms a number out of digits entered
}
nmb=num/(pow(10,dcp)); //if decimal point has been entered,this calculates the decimal
perm=1;
}
int sum(int mem) //function for sum
{
enter();
mem+=nmb;
return mem;
}
int mul(int mem) //function for multiplication
{
enter();
mem=mem*nmb;
return mem;
}
int sub(int mem) //function for subtraction
{
enter();
mem=mem-nmb;
return mem;
}
int div(int mem) //function for division
{
enter();
mem=mem/nmb;
return mem;
}
void main()
{
int mem=0;
while(x!=1) //while the exit trigger is off
{
if(perm==0)
{
enter();
mem=nmb;
}
switch(op)
{
case '+':
{
mem=sum(mem);
break;
}
case '*':
{
mem=mul(mem);
break;
}
case '-':
{
mem=sub(mem);
break;
}
case '/':
{
mem=div(mem);
break;
}
case 'e':
{
clrscr();
break;
}
case 'm':
{
mem=0;
clrscr();
break;
}
case 'p':
{
clrscr();
mem=3.14;
cout<<mem;
break;
}
case '^':
{
enter(); //enter power variable
mem=pow(mem,nmb);
break;
}
case '=':
{
display();
cout<<mem;
enter();
break;
}
}
}
clrscr();
cout<<"\napplication has ended,you may close the console window";
}
these are the steps I wanted to work in the program
enter() --> store number and operator
in operator functions --> use operator and store the result in value mem
unless MC is used main shouldn't run enter() again at the beginning of the while loop