【问题标题】:While loop comparisons of strings/characters with fgets [duplicate]While 循环比较字符串/字符与 fgets [重复]
【发布时间】:2015-12-15 01:23:57
【问题描述】:

我正在尝试在 C 中使用 fgets 获取用户输入,但在使用 while 循环时遇到了问题。

这是我正在使用的代码:

char input[300];

fgets(input, 300, stdin);

while(strcmp("Quit", input) != 0) {

    fgets(input, 300, stdin);

}

当我输入Quit 时,循环继续并且不会终止,我不明白为什么会这样。

【问题讨论】:

标签: c char fgets


【解决方案1】:

fgets() 也将换行符存储在 input 中。要么手动删除它,要么像这样比较:

while(strcmp("Quit\n", input) != 0)

【讨论】:

  • 如何删除换行符?
  • 和EOL类似吗?
  • @code 类似input[strlen(input) - 1] = '\0';
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-25
相关资源
最近更新 更多