Struct

College Student
Bibi is a lecturer who manages a student database at a university. Before Bibi existed, the student database was not well organized, and was not collected per student, so student's data could be chopped up and scattered irregularly between one another.
Because Bibi is a lecturer who has studied ”struct”, Bibi wants to store all the data given in the struct. Making it easier to find and reuse.

Data from students can contain:
• Name (Nama)
• NIM
• Age (Umur)
• Postal Code (Kode Pos)
• Place of Birth (POB / Tempat Lahir)
• Date of Birth (DOB / Tanggal Lahir)
• High School (Almamater SMA)
• Number of Siblings (Jumlah Saudara Kandung)
• Height (Tinggi Badan)
• Bank Account Number (NOMOR REKENING)

Format Input
The input in the first row contains an integer N, how much student data you have to save. For the next N x 10 rows of student data you must save sequentially from the Name until Account Number.
Format Output
The output consists of (N x 10) + N lines. Student data is preceded by ”Mahasiswa ke-X:”, where X is the order of students.
Sample Input (standard input)
2
Lili
123456789
13
786431
Jakarta
14 SEPT2010
Bina Nusantara School
7
123
968543769
Jojo
125676789
22
711111
Bekasi
1 JANUARY2018
Bina Nusantara School
99
12
890234789

Sample Output (standard output)
Mahasiswa ke-1:
Nama : Lili
NIM : 123456789
Umur : 13
Kode Pos : 786431
Tempat Lahir : Jakarta
Tanggal Lahir : 14 SEPT2010
Almamater SMA : Bina Nusantara School
Jumlah Saudara Kandung : 7
Tinggi Badan : 123
NOMOR REKENING : 968543769
Mahasiswa ke-2:
Nama : Jojo
NIM : 125676789
Umur : 22
Kode Pos : 711111
Tempat Lahir : Bekasi
Tanggal Lahir : 1 JANUARY2018
Almamater SMA : Bina Nusantara School
Jumlah Saudara Kandung : 99
Tinggi Badan : 12
NOMOR REKENING : 890234789
my source code:
#include <stdio.h>

struct database {
char nama[105];
char nim[10];
int age;
char postalcode[10];
char pob[105];
char dob[105];
char hs[105];
int siblings;
int height;
int bank[10];
} mahasiswa;

int main()
{
int tc;
int age, siblings, height;
scanf("%d", &tc);
for(int t=0;t<tc;t++){

scanf("%[^\n]", mahasiswa.nama);
scanf("%s", mahasiswa.nim);
scanf("%d", &age);
scanf("%s", mahasiswa.postalcode);
scanf("%[^\n]", mahasiswa.pob);
scanf("[^\n]", mahasiswa.dob);
scanf("%[^\n]", mahasiswa.hs);
scanf("%d", &siblings);
scanf("%d", &height);
scanf("%s", mahasiswa.bank);


printf("Mahasiswa ke-%d:\n", t+1);
printf("Nama: %s\n", mahasiswa.nama);
printf("NIM: %s\n", mahasiswa.nim);
printf("Umur: %d\n", age);
printf("Kode Pos: %s\n", mahasiswa.postalcode);
printf("Tempat Lahir: %s\n", mahasiswa.pob);
printf("Tanggal Lahir: %s\n", mahasiswa.dob);
printf("Almamater SMA: %s\n", mahasiswa.hs);
printf("Jumlah Saudara Kandung: %d\n", siblings);
printf("Tinggi Badan: %d\n", height);
printf("NOMOR REKENING: %s\n", mahasiswa.bank);
}

}
my output look like this:

Mahasiswa ke-1:
Nama:
NIM: Lili
Umur: 123456789
Kode Pos: 13
Tempat Lahir:
Tanggal Lahir:
Almamater SMA:
Jumlah Saudara Kandung: 786431
Tinggi Badan: 0
NOMOR REKENING: Jakarta
14 SEPT2010
Bina Nusantara School
Mahasiswa ke-2:
Nama:
NIM: 14
Umur: 123456789
Kode Pos: SEPT2010
Tempat Lahir:
Tanggal Lahir:
Almamater SMA:
Jumlah Saudara Kandung: 786431
Tinggi Badan: 0
NOMOR REKENING: Bina

can u tell me the problems in my code?
1. Use code tags when you post code.

2. Use -Wall (or equivalent) when you compile your code.
Do NOT attempt to run code containing warnings, only to then ask "what's wrong"
1
2
3
4
5
6
7
8
9
10
11
$ gcc -Wall foo.c
foo.c: In function ‘main’:
foo.c:28:7: warning: too many arguments for format [-Wformat-extra-args]
 scanf("[^\n]", mahasiswa.dob);
       ^
foo.c:32:7: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int *’ [-Wformat=]
 scanf("%s", mahasiswa.bank);
       ^
foo.c:45:8: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int *’ [-Wformat=]
 printf("NOMOR REKENING: %s\n", mahasiswa.bank);
        ^

Fix your printf/scanf calls.
Topic archived. No new replies allowed.