C 为什么不是';这个程序不允许我在需要的时候输入信息吗?

C 为什么不是';这个程序不允许我在需要的时候输入信息吗?,c,arrays,loops,structure,C,Arrays,Loops,Structure,好的,首先我来解释我的作业。对于这个任务,我必须使用动态内存分配,我没有任何问题。我遇到的问题是,如何正确地完成我的任务。对于我的作业,我需要创建一个程序,提示用户输入他们有多少学生,然后询问以下信息;学生ID、生日和电话号码。我需要使用循环提示用户输入所有学生信息。我需要创建一个循环,它将扫描所有学生ID,并使用他们的生日查找最年长的学生(该循环必须能够扫描3个以上的学生) 这是我的代码,我从你们那里得到了一些建议,甚至是一些代码,但它不允许我在进入循环时输入学生信息,因为它只是结束了程序。帮

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

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

多谢各位

#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));
    }
}
#包括
#包括
struct studentDataType
{
国际学生;
国际年;
整月;
国际日;
长手机;
};
内部主(空)
{
int*studentData=NULL;
int*studentDataType;
国际学生;
国际学生;
国际年;
整月;
国际日;
长手机;
printf(“您要为多少学生输入记录:\n”);
scanf(“%d”和学生);
studentData=malloc((sizeof(int)*students));
struct studentDataType*studentRecords=malloc(sizeof(struct studentDataType)*students);
for(int i=0;i!=学生;++i){
printf(“输入学生%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));
}
}

编辑:更改了记录迭代器,并在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);
    }
}
#包括
#包括
struct studentDataType
{
国际学生;
国际年;
整月;
国际日;
长手机;
};
内部主(空)
{
struct studentDataType*studentRecords=NULL;
未签字的国际学生;
无符号整数studentID;
未签名整数年;
未签名整数月;
未签名整数日;
无符号长电话;
printf(“您要为多少学生输入记录:\n”);
scanf(“%d”和学生);
studentRecords=malloc(sizeof(struct studentDataType)*students);
//检查malloc是否成功。
如果(studentRecords!=NULL)
{       
struct studentDataType*当前_记录=&studentRecords[0];
对于(int i=0;i学生ID),&(当前记录->年度),&(当前记录->月份),&(当前记录->日期),&(当前记录->电话));
}
免费(免费);
}
}

当前代码中存在许多问题。结构成员电话(即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();
}
#包括
#包括
类型定义结构
{
国际学生;
国际年;
整月;
国际日;
char phone[20];//此尺寸应适合本地、长距离和国际
}studentDataType;
studentDataType s,*studentRecords;
内部主(空)
{
//int*studentData=NULL;
//int*studentDataType;
国际学生;
//国际学生;
//国际年;
//整月;
//国际日;
//长手机;
printf(“您要为多少学生输入记录:\n”);
scanf(“%d”和学生);
studentRecords=malloc(学生人数)*;
//studentData=malloc((sizeof(int)*students));
//struct studentDataType*studentRecords=malloc(sizeof(struct studentDataType)*students);
for(int i=0;i!=学生;i++){
printf(“输入学生%d\n的信息”,i);
//struct studentDataType*s=&studentData[i];
scanf(“%d%d%d%d%s”)和studentRecords[i]。studentID,
&学生记录[i].年,
&学习记录[i].月,
&学生记录[i].天,
研究记录[i]。电话);
}
getchar();
}
struct studentDataType
{
国际学生;
国际年;
整月;
国际日;
长手机;
};
int _tmain(int argc,_TCHAR*argv[]
{
国际学生;
printf(“您要为多少学生输入记录:\n”);
scanf(“%d”和学生);
struct studentDataType*studentRecords=(struct studentDataType*)malloc(sizeof(struct studentDataType)*students);
struct studentDataType*student=studentRecords;
for(int i=0;i学生ID),
&(学生->学年),
&(学生->月),
&(学生->日),
&(学生->电话);
student++;//将指针移动到下一个学生
}
//打印信息
学生=学生记录;
for(int i=0;i学生ID,
学生->年,
学生->月,
学生日,
学生->电话);
student++;//将指针移动到下一个学生
}
getchar();
返回0;
}

首先,正如所写的
struct studentDa
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;
}