为什么这个程序不允许我在需要时输入信息?

时间:2013-11-01 16:13:41

标签: c arrays loops structure

首先我会解释我的任务。对于这个分配,我必须使用动态内存分配,我没有遇到任何问题。我遇到的问题是找出正确的工作方式。对于我的任务,我需要创建一个程序,提示用户输入他们有多少学生,然后要求提供以下信息;学生证,出生日期和电话号码。我需要使用循环来提示用户输入所有学生信息。我需要创建一个循环,扫描所有学生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));
    }
}

3 个答案:

答案 0 :(得分: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);
    }
}

答案 1 :(得分:0)

您当前的代码中存在许多问题。结构成员电话(即长电话号码不能正确保存电话号码,例如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();
}

答案 2 :(得分: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;
}