0

This one is pretty strange (or at least I can't figure out what triggers it). I have the piece of code below, but there seems to be a bug with the second call of scanf . It simply gets skipped. So the input should be something like

10 3
fbfbfbfbfb
3 1 2 3
3 4 5 6
4 7 8 9 10

but after I enter

10 3
fbfbfbfbfb

I get the (wrong) result, I do not get asked for the other 3 lines. Thanks in advance!

P.S.: The variables' names are not in English :)

#include <stdio.h>

#define N 200

int main(void) {

    unsigned short n, k, i, j, elevi_grupa, elev_curent, 
                   fete = 0, baieti = 0, corect = 1;
    int sir[N];

    scanf("%hu %hu", &n, &k);

    for(i = 0; i < n; ++i)
        sir[i] = getchar();

    for(i = 0; i < k; ++i) {

        scanf("%hu", &elevi_grupa);

        if(elevi_grupa < n % k || elevi_grupa > n % k + 1)
            corect = 0;

        for(j = 0; j < elevi_grupa; ++j) {

            scanf("%hu", &elev_curent);

            if(sir[elev_curent - 1] == 'f')
                ++fete;
            else
                ++baieti;
        }

        if(fete - baieti < -1 || fete - baieti > 1)
            corect = 0;

        printf("%hu %hu\n", baieti, fete);
        fete = 0;
        baieti = 0;
    }

    if(corect)
        printf("DA");
    else
        printf("NU");

    return 0;
}
2
Contributors
2
Replies
5
Views
6 Years
Discussion Span
Last Post by creeps
0

Its probably because getchar() is leaving a newline character in the buffer.

After your for statement put a fgetc(stdin).

for(i = 0; i < n; ++i)
        sir[i] = getchar();

	fgetc(stdin);

Daniweb has a great sticky on numerous problems and solution when dealing with input buffers.

Edited by gerard4143: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.