I have been trying to solve this problem last few days but I still can't figure out... as a newbie learner.
I understand that I suppose to use array, pointer or struct and I tried to apply to it but failed...
Apologise for some Korean language in the code.
What I am trying to do is store each of student information in order to sort and print at the end (see the photo below) result suppose to look like this
What I couldn't figure out is storing the data and printing all students data at the end (3 students) Data I try to store is (name, fullyear, month, day, leapyear, nationality, gender, major) with this order I tried using pointer, array but couldn't do it well.
Any advice would be appreciated and thanks in advance. Please understand English is not my first language and I am a newbie for programming so if my decription isn't enough, I will try to add more later on so feel free to comment. Thank you!
Here is my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
int i;
for (i = 0; i < 3; i++)
{
char name[20];
char major[50];
char ssn[20];
printf("%s", "이름 : ");
scanf("%[^\n]%*c", name);
//fgets(name, 20, stdin);
printf("%s", "학과 : ");
scanf("%[^\n]%*c", major);
//fgets(major, 50, stdin);
printf("%s", "주민등록번호 : ");
//fgets(ssn, 20, stdin);
scanf("%[^\n]%*c", ssn);
// last 2 digits of birth year
char year[3];
memcpy(year, &ssn[0], 2);
year[2] = '\0';
// 2 digits of birth month
char month[3];
memcpy(month, &ssn[2], 2);
month[2] = '\0';
// 2 digits of birth day
char day[3];
memcpy(day, &ssn[4], 2);
day[2] = '\0';
// distinguising birth years of 1900
char fullyear[5];
if ((ssn[6] == '1') || (ssn[6] == '2') || (ssn[6] == '5') || (ssn[6] == '6'))
{
fullyear[0] = '1';
fullyear[1] = '9';
fullyear[2] = year[0];
fullyear[3] = year[1];
fullyear[4] = '\0';
}
// distinguishing birth years of 2000
if ((ssn[6] == '3') || (ssn[6] == '4') || (ssn[6] == '7') || (ssn[6] == '8'))
{
fullyear[0] = '2';
fullyear[1] = '0';
fullyear[2] = year[0];
fullyear[3] = year[1];
fullyear[4] = '\0';
}
// distinguishing birth years of 1800
if ((ssn[6] == '9') || (ssn[6] == '0'))
{
fullyear[0] = '1';
fullyear[1] = '8';
fullyear[2] = year[0];
fullyear[3] = year[1];
fullyear[4] = '\0';
}
// print name
printf("%s, ", name);
// print birth year
printf("%s년 ", fullyear);
// print month
printf("%s월 ", month);
// print day
printf("%s일, ", day);
// leap year, converting from full year string --> int
int leapyear;
leapyear = atoi(fullyear);
if (((leapyear % 4 == 0) && (leapyear % 100 != 0)) || (leapyear % 400 == 0))
{
printf("윤년, ");
}
else
{
printf("윤년아님, ");
}
// foreigner or Korean
if ((ssn[6] == '5') || (ssn[6] == '6') || (ssn[6] == '7') || (ssn[6] == '8'))
printf("외국, ");
else printf("대한민국, ");
// gender
if ((ssn[6] == '1') || (ssn[6] == '3') || (ssn[6] == '5') || (ssn[6] == '7') || (ssn[6] == '9'))
printf("남자, ");
else printf("여자, ");
// print major
printf("%s\n\n", major);
}
return 0;
}
As you have said, you need to use structs. For example, if you'd like to store 3 people (name, surname, and age) you need to do something like this:
typedef struct {
char name[20];
char surname[20];
int age;
} People;
int main() {
People people[3];
int x;
for (x = 0; x < 3; x++) {
scanf("%[^\n]%*c", people[x].name);
scanf("%[^\n]%*c", people[x].surname);
scanf("%d%*c", &people[x].age);
}
return 0;
}
Also, take in mind that if the user puts more than 19 characters on the name/surname, will result in overflow. fgets()
plus some kind of contol will be better (or other functions), but I suppose it's just an excercice.
An example:
char txt[20];
char next;
scanf("%19[^\n]%c", txt, &next);
if (next == '\n') {
printf("[*] %s\n", txt);
}
else {
printf("[e] Error, >19 characters\n");
}
The easiest, most direct way to do what you want is with an (large enough) array of structures
#include <stdio.h>
struct Student {
char name[200]; // maximum name of each student is 199
int year, month, day;
char nationality[100]; // max 99
char gender[2];
char major[100]; // max 99
};
int byage(const void *aa, const void *bb) {
const struct Student *a = aa;
const struct Student *b = bb;
if (a->year != b->year) return b->year - a->year;
if (a->month != b->month) return b->month - a->month;
return b->day - a->day;
}
int main(void) {
struct Student data[500]; // maximum 500 students
int n = data_fill(data); // fill data with user input
qsort(data, n, sizeof *data, byage); // sort data
data_print(data, n); // print it
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.