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

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int N, M, i, a, b, k, total = 0;
    fscanf(stdin, "%d %d", &N, &M);
    fprintf(stdout, "N: %d, M: %d\n", N, M);

    for(i=0 ; i<M ; i++){
        fflush(stdin);
        fscanf(stdin, "%d, %d, %d", &a, &b, &k);
        fflush(stdout);
        fprintf(stdout, "A: %d, B: %d, K: %d\n", a, b, k);
        total += (((b-a)+1)*k);
        fprintf(stdout, "total: %d\n", total);
    }
    total = total/N;
    fprintf(stdout, "%d\n", total);
    return 0;
}

The fscanf(stdin, "%d, %d, %d", &a, &b, &k); executes for the first time, but didnot execute after that.And even on the first time, only the value of variable a is correct, for variable b, and k are zero. Tried using fflush(stdin) to clear the stdin before each input, but it didnot work. Please help.

[update from comment:] No I didnot enter a comma.

alk
  • 69,737
  • 10
  • 105
  • 255
Love Bisaria
  • 167
  • 13
  • Have you checked the returnvalues? That said, flush()ing doesn't discard any data, it only forwards buffered data. Also, you don't show what you entered as input. – Ulrich Eckhardt Feb 15 '15 at 08:09
  • 1
    Do you have entered a comma? – BLUEPIXY Feb 15 '15 at 08:11
  • Note that [`fflush(stdin)`](http://stackoverflow.com/questions/2979209/using-fflushstdin) is undefined behaviour on most systems — Windows being the primary exception. – Jonathan Leffler Feb 15 '15 at 08:16
  • 2
    What are your inputs? And try removing the commas between `%d` in that `fscanf`. – Spikatrix Feb 15 '15 at 08:16
  • Please show the format of the input data. You should also test the return value from `fscanf()` each time you use it to ensure you got the correct number of values entered. `if (fscanf(stdin, "%d, %d, %d", &a, &b, &k) != 3) { …oops!… }`, etc. My best guess is that your data doesn't have the commas that your format string requires; that would explain the behaviour of 'only `a` is set on the first loop', at least. – Jonathan Leffler Feb 15 '15 at 08:18
  • @BLUEPIXY No I didnot enter a comma – Love Bisaria Feb 15 '15 at 08:20
  • 1
    Your input (`fscanf()`) format requires commas separating the numbers in the data; if you omit them, it will not work properly. – Jonathan Leffler Feb 15 '15 at 08:20
  • Comma was the issue. I changed the statement to `fscanf(stdin, "%d %d %d", &a, &b, &k);` and it works now. – Love Bisaria Feb 15 '15 at 08:23

2 Answers2

1
fscanf(stdin, "%d, %d, %d", &a, &b, &k);

Has the format string

"%d, %d, %d"

Which means that fscanf expects you to type a number and then a comma and then the space discards any number of whitespace characters including none(which is redundant as %d already skips them) and then expects a number,a comma, any number of whitespace characters including none and then a number.

You type in three numbers seperated by a space. The fscanf reads the first number and fails to scan a comma. The rest of the calls to fscanf expects a number first, but found a comma in the stdin, thus failing.

To fix it, change

fscanf(stdin, "%d, %d, %d", &a, &b, &k);

To

fscanf(stdin, "%d %d %d", &a, &b, &k);

Or

fscanf(stdin, "%d%d%d", &a, &b, &k);
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
-1
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int N, M, i, a, b, k, total = 0;
    fscanf(stdin, "%d %d", &N, &M);
    fprintf(stdout, "N: %d, M: %d\n", N, M);

    for(i=0 ; i<M ; i++){
        fflush(stdin);
        fscanf(stdin, "%d %d %d", &a, &b, &k);
        fflush(stdout);
        fprintf(stdout, "A: %d, B: %d, K: %d\n", a, b, k);
        total += (((b-a)+1)*k);
        fprintf(stdout, "total: %d\n", total);
    }
    total = total/N;
    fprintf(stdout, "%d\n", total);
    return 0;
}
Love Bisaria
  • 167
  • 13
  • Please also add the tests to check that `fscanf()` works each time. As you've already discovered, people make mistakes with input formats; your program should detect problems with input formats. – Jonathan Leffler Feb 15 '15 at 08:25