关于C#:Printf,scanf和For Loop问题

Printf, scanf & For Loop problems

我的目标是能够将成员输入到学生数据库的数组的每个结构中。 我知道我的for循环在某种程度上是错误的,但是我不知道如何解决它...输出使用名字,然后在不使用输入的情况下打印循环的其余部分。

显然,问题出在我缺乏对printf和scanf功能的理解。 但是仅仅从我无知的角度来看它,我不明白为什么它不起作用。

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#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("\
First Name:"
);
        scanf("%s",stdt[i].name);
        printf("\
Surname:"
);
        scanf("%s",stdt[i].surname);
        printf("\
UUN:"
);
        scanf("%s",stdt[i].UUN);
        printf("\
Department:"
);
        scanf("%s",stdt[i].department);
        printf("\
Gender (m/f):"
);
        scanf(" %c",&stdt[i].gender);
        printf("\
Age:"
);
        scanf("%d",&stdt[i].age);
    }

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

    return 0;
}


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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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("\
Enter 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("\
Enter 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("\
Enter 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("\
Enter 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("\
Enter 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("\
Enter 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("\
The student of oldest age:%s, %s, %s, %s, %c, %d\
"
,
        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


您正在尝试将数据写入未分配的空间。 更改您的结构以使用char []:

1
2
3
4
5
6
7
8
typedef struct {
    char name[200];
    char surname[200];
    char UUN[200];
    char department[200];
    char gender;
    int age;
} student_t;

(这里仅提供200个示例;根据您的需要进行调整)

如果您确实想将它们保留为char*,则可以:

i)用malloc预先分配每个:

1
2
3
4
5
6
    ...
    printf("\
First Name:"
);
    stdt[i].name = malloc(200);
    scanf("%s",stdt[i].name);
    ...

要么

ii)scanf到缓冲区,然后将结果复制到最终成员:

1
2
3
4
5
6
7
8
9
10
int main() {
    ...
    char buffer[200];

    for(i=3;i<6;i++){ /* This is where I'm stuck */
        printf("\
First Name:"
);
        scanf("%s",buffer);
        stdt[i].name = strdup(buffer);
    ...