1

我是 C 编程的新手。我正在尝试运行这个程序,它接受来自用户的人名,并将这些人名存储在结构数组中。

输入名称的消息会打印在控制台上,但在用户输入字符串之前,会显示下一条语句。前卫是 -

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

//10 students taken in per batch
#define NUMBER_OF_SEATS 10
#define ROLL_NUM 4

//5 faculty members teach every batch
#define NUMBER_OF_FACULTY 5
#define LENGTH_OF_NAME 50


int main () 
{
//Details of a department
struct dept 
{
    char faculty[NUMBER_OF_FACULTY][LENGTH_OF_NAME];
    int id_students[ROLL_NUM];
    char students[NUMBER_OF_SEATS][LENGTH_OF_NAME];

};

struct dept batch2010[NUMBER_OF_SEATS];
struct dept *point_batch2010 = batch2010;

//Printing the size of the defined strucutre
printf("%lud",sizeof(struct dept));


for (int i = 0; i<NUMBER_OF_FACULTY; i++) 
{
    printf("\nEnter name of faculty %d teaching batch of 2010\n",i+1);
    gets(*point_batch2010->faculty);
    point_batch2010++;
}
printf("\n\n");
point_batch2010 = batch2010;

printf("\nFaculty members are:\n");
for (int i = 0; i<NUMBER_OF_FACULTY; i++)
{
    puts(*point_batch2010->faculty);
    point_batch2010++;
}
point_batch2010 = batch2010;

**for (int i = 0; i<NUMBER_OF_SEATS; i++) 
{
    printf("\nEnter name of student studying in batch of 2010");
    fgets(*point_batch2010->students, LENGTH_OF_NAME, stdin);
    printf("\nEnter roll number of student %d studying in batch 0f 2010\n",i+1);
    scanf("%d",point_batch2010->id_students);
}**

for (int i = 0; i<NUMBER_OF_SEATS; i++) 
{
    puts(*batch2010[i].students);
}
return 0;
}   

控制台上的错误是 -

Enter name of student studying in batch of 2010Anita
Enter roll number of student 1 studying in batch 0f 2010
1234
Enter name of student studying in batch of 2010
Enter roll number of student 2 studying in batch 0f 2010
3467
Enter name of student studying in batch of 2010
Enter roll number of student 2 studying in batch 0f 2010

我猜这个错误与 '\n' 字符有关,我可以将代码分成 2 个不同的 for 循环,但问题可以在这个循环本身中排序吗?

谢谢。

4

4 回答 4

1

教师是二维数组。你可以在你的循环中尝试这样的事情,从控制台询问输入。

gets(point_batch2010->faculty[i]);
于 2013-06-05T15:19:15.213 回答
1

scanf有将用户输入中的换行符留在输入流上的坏习惯。尝试scanf在它之后添加一个消耗单个字符(用户键入的 \n)的秒。
请参阅http://c-faq.com/stdio/scanfprobs.htmlscanf和 children 了解为什么是邪恶的更详细的解释。
PS 其他一些受访者指出您的代码中还有其他问题,但这应该可以帮助您解决这个特定问题。

于 2013-06-05T15:19:54.023 回答
1
for (int i = 0; i<NUMBER_OF_SEATS; i++) 
{
    printf("\nEnter name of student studying in batch of 2010");
    fgets(*point_batch2010->students, LENGTH_OF_NAME, stdin);
    printf("\nEnter roll number of student %d studying in batch 0f 2010\n",i+1);
    scanf("%d",point_batch2010->id_students);
}

问题是fgets读取 a\nscanf将其留在缓冲区中。

在读取输入for后的循环中,位于缓冲区中,然后在下一次迭代中读取。读取直到到达空格或换行符,而与换行符一起读取scanf\nfgetsscanffgets

你需要做

while ( getchar() != '\n' ); 

在你的scanf行之后。

于 2013-06-05T15:19:56.383 回答
1

建议将人工输入与扫描(解析)分开,从而避免邪恶scanf()

for (int i = 0; i<NUMBER_OF_SEATS; i++)  {
  char buffer[LENGTH_OF_NAME];
  printf("\nEnter name of student studying in batch of 2010");
  fgets(buffer, sizeof(buffer), stdin);
  sscanf(buffer, "%[^\n]", *point_batch2010->students);
  // using fgets(*point_batch2010->students, ...) puts the '\n' in the name  

  char number[256];
  printf("\nEnter roll number of student %d studying in batch 0f 2010\n",i+1);
  fgets(number, sizeof(number), stdin);
  sscanf(number, "%d",point_batch2010->id_students);
}

注意:我认为你希望你的结构int id_students /*[ROLL_NUM] */;sscanf(number, "%d",/* note the & */ & (point_batch2010->id_students));

于 2013-06-05T16:28:17.980 回答