C Scanf in a loop -
it works first time, when execute on 2nd time, skip sec scanf function. after google several pages, noticed behavior of scanf function add together \n in buffered, solved this, added fflush(stdin) after scanf , did worked, when executed on 2nd times, give me wrong result. can guide me problem of programme ?
#include <stdio.h> #include <stdlib.h> int main() { char userinput[50]; int = 0; int exit; do{ printf("please come in string (less 50 character): "); scanf("%[a-z,a-z, ,]s",&userinput); while(userinput[i] != '\0' && i<50) { i++; } if (i==50) printf("the string long\n"); else printf("the length of string %d\n",i); printf("to continue, please key in numbers other 0: "); scanf("%d",&exit); fflush(stdin); }while(exit !=0); system("pause"); homecoming 0; }
you don't utilize ampersand read array. alter line to:
scanf("%[a-z,a-z, ,]s",userinput);
also exit char array, not integer. either alter exit int or alter scanf to: scanf("%s",exit);
. , trust me exit is not name variable in c
.
c
No comments:
Post a Comment