scanf()的问题

时间:2014-12-05 08:16:44

标签: c struct scanf fgets

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

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

5 个答案:

答案 0 :(得分:1)

您必须从i = 0而不是i = 1开始才能获得数组的第一个元素。您应该将i <= n替换为i < n

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

答案 1 :(得分:0)

我建议用gets替换scanf(&#34;%s&#34;,id [i] ....) 例如。

gets(ID[i].Middle_Name);

并且从循环中的0开始而不是1

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

答案 2 :(得分:0)

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

//Your input code

}

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

      // your output code

}

答案 3 :(得分:-1)

代码中的语法错误

struct Students ID[n]; 

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

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

答案 4 :(得分:-1)

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