0

I want to scan and print two strings one after another in a loop.But I cannot do it.Only one string gets scanned and printed if i use the loop.If i try to print without the loop then the two "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);
}
}

3 Answers3

1

Here you go. Use fflush(stdin). It will take two inputs and print them one after the 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: As suggested in the comment below, using gets() is not advisable if you do not know the number of characters you wish to read.

Utsav T
  • 1,415
  • 2
  • 21
  • 39
  • 4
    It is worth recommending against using `gets` whenever possible. Not everyone is aware of the fact that `gets` is [considered *extremely* unsafe](http://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used). – Jongware Jun 13 '15 at 13:53
  • 2
    See [Using `fflush(stdin)`](http://stackoverflow.com/questions/2979209/using-fflushstdin) for a discussion of the (de)merits of using `fflush(stdin)`. – Jonathan Leffler Jun 13 '15 at 13:59
  • Then what should I do to copy and paste strings generally? @Jonhware – saqib shafin Jun 13 '15 at 14:01
  • @saqibshafin See the links in the comments. Links posted here typically explain why something is wrong as well as how to make them correct. –  Jun 13 '15 at 14:07
  • @saqibshafin Any string copying routine where you can place a limit on how many characters to copy is generally *much* safer than an unchecked routine such as `gets`. Buffer overflows are nasty – Alejandro Jun 13 '15 at 14:21
1

After taking testcase from the user, next line gets() function will take the '\n' you have to ignore the scenario.

Here's a tricky solution of this problem. Just use '\n' after %d in scanf function. scanf("%d\n",&T);

#include <stdio.h>

int main(void) {
    char s1[100],s2[100];
    int i,T;
    scanf("%d\n",&T);
    for(i = 0; i < T; i++){
        printf("Case %d: ",i+1);
        gets(s1);
        puts(s1);
        gets(s2);
        puts(s2);
    }
    return 0;
}
Prosen Ghosh
  • 615
  • 7
  • 15
0

You do not terminate your prints. stdout is buffered. Print is only performed after a "\n" or explicit flush. try something around the lines:

#include <stdio.h>

int main()
{
    int T,i,j;
    char name1[100];
    char name2[100];
    scanf("%d",&T);
    for(i=0; i<T; i++)
    {
#ifdef BAD_CODE
        printf("Case %d: ",i+1);
        gets(name1);
        puts(name1);
        gets(name2);
        puts(name2);
        putchar("\n");
#else //better code
        fgets(name1, sizeof(name1)-1, stdin);
        fgets(name2, sizeof(name2)-1, stdin);
        printf("Case %d: '%s' '%s'\n",i+1, name1, name2);
#endif
    }
}
ZioByte
  • 1,822
  • 21
  • 44