C language:scanning of input string is skipped inside for loop -


this question has answer here:

whenever code executed, contents inside loop not executing first time, i.e., when i=0. loop executes after i=0, i.e., i=1,2,3,..n-1. can explain what's wrong here?

#include <stdio.h> #include <stdlib.h> int main(int argc, char** argv) {     char string[30][100];     int n,i;     scanf("%d",&n);     for(i=0;i<n;i++){        gets(string[i]);        printf("%s\n",string[i]);     }     getch();     return (exit_success); } 

for(i=0;i<n;i++) {    fflush(stdin);   //clears buffer.insert here.    gets(string[i]);    printf("%s\n",string[i]); } 

after execution of scanf("%d",&n); leaving '\n'(when press enter key @ end) character in buffer.which should cleared before execution of other input operation.fflush(stdin) clear keyboard buffer. there many other ways it.but using fflush(stdin) easy beginners.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -