0
#include <stdio.h>


int b;
int main()
{
int a, c;
printf("Enter the number of students \n");
scanf("%d", &a);

struct studinfo{
    char name[50];
    int roll;
    float marks;
};

struct studinfo s[10];

for(int i = 0; i <= a; i++)
{
    printf("Enter the Name :\n");
    scanf("%[^\n]s", s[i].name);      //THIS ONE HERE
    printf("\n");
    printf("Enter the roll no. \n");
    scanf("%d", &s[i].roll);
    printf("\n");
    printf("Enter the marks\n");
    scanf("%f", &s[i].marks);
}

for(int i = 0; i<= a; i++)
{
        printf("Name: %s\n", s[i].name);
        printf("Roll no: %d\n", s[i].roll);
        printf("Marks: %f", s[i].marks);
}
return 0;
}






Works when I remove the scanf to enter number of students. 

#include <stdio.h>

int b; int main() { int a, c; printf("Enter the number of students \n");

struct studinfo{
    char name[50];
    int roll;
    float marks;
};

struct studinfo s[10];

for(int i = 0; i <= a; i++)
{
    printf("Enter the Name :\n");
    scanf("%[^\n]s", s[i].name);
    printf("\n");
    printf("Enter the roll no. \n");
    scanf("%d", &s[i].roll);
    printf("\n");
    printf("Enter the marks\n");
    scanf("%f", &s[i].marks);
}

for(int i = 0; i<= a; i++)
{
        printf("Name: %s\n", s[i].name);
        printf("Roll no: %d\n", s[i].roll);
        printf("Marks: %f", s[i].marks);
}
return 0;

}



-> This happenes because the Scanf initially used takes everything except for '\n' which is later is read by the next scanf which skips when it reads a '\n'.
-> Ive tried Fgets and gets too but still doesnt work.
- 

The output im getting is;

after I enter the number of students, it just skips the scanf to enter the name and just executes scanf to enter the roll no.
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
hmmm Mm
  • 11
  • 4
  • Please provide a [mcve], consistently formatted, along with instructions how exactly you run this and both expected and actual output you receive. As a new user here, also take the [tour] and read [ask]. – Ulrich Eckhardt Mar 05 '23 at 09:39
  • You can use `scanf(" %[^\n]", s[i].name);` to skip leading whitespace, like the leftover newline from the previous operation. Note the space before the `%`. – Retired Ninja Mar 05 '23 at 09:46
  • Does this answer your question? [scanf() leaves the newline character in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-newline-character-in-the-buffer) – Retired Ninja Mar 05 '23 at 09:46
  • OT: `for(int i = 0; i <= a; i++)`... If `a` is 5, the loop will execute **6** times... Think about it. – Fe2O3 Mar 05 '23 at 09:49

3 Answers3

1

scanf(" %[^\n]", s[i].name);

A white-space character (space, tab, etc...) (usually a space is used) in scanf reads consecutive white-space characters until a non-whitespace character appears. So this will solve the problem.

There is no s after %[].

doraemon1
  • 113
  • 1
  • 11
  • Consider `" %49[^\n]"` in order to avoid buffer overflows – David Ranieri Mar 05 '23 at 09:48
  • @DavidRanieri it allowes me to exceute the scanf to print the name but it skips all the subsequent scanfs. – hmmm Mm Mar 05 '23 at 09:53
  • @hmmmMm I don't think so, see [Vlad's answer](https://stackoverflow.com/a/75641379/1606345): _the input buffer contains the new line character '\n' that corresponds to the pressed key Enter._, the leading space before `%` prevents to skip all the subsequent calls. – David Ranieri Mar 05 '23 at 10:13
1

For starters you should check that the value of a is not greater than 10 and the condition in for loops should look like

struct studinfo s[10];

for(int i = 0; i < a; i++)

As for your question then after this call of scanf

scanf("%d", &a);

the input buffer contains the new line character '\n' that corresponds to the pressed key Enter. So this call of scanf

scanf("%[^\n]s", s[i].name); 

reads an empty string. And there is redundant s character in the format string.

Instead you should write

scanf( " %49[^\n]", s[i].name );
       ^^^^^^^^^^ 

Pay attention to the leading space in the format string. It allows to skip white space characters.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0
  • "eat" the first enter \n :
scanf("\n%[^\n]", s[i].name);

Good luck

Tips

  • limit input length: %49[^\n] to limit the number of characters to read.
  • correct the for loop end condition into: i<a
S_IROTH
  • 210
  • 1
  • 5