Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22130

Switch/Case exiting the code working when I type in the console C

$
0
0

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;}

Viewing all articles
Browse latest Browse all 22130

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>