Struct, Array, Loop

I'll make C Program that input 5 data and output that data again, but why the output data same as the fifth data? please help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<stdio.h>
struct mhs
{
    char name[20];
    int nim;
};

struct data
{
    char address[20];
    char country[20];
};

main()
{

    struct mhs a;
    struct data b;
    int i;
    for(i=0; i<5; i++)
    {
    printf("\nName: ");
    scanf("%s", &a.name);
    printf("NIM: ");
    scanf("%d", &a.nim);
    printf("Address: ");
    scanf("%s", &b.address);
    printf("Country: ");
    scanf("%s", &b.country);
    }

    for(i=0; i<5; i++)
    {
    printf("\nName= %s", a.name);
    printf("\nNIM= %d", a.nim);
    printf("\nAdress= %s", b.address);
    printf("\nJCountry= %s", b.country);
    }
}
The problem is that a and b are objects, not arrays.

Change line 17 and 18 to:

1
2
struct mhs a[5];
struct data b[5];


Now you will also have to modify your for loops too.
Last edited on
Topic archived. No new replies allowed.