我是C编程的新手。我正在尝试运行此程序,该程序接受来自用户的人员名称,并将这些名称存储在结构数组中。
输入名称的消息将打印在控制台上,但在用户输入字符串之前,将显示下一个语句。 Prog是 -
#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循环,但问题可以在这个循环中排序吗?
感谢。
答案 0 :(得分:1)
Faculty是2d数组。你可以尝试这样的东西来循环询问来自控制台的输入。
gets(point_batch2010->faculty[i]);
答案 1 :(得分:1)
scanf
有一个坏习惯,就是在输入流上留下用户输入的换行符。尝试在其后添加第二个scanf
,消耗单个字符(用户键入的\ n)
有关scanf
为邪恶的详细解释,请参阅http://c-faq.com/stdio/scanfprobs.html和儿童
附:其他一些受访者指出,您的代码中还存在其他问题,但这应该可以帮助您解决这个具体问题。
答案 2 :(得分: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);
}
在for
读取输入后的scanf
循环中,\n
位于缓冲区中,然后在下一次迭代中由fgets
读取。 scanf
读取直到它到达空格或换行符,而fgets
读取neline字符
你需要做
while ( getchar() != '\n' );
在scanf
行之后。
答案 3 :(得分: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));