samedi 28 mars 2015

C - get character issue



When I run the following code:



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


int main()
{
int n;
char y;

printf("Message\n");
fscanf(stdin, "%c", &y);

printf("Message\n");
fscanf(stdin, "%c", &y);

return 0;
}


I get this:



Message
{enter character}
Message


The problem is that I am not asked to enter a character twice even if there are 2 scanf functions. The output sould be this:



Message
{enter character}
Message
{enter character}






I have this issue for getc() too:



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


int main()
{
int n;
char y;

printf("Message\n");
y=getc(stdin);

printf("Message\n");
y=getc(stdin);

return 0;
}


Also, fflush() does not help:



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


int main()
{
int n;
char y;

printf("Message\n");
y=getc(stdin);

fflush(stdin);
fflush(stdout);

printf("Message\n");
y=getc(stdin);

return 0;
}


I've tried to fflush stdin, stdout, stdin+stdout (at the same time), but the result is still the same.




Aucun commentaire:

Enregistrer un commentaire