【问题标题】:Issues with scanf()scanf() 的问题
【发布时间】:2014-12-05 08:16:44
【问题描述】:

我正在使用一个程序(非常简单),它需要用户使用 scanf 进行输入。这些值存储在结构数组中。无论如何,我认为我的所有语法都正确(如果我不纠正我的话),但是每当我想 printf 查看结果时,我都没有从 ID[i].Middle_Name 获得值。由于我不明白的原因,它似乎是空的。我试图添加一个打印语句来尝试调试它,但仍然没有。也许我错过了什么?

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

struct Students{
  char First_Name[20];
  char Last_Name[20];
  char Middle_Name[20];
  int Age;
};

int main(void){
    int n = 0;  //number of students
    int i = 0;      //counter

  printf("Please enter the total number of students in your class: ");
  scanf("%d", &n);
  struct Students ID[n];
  printf("\n");

  printf("******************** \n");
  printf("After entering student info, they are displayed in full_name/middle_name/age order. \n");
  printf("******************** \n");

  for(i = 1; i <= n; i++){
      printf("Please enter the first name of student with  ID: %d \t", i);
      scanf("%s", (ID[i].First_Name));
      printf("\n");

      printf("Please enter the last name of student with ID: %d \t", i);
      scanf("%s", (ID[i].Last_Name));
      printf("\n");

      printf("Please enter the middle name of student ID: %d \t", i);
      scanf("%s", (ID[i].Middle_Name)); 
      printf("\n");

      printf("Please enter the age of student ID: %d \t", i);
      scanf("%d", &(ID[i].Age));
      printf("\n");

  }
  printf("In your class, we have: \n");
  printf("%s", ID[1].Middle_Name);

  for(i = 1; i <= n; i++){
      printf("%s \t", ID[i].First_Name); 
      printf("%s \t", ID[i].Last_Name); 
      printf("%s \t", ID[i].Middle_Name); 
      printf("%d \n", ID[i].Age); 
  }
  return(0);
}

【问题讨论】:

  • 你的输入格式是什么?
  • C 中的起始索引为 0 而不是 1,因此您的 for 应以 i=0 开头

标签: c struct scanf fgets


【解决方案1】:

您必须以i = 0 而不是i = 1 开头才能获取数组的第一个元素。您应该将i &lt;= n 替换为i &lt; n

for(i = 0; i < n; i++)
{
    ...
}

【讨论】:

  • 感谢您查看此问题,令人惊讶的是,它解决了。你能解释一下为什么会这样吗?为什么这与我以前的方法不同。不管数组的位置如何,ID[i].Middle_Name 不应该有一个值吗?
  • @RenegadeJ 数组的第一个元素位于索引 0 处。通过编写“ID[1].Middle_Name”,您正在寻找数组第二个元素的中间名。如果您只使用数组中的一个元素进行测试,则 ID[1] 不存在并且应该导致超出范围的错误。换句话说,如果你创建一个有 10 个元素的数组,最后一个元素将在索引 9 处,第一个元素在索引 0 处。
【解决方案2】:

我建议将 scanf ("%s", id[i]. ...) 替换为 gets 例如。

gets(ID[i].Middle_Name);

并且在循环中也从 0 开始,而不是 1

for(i = 0; i

【讨论】:

  • 感谢您的提示,会的。此外,从 0 开始实际上修复了它。你知道这是为什么吗?我的意思是,只要您处理相同/正确的数组索引,它从哪里开始并不重要,对吧?
  • 你是对的,如果你不小心按了姓氏中的空格,那也可能是造成这种情况的原因。
【解决方案3】:
for(i = 0; i <= n; i++)
{

//Your input code

}

for(i = 0; i <= n; i++)
{

      // your output code

}

【讨论】:

  • 如果你保持这个条件,你会循环一个太多次。您必须将 'i
  • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
【解决方案4】:

您的代码中有错误的语法

struct Students ID[n]; 

数组长度必须是一个常量,或者你可以像这样使用new

struct Students *ID = new struct Students[n];

【讨论】:

  • ID[n] 是正确的,是一个 VLA。您的“新”是 C++ 而不是 C。
【解决方案5】:
// <-- note leading ' ' on all format strings
// <-- always check returned value from input statements
// <-- following line would try to access past end of array, 
//     arrays start with offset of 0,
//     for(i = 1; i <= n; i++)

#include <stdio.h>
#include <stdlib.h>
//#include <string.h>
//#include <stdarg.h>

struct Students
{
  char First_Name[20];
  char Last_Name[20];
  char Middle_Name[20];
  int Age;
};

int main(void){
    int n = 0;  //number of students
    int i = 0;      //counter

  printf("Please enter the total number of students in your class: ");
  if( 1 != scanf(" %d", &n) )
  {
      perror( "scanf failed reading num students" );
      exit( EXIT_FAILURE );
  }

  struct Students ID[n];
  printf("\n");

  printf("******************** \n");
  printf("After entering student info, they are displayed in full_name/middle_name/age order. \n");
  printf("******************** \n\n");


  for( i=0; i< n; i++ )
  {
      printf("Please enter the first name of student with  ID: %d \t", i+1);
      if( 1 != scanf(" %s", (ID[i].First_Name)) )
      {
          perror( "scanf failed for first name" );
          exit( EXIT_FAILURE );
      }

      printf("\n");

      printf("Please enter the last name of student with ID: %d \t", i+1);
      if( 1 != scanf(" %s", (ID[i].Last_Name)) ) // <-- check returned value
      {
          perror( "scanf failed for last name" );
          exit( EXIT_FAILURE );
      }

      printf("\n");

      printf("Please enter the middle name of student ID: %d \t", i+1);
      if( 1 != scanf(" %s", (ID[i].Middle_Name)) ) // <-- check returned value
      {
          perror( "scanf failed for middle name" );
          exit( EXIT_FAILURE );
      }

      printf("\n");

      printf("Please enter the age of student ID: %d \t", i+1);
      if( 1 != scanf(" %d", &(ID[i].Age)) ) // <-- check returned value 
      {
          perror( "scanf failed for student age" );
          exit( EXIT_FAILURE );
      }

      printf("\n");
  } // end while

  printf("In your class, we have: \n");
  printf("%s", ID[1].Middle_Name);

  // <-- following line would try to access past end of array, arrays start with offset of 0, not 1
  //for(i = 1; i <= n; i++)
  for( i=0; i<n; i++ )
  {
      printf("%s \t", ID[i].First_Name);
      printf("%s \t", ID[i].Last_Name);
      printf("%s \t", ID[i].Middle_Name);
      printf("%d \n", ID[i].Age);
      printf("\n"); // put each student on a new line
  } // end for
  return(0);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-21
    • 2011-07-21
    • 2012-12-28
    • 2011-05-20
    • 1970-01-01
    相关资源
    最近更新 更多