【问题标题】:Why isn't this program allowing me to enter information when I need too? [duplicate]为什么这个程序不允许我在需要时输入信息? [复制]
【发布时间】:2013-11-12 21:21:49
【问题描述】:

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

【问题讨论】:

  • 一方面,所写的struct studentDataType * s = &amp;studentData[i]; 甚至不会编译,因为studentData 是一个指向int 的指针,显然s 是不是。所以标题中你的问题的直接答案是:“因为代码不会编译。”并且输入格式字符串中的最后一个%ds-&gt;phone 的数据类型不匹配,后者是long long,因此假设您修复了损坏的编译,您在那里有未定义的行为。
  • 如果你不能说我对此很陌生,那么请你给我一个解决方案。
  • 由于代码无法编译,请查看错误输出以了解有关错误的信息。
  • 从 for 循环中删除整数 i 的声明,并使其高于 for 循环(您已声明其他变量的地方),这些类型的初始化仅在 C99 标准中允许。
  • “给我一个解决方案” - 嗯.. 如果你想要解决的唯一“新”问题就是向人们寻求解决方案,我想我可以做到。我相当肯定这不是你所学课程的既定目标。我已经指出了两个重要问题。处理这些。在this, questionthis question 和当前问题之间,你已经吃到了大部分的勺子。

标签: c arrays loops structure


【解决方案1】:
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;
}

【讨论】:

    【解决方案2】:

    您当前的代码中存在许多问题。 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();
    }
    

    【讨论】:

      【解决方案3】:

      编辑:更改了记录迭代器并在 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);
          }
      }
      

      【讨论】:

      • 我仍然遇到无法输入任何学生信息的问题。我在这里做错了什么?
      • 没关系,我想通了,谢谢
      • 很高兴你解决了它!我修正了一个错字:对于“i
      • 如何创建一个循环来搜索学生并返回最年长学生的 ID?
      • @djbuijs - 一旦执行流程离开 for 块, s 就会消失。它应该根据学生的数量在 for 块之前定义和分配。
      猜你喜欢
      • 2022-11-18
      • 1970-01-01
      • 2021-10-17
      • 2013-07-10
      • 2020-11-24
      • 1970-01-01
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多