1

i want to make 2 lines calculator program but after first printf,scanf it skipped the second statement

#include <stdio.h>
int main ()
{
    int a,b,c,d,abcd;
    int e,f,g,h,efgh;
    
    printf("1. "); 
    scanf("(%d+%d)x(%d-%d)",&a,&b,&c,&d);
    printf("2. "); 
    scanf("(%d+%d)x(%d-%d)",&e,&f,&g,&h);
    
    abcd=(a+b)*(c-d);
    efgh=(e+f)*(g-h);
    
    printf("%d %d %d",abcd,efgh);
    return 0;
}

and i want to make the program like this:

input
 1. (1+2)x(3-4)
 2. (5+6)x(7-8)
output
-3 -11
MiiReal
  • 11
  • 2

3 Answers3

1

I would strongly suggest you read a line of input using fgets, then use sscanf to parse out the info you need. You will also want to check the return value of sscanf to ensure all four values were read. In this case I have made successful input required to break out of an infinite loop (for (;;) { ... }).

Please note also that all variables do not need to de declared at the start of a function. Instead they can be declared and initialized at their point of use. In this scenario, I have declared abcd and efgh later in main.

#include <stdio.h>

#define LINE_LEN 256

int main ()
{
    int a, b, c, d;
    int e, f, g, h;

    char line[LINE_LEN] = {0};

    for (;;) {
        printf("1. ");
        fgets(line, LINE_LEN, stdin);
        if (sscanf(line, "(%d+%d)x(%d-%d)", &a, &b, &c, &d) == 4)
            break;
 
        fprintf(stderr, "Bad input. Try again.");
    }  

    for (;;) {
        printf("1. ");
        fgets(line, LINE_LEN, stdin);
        if (sscanf(line, "(%d+%d)x(%d-%d)", &e, &f, &g, &h) == 4)
            break;
 
        fprintf(stderr, "Bad input. Try again.");
    } 

    int abcd = (a + b) * (c - d);
    int efgh = (e + f) * (g - h);

    printf("%d\n", abcd);
    printf("%d\n", efgh);

    return 0;
}
Chris
  • 26,361
  • 5
  • 21
  • 42
  • I like your answer, I tried to cover more scenarios in mine but yours is way clearer. – Fra93 Oct 05 '22 at 15:04
  • 1
    As a newcomer to Stack Overflow, please say "thank you" by marking the answer you feel best answers your question as "accepted." – Chris Oct 05 '22 at 15:14
0

By some suggestions here I copied the function to read from the stdin in C and adapted your example. This is a safe way to handle stdin from a C application.

Also I tried your code and I believe that what was wrong is that some newline gets "trapped" in the stdin, hence the second scanf returns before you can write anything. That's why this solution.

#include <stdio.h>
#include <string.h>

#define OK       0
#define NO_INPUT 1
#define TOO_LONG 2
static int getLine (char *prmpt, char *buff, size_t sz) {
    int ch, extra;

    // Get line with buffer overrun protection.
    if (prmpt != NULL) {
        printf ("%s", prmpt);
        fflush (stdout);
    }
    if (fgets (buff, sz, stdin) == NULL)
        return NO_INPUT;

    // If it was too long, there'll be no newline. In that case, we flush
    // to end of line so that excess doesn't affect the next call.
    if (buff[strlen(buff)-1] != '\n') {
        extra = 0;
        while (((ch = getchar()) != '\n') && (ch != EOF))
            extra = 1;
        return (extra == 1) ? TOO_LONG : OK;
    }

    // Otherwise remove newline and give string back to caller.
    buff[strlen(buff)-1] = '\0';
    return OK;
}

int main ()
{
    int a,b,c,d,abcd;
    int e,f,g,h,efgh;
    
    int rc;
    char buff[20];
    
    rc = getLine ("1. ", buff, sizeof(buff));
    if (rc == NO_INPUT) {
        // Extra NL since my system doesn't output that on EOF.
        printf ("\nNo input\n");
        return 1;
    }

    if (rc == TOO_LONG) {
        printf ("Input too long [%s]\n", buff);
        return 1;
    }
    
    sscanf(buff, "(%d+%d)x(%d-%d)",&a,&b,&c,&d);
    
    rc = getLine ("2. ", buff, sizeof(buff));
    if (rc == NO_INPUT) {
        // Extra NL since my system doesn't output that on EOF.
        printf ("\nNo input\n");
        return 1;
    }

    if (rc == TOO_LONG) {
        printf ("Input too long [%s]\n", buff);
        return 1;
    }
    
    sscanf(buff, "(%d+%d)x(%d-%d)",&e,&f,&g,&h);
    
    printf("Check \n");
    printf("%d %d %d %d\n" ,a,b,c,d);
    printf("%d %d %d %d\n" ,e,f,g,h);
    
    abcd=(a+b)*(c-d);
    efgh=(e+f)*(g-h);
    
    printf("Results: %d %d \n",abcd,efgh);
    return 0;
}
Fra93
  • 1,992
  • 1
  • 9
  • 18
-1

scanf is wrong.. scanf("%typeOfFirstVariable %typeOfSecondVariable...", &firstVariable, &secondVariable...);

int a,b,c,d,abcd;
    int e,f,g,h,efgh;
    
    printf("1. "); 
    scanf("%d %d %d %d)",&a,&b,&c,&d);
    
    ...
    return 0;
Ged0jzn4
  • 80
  • 6
  • Or if you want to take it like this: '1. (1+2)x(3-4)' then as an input use string, and then grab first element as s[1], second as s[3]... – Ged0jzn4 Oct 05 '22 at 14:47
  • so, how to read specificly the input? example: (1+2)x(3-4) how to read 1 as &a, 2 as &b, 3 as &c, 4 as &c – MiiReal Oct 05 '22 at 14:54
  • This answer reads as a comment rather than an answer without some elaboration. – Chris Oct 05 '22 at 15:09