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

1

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.)

5 Comments

@fts It is working. Perhaps you should use it appropriately. Also, "not working" is not descriptive at all. How exactly isn't it working?
@H2CO3 fgets() would put the \n in s[i].name.
@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.
@H2CO3 Agree about fgets() superiority. The answer, as posted, would be better including your recommended post-fgets() processing.
@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!"
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);

7 Comments

That's for scanning a string with no newlines in it.
@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'.
@H2CO3 I don't know any student whose name contains newlines :p
@fts it does return "My Name" ;)
@TimvanElsloo But surely you know students whose name contains spaces? "%s" stops at the first whitespace it finds after skipping initial whitespace.
|
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

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.