2

I am working on a simple C program using a struct named 'student'. Here is my code

#include<stdio.h>
#include<stdlib.h>
  struct  student {
    char name[50];
    int id;
    float marks_1;
    float marks_2;

};


void main(){

    int num,a,i;
    printf("Enter number of students\n");
    scanf("%d",&num);
    struct student s[num];
    for(i=0;i<num;i++)
    {
        a=i+1;
        printf("Enter name of student number %d\n",a);
        scanf("%[^\n]%*c",s[i].name);

    }

  }

When I run the program I am able to enter the number of students correctly, but after that I am not able to enter the name corresponding to each student. This is the output that I get.

Enter number of students
2
Enter name of student number 1
Enter name of student number 2

RUN FINISHED; exit value 2; real time: 1s; user: 0ms; system: 0ms

What might be the problem? Any help appreciated

fts
  • 141
  • 4
  • 14

5 Answers5

3
scanf("%d",&num);

leaves the newline you typed to send the input to the programme in the input buffer. Thus the first iteration of

for(i=0;i<num;i++)
{
    a=i+1;
    printf("Enter name of student number %d\n",a);
    scanf("%[^\n]%*c",s[i].name);

}

immediately finds that newline and scans in an empty string.

Consume the newline before scanning, either by changing the first format to "%d%*c" or by adding a space to the start of the name-scanning format to skip initial white space.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
2

change scanf("%[^\n]%*c",s[i].name); to scanf(" %[^\n]%*c",s[i].name);. Notice the space given before specifier to consume last input char left in stdin.

Dayal rai
  • 6,548
  • 22
  • 29
2

Scanf function not accepting input

If scanf() doesn't work, then use fgets():

fgets(s[i].name, sizeof(s[i].name), stdin);

And stay far away from scanf() if/while you don't have a full, proper understanding of how it works, because it's not intuitive to use, so to say. (It's also unsafe if you are not careful enough, and in this case, you weren't. The code is prone to buffer overflows.)

  • @fts It **is** working. Perhaps you should use it *appropriately.* Also, "not working" is not descriptive at all. How **exactly** isn't it working? –  Aug 22 '13 at 13:36
  • @H2CO3 `fgets()` would put the `\n` in `s[i].name`. – chux - Reinstate Monica Aug 22 '13 at 16:45
  • @chux Yes, it would. And then you write `*strchr(s[i].name, '\n') = 0;`, and **kaboom** it's gone. `fgets()` is still superior to `scanf()` when it comes to getting user input. –  Aug 22 '13 at 16:46
  • @H2CO3 Agree about `fgets()` superiority. The answer, as posted, would be better including your recommended post-`fgets()` processing. – chux - Reinstate Monica Aug 22 '13 at 16:50
  • @chux But I feel, it wouldn't, unfortunately. New users' attitude after they got a quick'n'dirty-but-works-solution: "thanks, it works perfectly, I never come back to this question anymore!" –  Aug 22 '13 at 16:51
0

I'm not sure what you're trying to do here:

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

but try replacing it with:

scanf("%50[^\n]s",s[i].name);
cutsoy
  • 10,127
  • 4
  • 40
  • 57
  • That's for scanning a string with no newlines in it. –  Aug 22 '13 at 13:02
  • 1
    @tim I want to include white spaces too in my 'name'. Eg. if I enter 'My Name' and then print the result I get 'My' and not 'My Name'. – fts Aug 22 '13 at 13:02
  • @H2CO3 I don't know any student whose name contains newlines :p – cutsoy Aug 22 '13 at 13:03
  • @fts it does return "My Name" ;) – cutsoy Aug 22 '13 at 13:04
  • 2
    @TimvanElsloo But surely you know students whose name contains spaces? `"%s"` stops at the first whitespace it finds after skipping initial whitespace. – Daniel Fischer Aug 22 '13 at 13:04
  • @TimvanElsloo Non sequitur. `%s` scans only until the first whitespace. Some people think that it's a good idea to tell `scanf()` to scan up to the first newline. (It isn't, but still.) –  Aug 22 '13 at 13:05
  • The `s` in `"%50[^\n]s"` serves no purpose. The format specifier ends with the `]`. – chux - Reinstate Monica Aug 22 '13 at 16:55
0

to read a line in c that include white spaces use fgets (name, 100, stdin);

here is the full code:

#include<stdio.h>
#include<stdlib.h>
  struct  student {
    char name[50];
    int id;
    float marks_1;
    float marks_2;

};


void main(){

    int num,a,i;
    printf("Enter number of students\n");
    scanf("%d",&num);
    struct student s[num];
    for(i=0;i<num;i++)
    {
        a=i+1;
        printf("Enter name of student number %d\n",a);
        fgets (s[i].name, 50, stdin);

    }

  }

scanf will read until the first whitespace, but not fgets, however, if you'll press enter when using fgets it'll will be stored in the string as well. so you need to remove it afterwards

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70