gpt4 book ai didi

c - Printf、scanf 和 For 循环问题

转载 作者:太空宇宙 更新时间:2023-11-04 03:42:48 24 4
gpt4 key购买 nike

我的目标是能够将成员输入到学生数据库数组的每个结构中。我知道我的 for 循环不知何故是错误的,但我不知道如何修复它...输出采用名字,然后在不接受输入的情况下打印循环的其余部分。

很明显问题出在我对printf和scanf的能力理解不够。但是从我无知的角度来看,我不明白为什么它行不通。

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

typedef struct {
char *name;
char *surname;
char *UUN;
char *department;
char gender;
int age;
} student_t;

student_t findOldest(student_t *studentarr, int len) {
int i;
student_t max=*(studentarr+0);

for (i=1; i < len; i++) {
if((*(studentarr+i)).age > max.age)
max = *(studentarr+i);
}

return max;
}

int main() {
int i;
student_t result;
student_t stdt[6]={{"John","Bishop","s1234","Inf",'m',18},{"Lady","Cook","s2345","Eng",'f',21},{"James","Jackson","s33456","Eng",'m',17}};
student_t *p=&stdt[0];

for(i=3;i<6;i++){ /* This is where I'm stuck */
printf("\nFirst Name: ");
scanf("%s",stdt[i].name);
printf("\nSurname: ");
scanf("%s",stdt[i].surname);
printf("\nUUN: ");
scanf("%s",stdt[i].UUN);
printf("\nDepartment: ");
scanf("%s",stdt[i].department);
printf("\nGender (m/f): ");
scanf(" %c",&stdt[i].gender);
printf("\nAge: ");
scanf("%d",&stdt[i].age);
}

findOldest(p,6);
result = findOldest(p,6);
printf("\nThe student of oldest age:%s, %s, %s, %s, %c, %d\n",result.name,result.surname,result.UUN,result.department,result.gender,result.age);

return 0;
}

最佳答案

this compiles with no errors/warnings
it implements the desired algorithm
it greatly reduces the complexity of certain parts of the code
it give names to the magic numbers in the code
it eliminates the probability of seg fault events
from trying to write to pointers that points nowhere

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

#define MAX_NAME_LEN (20)
#define MAX_SURNAME_LEN (20)
#define MAX_UUN_LEN (20)
#define MAX_DEPARTMENT_LEN (20)

struct student_t
{
char name[MAX_NAME_LEN];
char surname[MAX_SURNAME_LEN];
char UUN[MAX_UUN_LEN];
char department[MAX_DEPARTMENT_LEN];
char gender;
int age;
};

int findOldest(struct student_t *, int);

#define MAX_STUDENTS (6)

int main()
{
int i; // loop counter
int result; // returned index from findOldest()
struct student_t stdt[MAX_STUDENTS]=
{
{"John","Bishop","s1234","Inf",'m',18},
{"Lady","Cook","s2345","Eng",'f',21},
{"James","Jackson","s33456","Eng",'m',17}
};


// enter info for last 3 students
for( i=3; i<6; i++ )
{
printf("\nEnter First Name: ");
if( 1 != scanf(" %s", stdt[i].name) )
{ // then, scanf failed
perror( "scanf failed for name" );
exit( EXIT_FAILURE );
}

// implied else, scanf for name successful

printf("\nEnter Surname: ");
if( 1 != scanf(" %s", stdt[i].surname) )
{ // then scanf failed
perror( "scanf failed for surname" );
exit( EXIT_FAILURE );
}

// implied else, scanf for surname successful

printf("\nEnter UUN: ");
if( 1 != scanf(" %s", stdt[i].UUN) )
{ // then scanf failed
perror( "scanf failed for UUN" );
exit( EXIT_FAILURE );
}

// implied else, scanf for UUN successful

printf("\nEnter Department: ");
if( 1 != scanf(" %s",stdt[i].department) )
{ // then scanf failed
perror( "scanf failed for department" );
exit( EXIT_FAILURE );
}

// implied else, scanf for department successful

printf("\nEnter Gender (m/f): ");
if( 1 != scanf(" %c", &stdt[i].gender) )
{ // then scanf failed
perror( "scanf failed for gender" );
exit( EXIT_FAILURE );
}

// implied else, scanf for gender successful

printf("\nEnter Age: ");
if( 1 != scanf(" %d", &stdt[i].age) )
{ // then scanf failed
perror( "scanf failed for age" );
exit( EXIT_FAILURE );
}

// implied else, scanf for age successful

} // end for


result = findOldest( stdt, MAX_STUDENTS );
printf("\nThe student of oldest age:%s, %s, %s, %s, %c, %d\n",
stdt[result].name,
stdt[result].surname,
stdt[result].UUN,
stdt[result].department,
stdt[result].gender,
stdt[result].age);

return 0;
} // end function: main

// note: if more than one student has same (oldest) age
// then the student index of the first student
// of that age will be returned
int findOldest(struct student_t *studentarr, int len)
{
int i; // loop index
int max = -1;
int index = -1;

for (i=0; i < len; i++)
{
if( studentarr[i].age > max )
{
max = studentarr[i].age;
index = i;
} // end if
} // end for

return index;
} // end function findOldest

关于c - Printf、scanf 和 For 循环问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27237750/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com