0

So I would like to build a simple program to input data using structures.

My original program looked like this:

#include <stdio.h>
#include <stdlib.h>

struct student {
    int num;
    char name[20];

};

int main()
{
    int size, i;
    scanf("%d", &size);
    struct student s[size];
    for(i=0; i < size; i++){
        scanf("%d", &s[i].num);
        scanf("%s", &s[i].name);

    }

    for(i=0; i < size; i++){
         printf("no.:%d\n", s[i].num);
         printf("name:%s\n", s[i].name);

    }

    return 0;
}

My test input would be:

2
1 Name1
2 Name2

It was working but only when data was entered correctly. But when I tried to use more strings in my structure it started to get messy. For example something like this won't work:

#include <stdio.h>
#include <stdlib.h>

struct student {
    int num;
    char name[20];
    char gender;
    char address[20];
};

int main()
{
    int size, i, j;
    scanf("%d", &size);
    struct student s[size];
    for(i=0; i < size; i++){
        scanf("%d", &s[i].num);
        scanf("%s", s[i].name);
        scanf("%s", s[i].gender);
        scanf("%s", s[i].address);
    }

    for(i=0; i < size; i++){
         printf("no.:%d\n", s[i].num);
         printf("name:%s\n", s[i].name);
         printf("gender:%s\n", s[i].gender);
         printf("address:%s\n", s[i].address);
    }

    return 0;

}

I understood that problem must lay in usage of scanf for string input so I tried to use getchar(). I thought something like this might work.

for(i=0; i < size; i++){
        int j=0;
        while(( s[i].name[j]=getchar()) != ' ');
            j++;
        s[i].name[j] = '\0';
}

It's not working though. At this point I got confused and I'm not really sure what is doing wrong. I mean I would like to input something like:

1001 Jeff M No.2_road_city

by using structure, but I'm getting confused how exactly it should be done.

Patryk
  • 1
  • 1
  • 1
    Enable warning is the first step BEFORE ask on stackoverflow. `scanf("%s", &s[i].name);` => `scanf("%s", s[i].name);`... and other trivial mistake. `scanf("%s", &s[i].gender);` => `scanf("%c", &s[i].gender);` etc – Stargateur Dec 03 '17 at 07:39
  • 1
    Check each `scanf()` call to ensure it returns the correct number (1 when you read one field on each call). If it doesn't return 1, you've got a problem. It might be better to combine the N calls into one — you can do that. It might also be better to read lines with [`fgets()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fgets.html) or POSIX [`getline()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html) and then process the line with `sscanf()`. – Jonathan Leffler Dec 03 '17 at 08:07

1 Answers1

-1

put fflush(stdin); before using a scanf("%s".... It clears the keyboard buffer. Two consecutive scanf statements without flushing the buffer causes the second to contain a newline/carriage return from the first input only

Another problem: you use scanf("%s" for a char variable (char gender). Change to scanf("%c",&s[i].gender);.

Dark.Rider
  • 381
  • 1
  • 4
  • 17