-5

好的,首先我将解释我的任务。对于这个任务,我必须使用我没有问题的动态内存分配。我遇到的问题是找出完成任务的正确方法。对于我的作业,我需要创建一个程序,提示用户输入他们有多少学生,然后询问以下信息;学生证、生日和电话号码。我需要使用循环来提示用户输入所有学生信息。我需要创建一个循环来扫描所有学生 ID,并使用他们的生日找到年龄最大的学生(循环必须能够扫描超过 3 个学生)。

这是我的代码,我从你们那里得到了一些建议,甚至是一些代码,但是当它进入 for 循环时,它不允许我输入学生信息,它只是结束了程序。帮助

谢谢你。

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

struct studentDataType
{
    int studentID;
    int year;
    int month;
    int day;
    long long phone;
};

int main (void)
{
    int * studentData= NULL;
    int * studentDataType;
    int students;
    int studentID;
    int year;
    int month;
    int day;
    long long phone;

    printf("How many students are you entering records for:\n");
    scanf("%d", &students);

    studentData= malloc((sizeof(int)*students));

    struct studentDataType *studentRecords = malloc(sizeof(struct studentDataType) * students);

    for (int i = 0 ; i != students ; ++i)  {
        printf("Enter information for student %d\n", i+1);
        struct studentDataType * s = &studentData[i];
        scanf("%d%d%d%d%d", &(s->studentID), &(s->year), &(s->month), &(s->day), &(s->phone));
    }
}
4

3 回答 3

0

您当前的代码中有很多问题。struct member phone(即 long long 不能正确保存电话号码,例如 555-5555),以及为学生人数分配内存的方式只是其中两个问题。我做了一些更改,以说明如何循环一些学生,并将这些信息收集到结构中。

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

typedef struct 
{
    int studentID;
    int year;
    int month;
    int day;
    char phone[20];//this size should accommodate local, long dist, and intn'l 
}studentDataType;
studentDataType s, *studentRecords;

int main (void)
{
   // int * studentData= NULL;
    //int * studentDataType;
    int students;
    //int studentID;
    //int year;
    //int month;
    //int day;
    //long long phone;



    printf("How many students are you entering records for:\n");
    scanf("%d", &students);

    studentRecords = malloc(sizeof(s)*students);



    //studentData= malloc((sizeof(int)*students));

    //struct studentDataType *studentRecords = malloc(sizeof(struct studentDataType) * students);

    for (int i = 0 ; i != students ; i++)  {
        printf("Enter information for student %d\n", i);
        //struct studentDataType * s = &studentData[i];
        scanf("%d%d%d%d%s", &studentRecords[i].studentID, 
                            &studentRecords[i].year, 
                            &studentRecords[i].month, 
                            &studentRecords[i].day, 
                            studentRecords[i].phone);

   }
    getchar();
}
于 2013-11-01T16:44:19.603 回答
0
struct studentDataType
{
    int studentID;
    int year;
    int month;
    int day;
    long long phone;
};

int _tmain(int argc, _TCHAR* argv[])
{
    int students;

      printf("How many students are you entering records for:\n");
    scanf("%d", &students);

    struct studentDataType *studentRecords = (struct studentDataType *)malloc(sizeof(struct studentDataType) * students);
    struct studentDataType *student = studentRecords;

    for (int i = 0; i < students; i++)
    {
        printf("Enter information for student #%d\n", i+1);

        scanf("%d#%d#%d#%d#%d", &(student->studentID),
                                &(student->year),
                                &(student->month),
                                &(student->day),
                                &(student->phone));
        student++; // move pointer to next student
    }

    // print info
    student = studentRecords;

    for (int i = 0; i < students; i++)
    {

        printf("%d#%d#%d#%d#%d\n", student->studentID,
                                   student->year,
                                   student->month,
                                   student->day,
                                   student->phone);
        student++; // move pointer to next student
    }

    getchar();
    return 0;
}
于 2013-11-01T16:47:35.250 回答
0

编辑:更改了记录迭代器并在 malloc() 结果上添加了一些错误检查。

您的代码中有几个问题,所以我只是发布一些我认为应该有效的内容,如果您愿意,可以提出具体问题。尝试以下操作:

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

struct studentDataType
{
    int studentID;
    int year;
    int month;
    int day;
    long long phone;
};

int main (void)
{
    struct studentDataType *studentRecords=NULL;
    unsigned int students;
    unsigned int studentID;
    unsigned int year;
    unsigned int month;
    unsigned int day;
    unsigned long phone;

    printf("How many students are you entering records for:\n");
    scanf("%d", &students);

    studentRecords = malloc(sizeof(struct studentDataType) * students);

    // Check whether malloc succeeded.
    if(studentRecords != NULL)
    {       
        struct studentDataType *current_record = &studentRecords[0];
        for (int i = 0 ; i < students ; ++i, current_record++)  {
            printf("Enter information for student %d\n", i+1);              
            scanf("%u %u %u %u %u", &(current_record->studentID), &(current_record->year), &(current_records->month), &(current_record->day), &(current_records->phone));
        }
        free(studentRecords);
    }
}
于 2013-11-01T16:29:09.057 回答