为什么不是这个节目让我当我需要太多输入信息? [英] Why isn't this program allowing me to enter information when I need too?

查看:245
本文介绍了为什么不是这个节目让我当我需要太多输入信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧首先我会解释我的任务。对于这个任务,我不得不使用它我有没有问题,动态内存分配。什么我有是搞清楚工作我分配正确的方式有问题。对于我的任务,我需要建立一个程序,提示用户输入他们有多少学生然后问以下信息;学号,生日和电话号码。我需要使用一个循环来提示用户输入所有学生的信息。我需要创建一个循环,就能把所有的学生ID扫描和使用他们的生日发现的最古老的学生(循环必须通过3个以上的学生能够扫描)。

下面是我的code,我已经得到了一些建议,并从你们甚至code位,但它不是让我进入学生信息时,它到达for循环,它只是结束程序。帮助

感谢您。

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;结构studentDataType
{
    INT studentID;
    INT年;
    INT月;
    日整型;
    长长的电话;
};INT主要(无效)
{
    为int * studentData = NULL;
    INT * studentDataType;
    INT学生;
    INT studentID;
    INT年;
    INT月;
    日整型;
    长长的电话;    的printf(有多少学生进入你的记录:\\ n);
    scanf函数(%d个,&安培;生);    studentData =的malloc((的sizeof(int)的*学生));    结构studentDataType * studentRecords =的malloc(sizeof的(结构studentDataType)*学生);    的for(int i = 0;!I =学生; ++ I){
        的printf(学生%d个\\ n输入信息,I + 1);
        结构studentDataType * S =安培; studentData [I]
        scanf函数(%D%D%D,及(S-> studentID),及(S->一年),及(S->当月),及(S- >天),及(S->电话));
    }
}


解决方案

编辑:改变了创纪录的迭代器,并增加了一些错误的malloc()函数的结果检查

您在你的code的几个问题,所以我只是发表一些我认为应该工作,你可以问的具体问题,如果你愿意的话。
请尝试以下操作:

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;结构studentDataType
{
    INT studentID;
    INT年;
    INT月;
    日整型;
    长长的电话;
};INT主要(无效)
{
    结构studentDataType * studentRecords = NULL;
    unsigned int类型的学生;
    无符号整型studentID;
    无符号整型年;
    无符号整型月;
    无符号整型日;
    unsigned long类型的手机;    的printf(有多少学生进入你的记录:\\ n);
    scanf函数(%d个,&安培;生);    studentRecords =的malloc(sizeof的(结构studentDataType)*学生);    //检查的malloc是否成功。
    如果(studentRecords!= NULL)
    {
        结构studentDataType * current_record =安培; studentRecords [0];
        的for(int i = 0; I<学生++我,current_record ++){
            的printf(学生%d个\\ n输入信息,I + 1);
            scanf函数(%U%U%U%U%U,及(current_record-> studentID),及(current_record->一年),及(current_records->当月),及(current_record- >天),及(current_records->电话));
        }
        免费(studentRecords);
    }
}

Ok firstly I'll explain my assignment. For this assignment I have to use dynamic memory allocation which I am having no problems with. What I am having a problem with is figuring out the correct way to work my assignment. For my assignment I need to create a program that prompt the user to enter how many students they have then ask for the following information; Student ID, Birthdate, and Phone number. I need to use a loop to prompt the user to enter all the students information. I need to create a loop that will scan through all the student IDs and find the oldest student using their birthdate (The loop must be able scan through more then 3 students).

Here is my code, I've gotten some suggestions and even bits of code from you guys, but it's not allowing me to enter the students information when it gets to the for loop it just ends the program. Help

Thank you.

#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));
    }
}

解决方案

EDIT: Changed the record iterator and added some error checking on the malloc() result.

You have several issues in your code, so I am just posting something I believe should work and you can ask specific questions if you'd like. Try the following:

#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);
    }
}

这篇关于为什么不是这个节目让我当我需要太多输入信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆