c - 如何在c中制作while循环

标签 c while-loop

我正在尝试在我的 C 代码中进行 while 循环

像这样:

main()
{
char q ;
while( a == 'yes' )   
{
    /* my code */
    printf("enter no to exit or yes to continue");
    scanf("%s",q);
 } 
}

但是当我输入字符“q”时......控制台崩溃了

停止工作

我在 while 循环中出了什么问题??

最佳答案

您不能将字符串与 a == 'yes' 进行比较。您需要使用 strcmp 函数。

你需要这样的东西:

int main(int argc, char **argv)
{
    char a[200];
    strcpy(a, "yes");
    while( !strcmp(a, "yes") )
    {
       /* my code */
       printf("enter no to exit or yes to continue");
       scanf("%s",a);
    } 
}

关于c - 如何在c中制作while循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10720991/

相关文章:

c - 读取当前目录,根据文件属性打印结果

c - 我正在尝试使用 C 编码从 linux 制作 uniq 命令

java - 使用 while() 循环检查 .txt 文件中的元素

c# - 有关在C#中执行while循环的问题

Perl 在 while 循环中挂起

c - 在c中的不同函数中修改字符串数组

c - 对包含从 C 中的文件输入的数字的字符串进行排序?

c - 确定C中char数组的长度

c - 如果输入无效输入(在 C 编程中),如何使该程序再次请求输入?

php - 我可以将这两个 while 循环合并为一个吗?值没有正确插入?