2


I am currently trying to solve a problem from CodeChef but I am having troubles with using fgets() inside a loop.

The first input (T) is going to be a positive integer containing the number of user inputs.
Then delimited by newline characters, the user is going to input a string below the length of 10 under any circumstances.

So, I've tried this:

#include <stdio.h>
#include <stdlib.h>

#define SIZE 10

int main()
{
    int T;
    int diffX, diffY;
    char s[SIZE];

    scanf("%d", &T);

    while (T--){
        fgets(s, SIZE, stdin);
        printf("%s\n", s);
    }
    return 0;
}

However, when I attempted to test the code with the following inputs:

3
Hello
Hi
What

I was only able to input until "Hi" then the program exited successfully (returning 0).

Why is this the case and how can I fix it?

Thank you in advance,
kpark.

3
  • 2
    scanf doesn't consume the \n from the user entered number. You need to first consume this Commented Aug 19, 2013 at 20:57
  • Suggest avoid using both scanf() with fgets(). BTW: Nice tight example of your problem. Commented Aug 19, 2013 at 21:00
  • 1
    It takes three strings. The first one consists of just the newline after the 3 (because the scanf() leaves the newline behind). Then you have Hello and Hi as numbers 2 and 3. Commented Aug 19, 2013 at 21:00

3 Answers 3

5

fgets() consumes the newline left behind by the first call to scanf(). So, it is consuming 3 lines, but the first line looks like an empty line to the fgets() loop you have.

You can fix this by using fgets() to get the first line too, and parse the string into a number using sscanf().

fgets(s, SIZE, stdin);
sscanf(s, "%d", &T);
/* ... */
Sign up to request clarification or add additional context in comments.

1 Comment

This has worked well for me and learned a new function. Thank you :)
0

It is counting the read of the T as part of the counting. Add a newline in the scanf.

#include <stdio.h>
#include <stdlib.h>

#define SIZE 10



    int main()
    {
        int T;
        int diffX, diffY;
        char s[SIZE];

        scanf("%d\n", &T);

        while (T--){
            fgets(s, SIZE, stdin);
            printf("%s\n", s);
        }
        return 0;
    }

4 Comments

Adding the newline to the scanf() format will work OK here, but would be horrid if the user was typing because the scanf() would not return until some other non-white-space input was entered after the number. Better to use fgets() for all inputs, and sscanf() to process the number.
This has worked for me and the advice from Jonathan was informative. Thank you!
I agree with Jonathan. Just trying to change the code minimally.
This does not perform consistently is the first time fgets() is called has leading spaces as compare to subsequent calls. If you want to scan in a single '\n, use "%*[\n]". Recall that '\n' in a scanf()` format matches any number of any white space.
0

Is your Question is about how to read Multiple Strings in C ? Then it can be done by 2 ways :- 1.By declaring two dimensional Array of characters.

//Let say we want 6 strings each of them having max 10 characters.
char set[6][10] ;
for(int i=0;i<6;i++)
scanf("%s",set[i])

2.By declaring one dimensional Array of pointers to character (Notice the naming Conventions), in which each of those pointer pointing to a String.

int main(){

int i,numOfStrings;
char temp[30];

printf("Enter Number of strings in set ");
scanf("%d",&numOfStrings);

//Here We have defined array of pointer that will store each string sepratly.
//Arry of pointer to character.
char *setOfStrings[numOfStrings];

for(i=0;i<numOfStrings;i++)
{
    printf("Enter string ");
    scanf("%s",temp);
    setOfStrings[i]= (char*)malloc(sizeof(temp)); //allocted new memory and gave it to array of pointer
    strcpy(setOfStrings[i],temp);
}

for(i=0;i<numOfStrings;i++)
{
    printf("string = %s \n",setOfStrings[i]);
}
return 0;
}

But that need to understand : In case of array of pointers we may initialize them with String but Can't take as input from Command line like

char *set[2]={"Dinesh","Kandpal"}; //Its valid but you can't do this from command line 

for doing so What we do we will create an space dynamically ,store that address in the one of the element in 1-D array of pointers and then whatever value we have scanned copy that content to the another string to the location that we created using malloc

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.