1

I created a structure Student for storing name, age and gender of a student. While accepting inputs for gender my program doesn't accepts the input (gender-'M' or 'F') which is of char datatype.

#include<stdio.h>

struct Student
{
    char name [25];
    int age;
    char gender;
};

struct Student stud[5];
int i;

void ask()
{
    for (i = 0; i<=5; i++)
    {
        printf("\nEnter details of student %d:",i+1);
        printf("\nEnter name: ");
        scanf("%s", stud[i].name);
        printf("\nEnter age: ");
        scanf("%d",&stud[i].age);
        printf("\nEnter gender: ");
        scanf("%c",&stud[i].gender);
    }
    printf("\nDisplaying student record:\n");
    for(i = 0; i<=5; i++)
    {
        printf("\nStudent name is: ",stud[i].name);
        printf("\nAge is: ",stud[i].age);
        printf("\nGender: ",stud[i].gender);
    }
}

void main()
{
    ask();
}

I expected the output as:

Enter the details of student1:
Enter Name: xyz
Enter age: 20
Enter gender: F
Enter the details of student2:

but the actual output is:

Enter the details of student1:
Enter Name: xyz
Enter age: 20
Enter gender:
Enter the details of students2:
Enter name:
Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
Asfiya Shaikh
  • 107
  • 2
  • 7

0 Answers0