user14256058
user14256058

Reputation:

Why I am getting an extra space on output?

I'm having a problem. I am learning structure in C language. I wrote a code in C language. Everything is fine but when I go to take the second input a second time after the first line of input comes an extra space. How can I remove it?

How many student's info you want to add: 3

Enter student 1 info:
Full name: Dabananda Mitra
ID: 28
Section: B
Department: CSE
Session: 2019-2020
University: ISTT

Displaying student 1 info:
Name: Dabananda Mitra
ID: 28
Section: B
Department: CSE
Session: 2019-2020
University: ISTT


Enter student 2 info:
                     //Empty Line
Full name: SM Zakaria
ID: 29
Section: C
Department: CS
Session: 2019-2020
University: ISTT

Displaying student 2 info:
Name: SM Zakaria
ID: 29
Section: C
Department: CS
Session: 2019-2020
University: ISTT

I used getchar() before fgets() in my program because it didn't take any input if I didn't use it. Can anyone explain this?

My Code in C:

#include <stdio.h>
#define Max_Size 100

struct info
{
    char name[Max_Size];
    int id;
    char section;
    char department[Max_Size];
    char session[Max_Size];
    char university[Max_Size];
};

void display(struct info student, int x)
{
    printf("\nDisplaying student %d info:\n", x + 1);
    printf("Name: %s", student.name);
    printf("ID: %d\n", student.id);
    printf("Section: %c\n", student.section);
    printf("Department: %s", student.department);
    printf("Session: %s", student.session);
    printf("University: %s\n", student.university);
}

int main()
{
    int n;
    printf("How many student's info you want to add: ");
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
    {
        struct info studentInfo;
        printf("\nEnter student %d info:\n", i + 1);
        getchar();
        printf("Full name: ");
        fgets(studentInfo.name, Max_Size, stdin);
        printf("ID: ");
        scanf("%d", &studentInfo.id);
        printf("Section: ");
        getchar();
        scanf("%c", &studentInfo.section);
        printf("Department: ");
        getchar();
        fgets(studentInfo.department, Max_Size, stdin);
        printf("Session: ");
        fgets(studentInfo.session, Max_Size, stdin);
        printf("University: ");
        fgets(studentInfo.university, Max_Size, stdin);
        display(studentInfo, i);
    }

    return 0;
}

Please help me solve this.

Thanks in advance.

Upvotes: 0

Views: 931

Answers (2)

Some programmer dude
Some programmer dude

Reputation: 409166

Lets begin by looking at the end of the loop:

printf("University: ");
fgets(studentInfo.university, Max_Size, stdin);
display(studentInfo, i);

Then when the loop iterates, it begins like this:

struct info studentInfo;
printf("\nEnter student %d info:\n", i + 1);
getchar();
printf("Full name: ");

Notice how that getchar call follows a call to fgets? It means you need to press a key before the next fgets can read its input.

That first getchar call needs to be outside the loop:

scanf("%d", &n);
getchar();
for (int i = 0; i < n; i++)
{
    struct info studentInfo;
    printf("\nEnter student %d info:\n", i + 1);
    printf("Full name: ");

Upvotes: 1

klutt
klutt

Reputation: 31296

fgets() saves the newline character. To remove it, do something like this:

char buffer[size];
fgets(buffer, size, stdin);
buffer[strcspn(buffer, "\n")] = 0;

Upvotes: 2

Related Questions