Member Avatar for anu07

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

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)

1) In English, there is always a SPACE after punctuation
2) As for it not functioning well, try unplugging the computer from the wall socket, then the lights won't go out when you run the program

Now I admit I'm guessing on #2 what "doesn't work well" means since you didn't bother to tell us. If I'm wrong, try explaining the problem.

Member Avatar for anu07

Sorry for that,well anyways the problem is,if i divide a number by another,the output is always zero...or perhaps if I add two numbers,the output is usually a random negative number.
In the program,while the number is being is stored in variable num which has a var type of unsigned long int,but which itself is being stored in a float variable nmb....would that be a reason for the problem?unsigned long int to float?

commented: English, there is always a SPACE after punctuation -3

1) In English, there is always a SPACE after punctuation!!!!!!
2) Dividing ints give you ints. 3/4=0 not 0.75. 8/6=1 not 1.5

During the division make sure one of the values is cast to float (float) 8 / 6 Also, nmb? Please reread the Member Rules -- Keep it Clear, first and last points.

Member Avatar for anu07

Thank you,and to not be rude but still informative,there is no standard English rule for use all over the world,at least not that comma thing you just spoke of :),thanks for your time again :)..........okay that was rude,but umm that's the thing,and thank you very very much for helping :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.