以下は私のコードのスニペットです。fscanf
の代わりに使用する方が良いことを学びましたscanf
。しかし、 fscanf は入力を待っていません
switch (argc) {
case 2: printf("\nEnter the subject code: ");
while(fgets(temp_op->subject, BUF_NOTES, stdin)==NULL);
case 3: printf("\nEnter the topic: ");
while(fgets(temp_op->topic, BUF_TOPIC, stdin)==NULL);
case 4: printf("\nEnter the Level: ");
flag = fscanf(stdin,"%d",&temp_op->level);
case 5: printf("\nEnter the Answer Key: ");
while(fgets(temp_op->key, BUF_KEY, stdin)==NULL);
case 6: printf("\nEnter any additional notes(optional): ");
while(fgets(temp_op->notes, BUF_NOTES, stdin)==NULL);
break;
default:printf("\nExcess Arguments");
}
問題は ですcase 5
。fgets は入力を待っていませんが、ケース 6 はうまくいっています。
case 4
ただし、 「flag =...」という行をコメントアウトすると、次の fgets で入力を求めるプロンプトが表示されます。奇妙な。以前の fscanf が後の fgets に影響を与えるのはなぜだろうか。私の構造定義は次のとおりです。
typedef struct {
int mode ;
int level;
char subject[BUF_SUBJECT], topic[BUF_TOPIC], notes[BUF_NOTES], key[BUF_KEY];
} operation;
完全なソースはhttp://pastebin.com/HVvGC3B7にあります
何が間違っている可能性がありますか?