gpt4 book ai didi

c - 理解我的 while do if c 代码的逻辑吗? 37 行/空格代码

转载 作者:行者123 更新时间:2023-11-30 16:02:54 25 4
gpt4 key购买 nike

我正在编写的代码基本上是一个要求用户输入名字的问题。如果名称为空,即用户忘记输入名字,则代码将提示用户忘记输入名称,并再次询问。它应该继续询问,直到满足条件。

// This sample compares user input to what is being typed. If the 
// input is void of any charicters before pressing enter, it will print a reply.

#include <stdio.h>
#include <string.h>

int main()
{
char firstname[25];

printf("Please enter your first name:");
fgets(firstname,25,stdin);

// ask to enter in a name. If no name or text is present,
// will reply with no name entered
// and will loop untill a name is pressed and entered

do
{
printf("You pressed enter before entering your first name.\n Please enter first name");
}
while (firstname == NULL || strcmp(firstname,"")==0);

if(!strcmp(firstname, "firstname"))
{
printf("Thank you %s! for entering in your first name",firstname);
}

getchar();
}

它只循环一次。所以,不知道为什么它不会继续,并且,打破循环说“谢谢你%s!任何人都可以举一个不同的例子,这样它确实有效并且我可以更好地理解它吗?

最佳答案

do...while 循环中,只有一条 printf 语句,该语句不会更改循环条件。考虑将行 fgets(firstname,25,stdin) 移到循环内。

关于c - 理解我的 while do if c 代码的逻辑吗? 37 行/空格代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4651733/

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