gpt4 book ai didi

c - 用户输入循环

转载 作者:太空宇宙 更新时间:2023-11-04 04:04:20 26 4
gpt4 key购买 nike

我想编写一个程序,不断询问用户输入,直到我使用 ctrl+D 中断它。这是我的:

char input[100];
while(input != EOF){
printf("Give me a sentence:\n");
fgets(input, 5, stdin);
printf("your sentence was: %s\n", input);
}

我希望 fgets 从新输入的前 5 个字符开始,而不是像上次输入的第 6 个字符那样循环,而且我也不知道如何写 while to break 的条件通过ctrl+D出来。现在你可以看到输入(这是一个 char[] 正在与 EOF 进行比较)。

感谢您的任何建议。

最佳答案

我认为您正在寻找函数 feof。

char input[100];
while(!feof(stdin)){
printf("Give me a sentence:\n");
fgets(input, 5, stdin);
printf("your sentence was: %s\n", input);
}

关于c - 用户输入循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7942503/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com