Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
for循环在C中运行两次_C - Fatal编程技术网

for循环在C中运行两次

for循环在C中运行两次,c,C,我是C编程新手。我在做一个练习,问题是这样的:使用?:运算符和for语句来编写一个程序,该程序会一直使用用户输入的字符,直到字符q被计算出来 这是我写的程序: #include <stdio.h> main() { int x, i=0; for (x = 0; x == 'q'? 0 : 1; printf("Loop %d is finished\n",i)) { printf("Enter q to exit!!!\n");

我是C编程新手。我在做一个练习,问题是这样的:使用?:运算符和for语句来编写一个程序,该程序会一直使用用户输入的字符,直到字符q被计算出来

这是我写的程序:

#include <stdio.h>

main()
{
    int x, i=0;
    for (x = 0; x == 'q'? 0 : 1; printf("Loop %d is finished\n",i))
    {
        printf("Enter q to exit!!!\n");
        printf("Please enter a character:\n");
        x=getc(stdin);
        putc(x,stdout);
        ++i;
    }
    printf("\nThe for loop is ended. Bye!");

    return 0;       
}
#包括
main()
{
int x,i=0;
对于(x=0;x==“q”?0:1;printf(“循环%d已完成\n”,i))
{
printf(“输入q退出!!!\n”);
printf(“请输入字符:\n”);
x=getc(标准偏差);
putc(x,stdout);
++一,;
}
printf(“\n for循环结束。再见!”);
返回0;
}
问题是:每次我输入一个“非q”字符,循环似乎运行两次。 我不知道我的程序出了什么问题。
请帮忙

当您输入一个非
q
字母时,您还可以点击回车键,该键在第二个循环中读取


要使循环每次输入只运行一次,请使用
fgets()
一次读取整行输入,并检查输入字符串是否符合您的期望。

当您键入
a
然后按
Enter
时,换行符将成为
stdin
流的一部分。读取
a
后,下次执行
x=getc(stdin)
时,
x
的值设置为
\n
。这就是循环执行两次迭代的原因。

循环运行两次,因为当您输入非
q
字符时,实际上输入了两个字符-非
q
字符和换行符
'\n'
字符<代码>x=getc(标准输入)从
stdin
流中读取非
q
字符,但换行符仍位于
stdin
的缓冲区中,该缓冲区在下一次
getc
调用中读取

您应该使用
fgets
从流中读取其他人建议的行,然后您可以处理该行。此外,还应将
main
的返回类型指定为
int
。我建议作出以下更改:

#include <stdio.h>

int main(void)
{
    int x, i = 0;

    // array to store the input line
    // assuming that the max length of
    // the line is 10. +1 is for the 
    // terminating null added by fscanf to
    // mark the end of the string
    char line[10 + 1];

    for (x = 0; x == 'q'? 0 : 1; printf("Loop %d is finished\n", i))
    {
        printf("Enter q to exit!!!\n");
        printf("Please enter a character:\n");

        // fgets reads an input line of at most 
        // one less than sizeof line, i.e., 
        // 10 characters from stdin and saves it 
        // in the array line and then adds a 
        // terminating null byte
        fgets(line, sizeof line, stdin);

        // assign the first character of line to x
        x = line[0];
        putc(x, stdout);
        ++i;
    }
    printf("\nThe for loop is ended. Bye!");

    return 0;       
}
#包括
内部主(空)
{
int x,i=0;
//数组来存储输入行
//假设
//该行为10。+1表示
//由fscanf添加到的终止null
//标记字符串的结尾
字符行[10+1];
对于(x=0;x==“q”?0:1;printf(“循环%d已完成\n”,i))
{
printf(“输入q退出!!!\n”);
printf(“请输入字符:\n”);
//fgets读取的输入行最多为
//比生产线尺寸小一个,即:。,
//从标准输入中输入10个字符并保存
//在数组行中,然后添加
//终止空字节
fgets(生产线、生产线尺寸、标准尺寸);
//将行的第一个字符指定给x
x=线[0];
putc(x,stdout);
++一,;
}
printf(“\n for循环结束。再见!”);
返回0;
}

当您输入一个字符,比如说“x”并按enter键时,实际上您输入了两个字符,即“x”和“\n”,也称为换行符(当您按enter键时)。“\n”将成为输入流的一部分,并为其执行循环

此外,尝试输入“xyz”并点击回车键,循环将执行4次。对于每个“x”、“y”、“z”和“\n”

如果希望代码对每个输入工作一次,请使用函数get

#include <stdio.h>

main()
{
    int i=0;
    char x[10];
    for ( ; x[0]!='q'; printf("Loop %d is finished\n",i) )
    {
        printf("Enter q to exit!!!\n");
        printf("Please enter a character:\n");
        gets(x);
        i++;
    }
    printf("\nThe for loop is ended. Bye!");

    return 0;
}
#包括
main()
{
int i=0;
charx[10];
对于(;x[0]!='q';printf(“循环%d已完成\n”,i))
{
printf(“输入q退出!!!\n”);
printf(“请输入字符:\n”);
得到(x);
i++;
}
printf(“\n for循环结束。再见!”);
返回0;
}

在这段代码中,我们将x声明为一个字符串,gets()函数读取我们输入的整行,然后在for循环的条件部分,我们检查字符串的第一个字符是否为“q”。

那么如何使它只运行一次呢?换句话说,
getc(stdin)有两个字符阅读,它完成了它的工作!使用
fgets
阅读整行内容。您可能希望使用阅读有关安全性的内容。或者,查看
getc
get
\n
并在这种情况下执行另一个
getc
。和handle
EOF
。似乎您需要清除输入缓冲区,因为我认为您正在读新行。