私は以下の問題を抱えています..: 私は自分のコードを実行しますが、著者の数は正しく読みますが、あなたは名の文字列/ char型の値が正しく表示されますが、日付の整数値だけでランダムな数字です見ることができるように...構造体を持つ動的配列の変数からランダムな値を返します
typedef struct{
int year;
int month;
int day;
}Date;
typedef struct{
char lastName[30];
char firstName[30];
Date birthday;
}Person;
int main(){
//assigning memory dynamically to array of authors
int n;
printf("How many authors should be added to the archive?\n");
scanf("%i", &n);
//array of authors
Person* authors = (Person*) calloc(n, sizeof(Person));
//reading authors
int i;
for(i = 0; i < n; i++){
addAuthor(authors, i);
}
//writing authors to screen
for(i = 0; i < n; i++){
printAuthor(authors[i]);
}
free(authors);
return 0;
}
Date inputDate(){
Date d;
printf("Input year: ");
scanf(" %s", &d.year);
printf("Input month: ");
scanf(" %s", &d.month);
printf("Input day: ");
scanf(" %s", &d.day);
return d;
}
Person inputAuthor(){
Person p;
printf("\nInput last name: ");
scanf(" %s", &p.lastName);
printf("Input last name: ");
scanf(" %s", &p.firstName);
p.birthday = inputDate();
return p;
}
void printAuthor(Person p){
printf("\n%s, %s born %i.%i.%i", p.lastName, p.firstName, p.birthday.day, p.birthday.month, p.birthday.year);
}
void addAuthor(Person* p, unsigned u){
p[u] = inputAuthor();
}
を
:私は(例えば)これを取得する画面
'%s'は整数ではなく文字列を読み取るためのものです。 –
2nd 'printf("入力姓名: ");' - > 'printf("入力名: ");' – BLUEPIXY