-1

Im a freshman in college and new to programming entirely. I have to write a short program where the user inputs two operands, inputs a specific precision (say to 4 decimals points), then inputs an operator, such as /, +, or *. The expression is then calculated.

#include <stdio.h>

int main(int argc, const char * argv[]) {
int precision;
double a, b;
printf("Please enter desired precision");
scanf("%d", &precision);
printf("Please enter desired operands");
scanf("%lf %lf", &a, &b);
printf("%.*lf %.*lf", precision, a, precision, b);
printf("Please enter an expression\n");


return 0;
}

This is what I have thus far. Not sure how to get the user to input an operator without getting an error.

Guanxi
  • 3,103
  • 21
  • 38
Jamie
  • 1
  • 1
  • 1
  • 4
    Or use `char operator;` and `scanf( " %c", &operator )`. Note that there is a space before the `%` in `" %c"`. – user3386109 Sep 24 '15 at 17:51
  • the posted code fails to cleanly compile. Always enable all warnings when compiling. (for gcc, at a minimum, use: `-Wall -Wextra -pedantic`) Because the two parameters for the main() function declaration are not used, the compiler will output these warnings: unused parameter 'argc' and unused parameter 'argv[]' This can be fixed in a number of different ways including: `int main( void )` – user3629249 Sep 24 '15 at 21:49
  • when calling the function: `scanf()`, always check the returned value (not the parameter value) to assure the operation was successful. – user3629249 Sep 24 '15 at 21:51
  • when asking the user to enter a precision, prompt should contain what range of precision is allowed, say 0...6, then, after the precision value is successfully read, use a 'if' statement to verify the entered value is within the allowed range. – user3629249 Sep 24 '15 at 21:54
  • there are limitations on the available accuracy of floating point numbers. in general, the more precision wanted, the more limited the actual values are and still be representable via the printf() statement. This should be taken into consideration. see: for further details – user3629249 Sep 24 '15 at 22:05

1 Answers1

2

You can use a char type for the operator, note the space in the " %c" format. This will clean off any preceding whitespace, such as the newline left in the input buffer after the previous inputs, which would otherwise be accepted as a char.

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

void fatal(char *msg) {
    printf("\n%s\n", msg);
    exit (1);
}

int main(int argc, const char * argv[]) {
    int precision;
    double a, b;
    char operator;

    printf("Please enter desired precision ");
    if (scanf("%d", &precision) != 1)
        fatal("Illegal precision entered");

    printf("Please enter desired operands ");
    if (scanf("%lf %lf", &a, &b) != 2)
        fatal("Illegal operands entered");

    printf("Please enter the operator ");
    if (scanf(" %c", &operator) != 1)
        fatal("Illegal operator entered");

    switch(operator) {
        case '+': 
            printf("%.*f %c %.*f = %.*f",
                   precision, a, operator, precision, b, precision, a + b);
            break;
        case '-': 
            printf("%.*f %c %.*f = %.*f",
                   precision, a, operator, precision, b, precision, a - b);
            break;
        case '*': 
            printf("%.*f %c %.*f = %.*f",
                   precision, a, operator, precision, b, precision, a * b);
            break;
        case '/': 
            if (fabs(b) > 0.0)
                printf("%.*f %c %.*f = %.*f",
                   precision, a, operator, precision, b, precision, a / b);
            else
                fatal("Divide by zero");
            break;
        default:
            fatal("Illegal operator");
    }
    return 0;
}
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • a good answer, however, I would also be checking for overflow and underflow events. and outputting an appropriate error message. – user3629249 Sep 24 '15 at 21:58