問題是fgets在循環的第一次迭代中沒有取任何值。只需簡單地跳過它。照我看來。我在那裏有一些基本的錯誤,但我沒看到它。 Fgets只在2d迭代時開始取值。Fgets - 不能在循環中工作
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main(void)
{
int t;
scanf("%i", &t);
char ways[100];
for (int j = 0; j <= t; t++)
{
fgets (ways, 100, stdin);
printf("Ways = %s\n", ways);
}
return 0;
}`
請修復您問題的格式。請注意,您的代碼中未包含#include。 –
由於第一個'scanf'在輸入緩衝區中爲'fgets'離開'newline'來讀取 - 不要混用你的方法!用'fgets'和'sscanf'輸入't'。 –
「在循環的第一次迭代中獲取並沒有任何價值」 - >是的。 'fgets()'「把''\ n''從前一行中取出。 – chux