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

php - How to serve a file for download on WordPress -

how to share virtual sharepoint server and get it in host windows and visual studio? -