caluclator

Feb 28, 2009 at 7:51pm
i have run time error at running this c code &i don't know why
#include<iostream>
void main(void)
{
float a;
float b;
char c;
float d;
do
{
printf("please enter the operation or press x to exit");
scanf("%c",c);
if(c!='x')
{
printf("please enter first number");
scanf("%f",a);
printf("please enter second number");
scanf("%f",b);
switch(c)
{
case'+':
d=a+b;
printf("result=%d \n",d);
break;
case'-':
d=a-b;
printf("result=%d \n",d);
break;
case'*':
d=a*b;
printf("result=%d \n",d);
break;
case'/':
if(b==0)
{
Printf("can't divide by 0");
}
else
{
d=a/b;
printf("result=%d \n",d);
}
break;
default:
printf("invalid op \n");
}
}
}
while(c!='x');
}
Feb 28, 2009 at 8:39pm
1. There was a "Printf()" instead of "printf()".
2. scanf() takes a pointer to a variable.
3. To print floats, use "%f", not "%d".
Below is the corrected code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<iostream>

int main(){
	float a;
	float b;
	char c;
	float d;
	do{
		printf("please enter the operation or press x to exit");
		//Here.
		scanf("%c",&c);
		if(c!='x'){
			printf("please enter first number");
			//Here.
			scanf("%f",&a);
			printf("please enter second number");
			//Here.
			scanf("%f",&b);
			switch(c){
				case'+':
					d=a+b;
					//Here.
					printf("result=%f \n",d);
					break;
				case'-':
					d=a-b;
					//Here.
					printf("result=%f \n",d);
					break;
				case'*':
					d=a*b;
					//Here.
					printf("result=%f \n",d);
					break;
				case'/':
					if(b==0){
						printf("can't divide by 0");
					}else{
						//Here.
						d=a/b;
						printf("result=%f \n",d);
					}
					break;
				default:
					printf("invalid op \n");
			}
		}
	}
	while(c!='x');
	return 0;
}


Your code is keeping left-over enters.
Feb 28, 2009 at 9:12pm
thanks a lot
thank u very much
Mar 1, 2009 at 9:18pm
hi im new on this of programming and im trying to make a scientific calculator, a little more advance of the one above, but i have found hard to do it. here is my code, thanks in advance for the help and hoping to add more features to it like add, sub and multiply matrices up to 3x3. Also im not sure of the second switch. Option 6 (TRIGONOMETRICS). thks again.



#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <math.h>
#define PI 3.14159265


using namespace std;

int main()
{
double num,num2;
double result;
int num3;
int num4;
char choice,trigonometrics;
for (;;){
do {
cout<<"Scientific calculator. \n\n\n";
cout<<"Please choose an option by entering the number, press q to quit\n";
cout<<"1 - Addition\n";
cout<<"2 - Subtraction\n";
cout<<"3 - Division\n";
cout<<"4 - Multiplication\n";
cout<<"5 - Remainder\n";
cout<<"6 - trigonometrics\n";
cout<<"7 - Logarithm\n";
cout<<"8 - Exponential\n";
cin>>choice;
} while ( choice < '1' || choice > '15' && choice != 'q');
if (choice == 'q') break;
switch (choice) {
case '1':
cout<<"Please enter a number\n";
cin>>num;
cout<<"Another number to be added\n";
cin>>num2;
cout<<num + num2;
cout<<"\n\n\n";
break;
case '2':
cout<<"Please enter a number\n";
cin>>num;
cout<<"Another number to be subtracted\n";
cin>>num2;
cout<<num - num2;
cout<<"\n\n\n";
break;
case '3':
cout<<"Please enter a number\n";
cin>>num;
cout<<"Another one to be divided\n";
cin>>num2;
cout<<num / num2;
cout<<"\n\n\n";
break;
case '4':
cout<<"Please enter a number\n";
cin>>num;
cout<<"Another one to be multiplied\n";
cin>>num2;
cout<<num * num2;
cout<<"\n\n\n";
break;
case '5':
cout<<"Please enter a number\n";
cin>>num3;
cout<<"Another one \n";
cin>>num4;
cout<<num3 % num4;
cout<<"\n\n\n";
break;
case '6':
do
{
cout<<"a - Sine\n";
cout<<"b - Cosine\n";
cout<<"c - Tangent\n";
cout<<"d - Arc sine\n";
cout<<"e - Arc cosine\n";
cout<<"f - Arc tangent\n";
cout<<"Please enter a letter\n";
cin>>trigonometrics;
}
while ( trigonometrics < 'a' || choice > 'f');
switch (trigonometrics){
case 'a':
cout<<"Please enter a number\n";
cin>>num;
result=sin (num*PI/180);
printf ("The sine of %lf degrees is %lf.\n", num, result );
break;
case 'b':
cout<<"Please enter a number\n";
cin>>num;
result=cos (num*PI/180);
printf ("The cosine of %lf degrees is %lf.\n", num, result );
break;
case 'c':
cout<<"Please enter a number\n";
cin>>num;
result=tan (num*PI/180);
printf ("The tangent of %lf degrees is %lf.\n", num, result );
break;
case 'd':
cout<<"Please enter a number\n";
cin>>num;
result=asin (num)*180/PI;
printf ("The arc sine of %lf is %lf degrees\n", num, result );
break;
case 'e':
cout<<"Please enter a number\n";
cin>>num;
result=acos(num)*180/PI;
printf ("The arc cosine of %lf degrees is %lf.\n", num, result );
break;
case 'f':
cout<<"Please enter a number\n";
cin>>num;
result=atan(num)*180/PI;
printf ("The arc tangent of %lf degrees is %lf.\n", num, result );
break;
default;
cout<<"That is not an option";
return 0;

}

case '7':
cout<<"Please enter a number\n";
cin>>num;
result = log (num);
printf ("ln(%lf) = %lf\n", num, result );
break;
case '8':
cout<<"Please enter a number\n";
cin>>num;
result = exp (num);
printf ("The exponential value of %lf is %lf.\n", num, result );
break;

default:
cout<<"That is not an option";

}

}
return 0;


}
Topic archived. No new replies allowed.