0

I am trying to learn c Language, and I have to create a calculator, the thing is, if I don't type anything and press the enter key it should print out an error, I have tried to do it with scanf but it is not working.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    float a,b,c;
    char op;
    int q=1;


    while(q=1){
   scanf("%f%c%f",&a,&op,&b);



    if (scanf("%f%c%f",&a,&op,&b)=='\n')
    {
        printf("error");

    }


switch (op)
    {
        case '+':c=a+b;
        break;
        case '-':c=a-b;
        break;
        case'*':c=a*b;
        break;
        case'/':c=a/b;
        break;
        default:printf("error");
        q=2;
        break;
    }
    {printf("%f\n",c);}

}}

Thomas Smyth - Treliant
  • 4,993
  • 6
  • 25
  • 36
  • You can use getchar. – Vlad from Moscow Nov 01 '22 at 08:12
  • Pay attention to that it seems you mean the equality operator == instead of the assignment operator = in the while loop while(q=1){ – Vlad from Moscow Nov 01 '22 at 08:15
  • Take the input as a string, and process that with `sccanf`. If the user just types **Enter** that is what will be in the string. Asides: use `double`, the `float` is useless for even a basic 8-digit calculator. Also put a space in front of the `%c` which will make the input tolerant of spaces. You might end up with `sscanf(str, "%lf %c%lf", &a, &op, &b);` – Weather Vane Nov 01 '22 at 08:44
  • Does this answer your question? [How to check if user enters blank line in scanf in c](https://stackoverflow.com/questions/42265038/how-to-check-if-user-enters-blank-line-in-scanf-in-c) – Ruud Helderman Nov 01 '22 at 09:17
  • `scanf` returns the number of conversions it made. If your format string is `%f%c%f` and it is able to match only the first 2, it will return 2. With that format string, the only possible return values are 0, 1, 2, 3 and EOF. It is not possible for that `scanf` to return `'\n'`, which is (almost certainly) 10. – William Pursell Nov 01 '22 at 10:08
  • `while(q=1)` ??!?!?! – William Pursell Nov 01 '22 at 10:10
  • [What can I use for input conversion instead of `scanf`?](https://stackoverflow.com/questions/58403537) – Steve Summit Nov 02 '22 at 11:27

1 Answers1

0

Try to scan for a newline using a scanset %1[\n].
Another scanset %*[^\n] will scan and discard everything that is not a newline.
Another approach would be to use fgets to read a line and then parse the line with sscanf.

#include <stdio.h>
#include <stdlib.h>
int main ( void) {
    char newline[2] = "";
    char op = 0;
    int scanned = 0;
    double a = 0.0;
    double b = 0.0;
    double c = 0.0;

    while ( 1) {
        printf ( "input a number, an operator and a number\n");
        printf ( "        or just enter to quit\n");
        if ( 1 == scanf ( "%1[\n]", newline)) { // try to scan a newline
            printf ( "done\n");
            break;
        }
        if ( 3 != ( scanned = scanf("%lf %c%lf", &a, &op, &b))) {
            printf ( "bad input error\n");
            printf ( "try again\n");
        }

        if ( EOF == scanned) {
            printf ( "EOF error\n");
            break;
        }

        scanf ( "%*[^\n]"); // scan and discard up to newline
        scanf ( "%1[\n]", newline); // scan a newline

        if ( 3 == scanned) {
            switch (op) {
                case '+':
                    c = a + b;
                    break;
                case '-':
                    c = a - b;
                    break;
                case '*':
                    c = a * b;
                    break;
                case '/':
                    c = a / b;
                    break;
                default:
                    printf ( "bad op error\n");
                    printf ( "try again\n");
                    op = 0;
                    break;
            }
            if ( op) {
                printf ( "%f\n", c);
            }
        }
    }
    return 0;
}
user3121023
  • 8,181
  • 5
  • 18
  • 16