Home > Enterprise >  Looping Calculator in C
Looping Calculator in C

Time:09-30

I am just making a simple calculator and would like to know how could I make my calculator loop back to the start when an invalid operator/float is entered; to ask the user to input everything again. Basically restarting it.

#include <stdlib.h>
#include <stdio.h>
#include <math.h>

int main()
{
    double num1;
    double num2;
    char op;

    printf("Input a number: ");
    scanf("%lf", &num1);
    printf("Enter operator ( ,-,*,/,^): ");
    scanf(" %c", &op);
    printf("Enter the second number: ");
    scanf(" %lf", &num2);

    if (op == ' ')
    {
        printf("%f", num1   num2);
    } 
    else if (op == '-')
    {
        printf("%f", num1 - num2);
    }
    else if (op == '/')
    {
        printf("%f", num1 / num2);
    }
     else if (op == '*')
    {
        printf("%f", num1 * num2);
    } 
    else if (op == '^')
    {
        printf("%f", pow(num1,num2));
    }
    else
    {
        printf("Invalid operator entered");
    }
    return 0;
}
                                

CodePudding user response:

Insert the whole logic in an infinite loop like below.

while(1) {
    printf("Input a number: ");
    scanf("%lf", &num1);
    printf("Enter operator ( ,-,*,/,^): ");
    scanf(" %c", &op);
    printf("Enter the second number: ");
    scanf(" %lf", &num2);

    if (op == ' ')
    {
        printf("%f", num1   num2);
    } 
    else if (op == '-')
    {
        printf("%f", num1 - num2);
    }
    else if (op == '/')
    {
        printf("%f", num1 / num2);
    }
     else if (op == '*')
    {
        printf("%f", num1 * num2);
    } 
    else if (op == '^')
    {
        printf("%f", pow(num1,num2));
    }
    else
    {
        printf("Invalid operator entered");
    }
}

CodePudding user response:

I would do it another way to avoid \n scanf problems

#define MAXNUMSTR 100

int calc(void)
{
    char line[MAXNUMSTR];
    double num1;
    double num2;
    double res;

    do
    {
        printf("\nInput first number: ");
        if(!fgets(line, MAXNUMSTR -1, stdin)) return 0;
    }while(sscanf(line, "%lf", &num1) != 1);
    do
    {
        printf("\nInput second number: ");
        if(!fgets(line, MAXNUMSTR -1, stdin)) return 0;
    }while(sscanf(line, "%lf", &num2) != 1);
    printf("\nEnter operator ( ,-,*,/,^, x): ");
    if(!fgets(line, MAXNUMSTR -1, stdin)) return 0;

    switch(line[0])
    {
        case ' ':
            res = num1   num2;
            break;
        case '-':
            res = num1 - num2;
            break;
        case '*':
            res = num1 * num2;
            break;
        case '/':
            if(num2 == 0.0) 
            {
                printf("\nDivision by zero\n");
                return 1;
            }
            res = num1 / num2;
            break;
        case '^':
            res = pow(num1, num2);
            break;
        case 'x':
            return 0;
        default:
                printf("\nInvalid operation\n");
                return 1;

    }
    printf("\n%f %c %f = %f\n", num1, num2, res);
    return 1;
}

int main(void)
{
    while(calc());
    return 0;
}
  •  Tags:  
  • c
  • Related