0

I have write simple code using structure method and in that code I am taking string(name) as input using for loop for 3 students. For 1st iteration each line in for loop is working as it should be.... but problem comes on second iteration... in second iteration scanf is skipping input for string (name)...

My code is as below :

#include<stdio.h>

struct student_data
{
    char name[5];
    int dsd;
    int dic;
    int total;  
};

void main()
{
    struct student_data s[3];


    int i;

    for(i = 0;i<3;i++)
    {
        printf("Enter Name of Student\n");
        scanf("%[^\n]",s[i].name);         // Problem occures here in     
                                       //  second iteration  

        printf("\nEnter marks of DSD\n");
        scanf("%d",&s[i].dsd);

        printf("Enter marks of DIC\n");
        scanf("%d",&s[i].dic);


        s[i].total = s[i].dsd+s[i].dic;     

    }

    printf("%-10s %7s %7s  %7s\n ","Name","DSD","DIC","Total");
    for(i=0;i<3;i++)
    {

       printf("%-10s %5d  %6d      %6d\n",s[i].name,s[i].dsd,s[i].dic,s[i].total);
}  

}

rushank
  • 9
  • 4
  • 1
    You are using `scanf()` wrong anyway. You MUST check for the return value if you don't wan't to read uninitialized memory. And you don't want to, because it's undefined behavior. The problem with `scanf()` is that it's really hard to make it useful in a real life program, that's why it's rarely used, nevertheless it's the first thing every one learns about [tag:c]. Please read `scanf()`'s documentation, you might find out yourself why it's not working, and why it hardly does. – Iharob Al Asimi Jul 25 '16 at 11:26
  • wrong `scanf()` . use `scanf(" %4[^\n]",s[i].name);` instead. – Abhishek T. Jul 25 '16 at 11:30
  • @AbhishekTandon if we are using `scanf(" %4[^\n]",s[i].name);` . Can we give name more than 4 characters? – GShaik Jul 25 '16 at 11:39
  • By the way, your code will fail because you iterate 3 times (when i=0,1,2) over an array of 2 elements (elem=0,1). Don't you get a Seg Fault on the 3rd iteration? Or maybe it's just a type when you wrote 'struct student_data s[2];' and simply meant 'struct student_data s[3];' – Craig Douglass Jul 25 '16 at 12:21

1 Answers1

0

The main problem with your code was that you defined the student_data s[2] which is a array of size two, but in the loop, you are looping for (i=0; i<3; i++) which is valid for an array of size 3. This small modifications will work fine:

 int main()
{
struct student_data s[2];  // array of size 2


int i;

for(i=0; i<2; i++)      // i will have values 0 and 1 which is 2 (same as size of your array)
{
    printf("Enter Name of Student\n");
    scanf("%s", s[i].name);

    printf("\nEnter marks of DSD\n");
    scanf("%d", &s[i].dsd);

    printf("Enter marks of DIC\n");
    scanf("%d", &s[i].dic);

    s[i].total = s[i].dsd+s[i].dic;

}

    printf("%-10s %7s %7s  %7s\n ","Name","DSD","DIC","Total");

    for(i=0; i<2; i++)   // same mistake was repeated here
    {
        printf("%-10s %5d  %6d     %6d\n",s[i].name,s[i].dsd,s[i].dic,s[i].total);
    }
return 0;
}
ReshaD
  • 936
  • 2
  • 18
  • 30
  • thnx ur solution worked.... but i still want to know why %[^\n] was skipping input from user ?? – rushank Jul 26 '16 at 16:54
  • @rushank27 take a look at this post: http://stackoverflow.com/questions/6083045/scanf-n-skips-the-2nd-input-but-n-does-not-why – ReshaD Jul 29 '16 at 08:17