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 ループに分割できますが、この 1 つのループ自体で問題を分類できますか?

ありがとう。

4

4 に答える 4