I'm trying to take user input, as the user wants.
- First user enter number, I print it.
- Second user may enter any thing. I am trying names, or single words. Reading with them fgets, with 100 size.
Something weird is going on with new line character. I'm simply trying to print input, but this buggy code takes input one less than what I intend to do.
Code:
int main(int argc, char *argv[]){
char array[100]; //store input.. over write it every time...
int test=0; //test case .. this decides how many inputs user will give
scanf("%d",&test);
printf("%d\n",test); //print your input
while(test){
fgets(array,100,stdin); //why it doesn't stop?
printf("Value is %s\n",array);
test--;
}
return 0;
}
Output:
4
4
Value is
jeev
Value is jeev
user
Value is user
lol
Value is lol
There's one new line character with first "value is"
How do I make fgets stop, to take values that is input? It's printing first time without stopping.
Should I use fflush or similar things?
I can set the new line character to '\0', but my concern is, why doesn't stdin stop?
I can make new line to null character as:
Code:
if(strlen(array) && array[strlen(arrray)-1] == "\n"){
array[strlen(arrray)-1] == "\0"
}
Thanks.