Reading and printing strings in C -


i want scan , print 2 strings 1 after in loop.but cannot it.only 1 string gets scanned , printed if use loop.if try print without loop 2 "gets()" work properly.

#include <stdio.h>  int main() { int t,i,j; char name1[100]; char name2[100]; scanf("%d",&t); for(i=0; i<t; i++) {     printf("case %d: ",i+1);     //scanf("%[^\n]s",name1);             gets(name1);     /*for(j=0; j<strlen(name1); j++)     {         printf("%c",name1[j]);     }*/     puts(name1);     //scanf("%[^\n]s",name2);     gets(name2);     /*for(j=0; j<strlen(name2); j++)     {         printf("%c",name2[j]);     }*/     puts(name2); } } 

here go. use fflush(stdin). take 2 inputs , print them 1 after another.

#include<stdio.h>  int main() { int t,i,j; char name1[100]; char name2[100]; scanf("%d",&t); for(i=0; i<t; i++) {     printf("case %d: ",i+1);     fflush(stdin);     gets(name1);      gets(name2);      puts(name1);      puts(name2); } return 0; } 

edit: suggested in comment below, using gets() not advisable if not know number of characters wish read.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -