I am trying to make a basic console script for a calculator as a starter C project. When I type anything after the printf("\nOperator: ");
Nothing happens it just exits the program. This shouldn't happen because of the switch
statement at line 45
Language: C
Compiler: GCC
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <Windows.h>
int main()
{
// Variables
int x;
int y;
char op = ' ';
int sum;
int difference;
int product;
int quotient;
int sqrta;
int logarithm;
int exponent;
// Functions
printf("**FxrMath**\n");
printf("");
printf("Options:\n");
printf("(A)dd\n");
printf("(S)ubtract\n");
printf("(M)ultiply\n");
printf("(D)ivide\n");
printf("S(q)uare Root\n");
printf("(L)ogarithm\n");
printf("(P)ower (x^y)\n");
printf("(H)ypotoneuse Calculator");
printf("\n");
printf("\n**OPERATOR MUST BE LOWERCASE**");
printf("\n");
printf("\nOperator: ");
scanf("%d", &op);
switch (op) // Checking what to do
{
case 'a':
printf("First Number: ");
scanf("%d", &x);
printf("\nSecond Number: ");
scanf("%d", "&d", &y);
int sum = x + y;
printf("\n%dSum: ", sum);
Sleep(2500);
break;
case 's':
printf("First Number: ");
scanf("%d", &x);
printf("\nSecond Number: ");
scanf("%d", &y);
int difference = x - y;
printf("\n%dDifference: ", difference);
Sleep(2500);
break;
case 'm':
printf("First Number: ");
scanf("%d", &x);
printf("\nSecond Number: ");
scanf("%d", &y);
int product = x * y;
printf("\n%dProduct: ", product);
Sleep(2500);
break;
case 'd':
printf("First Number: ");
scanf("%d", &x);
printf("\nSecond Number: ");
scanf("%d", &y);
int quotient = x / y;
printf("\n%dQuotient: ", quotient);
Sleep(2500);
break;
case 'q':
printf("Number: ");
scanf("%d", &x);
int sqrta = sqrt(x);
printf("\n%dSquare Root: ", sqrta);
Sleep(2500);
break;
case 'l':
printf("First Number: ");
scanf("%d", &x);
int logarithm = log(x);
printf("\n%dLogarithm: ", logarithm);
Sleep(2500);
break;
case 'p':
printf("First Number: ");
scanf("%d", &x);
printf("\nSecond Number: ");
scanf("%d", &y);
int exponent = pow(x, y);
printf("\n%dExponent: ", exponent);
Sleep(2500);
break;
case 'h':
int a;
int b;
printf("\nA: ");
scanf("%d", a);
printf("\nB: ");
scanf("%d", b);
double hypotenuse = sqrt((a*a) + (b*b));
printf("\n%dHypotenuse: ", hypotenuse);
Sleep(2500);
break;
default:
break;
}
return 0;
}
The variable op is declared as having the type char
char op = ' ';
So this call of scanf
scanf("%d", &op);
is incorrect. Firstly the function expects an argument of the type int and secondly entering a symbols will not be successful.
Instead write
scanf( " %c", &op );
Pay attention to the leading space in the format string. It allows to skip automatically white space characters in the input buffer.
Also such a call of scanf
scanf("%d", "&d", &y);
also is incorrect. It seems you mean
scanf("%d", &y);
And these variable declarations
int sum;
int difference;
int product;
int quotient;
int sqrta;
int logarithm;
int exponent;
are redundant because in each code snippet under a case label you declared a corresponding variable one more as for example
case 'a':
printf("First Number: ");
scanf("%d", &x);
printf("\nSecond Number: ");
scanf("%d", "&d", &y);
int sum = x + y;
^^^^^^^
And such declarations must be enclosed in block scopes as for example
case 'a':
{
printf("First Number: ");
scanf("%d", &x);
printf("\nSecond Number: ");
scanf("%d", "&d", &y);
int sum = x + y;
//...
break;
}