C 验证回路

C 验证回路,c,loops,scanf,C,Loops,Scanf,这本来是来自另一个程序,但这一部分将无法按我需要的方式工作,我想其他人可能也会遇到麻烦。还需要注意的是,在用户输入被接受后,它将在while循环中使用 printf("would you like to check another time?(y/n)?"); fflush(stdin); scanf("% c", &yesno); while(yesno != 'y' && yesno != 'n') { printf("That was not a valid

这本来是来自另一个程序,但这一部分将无法按我需要的方式工作,我想其他人可能也会遇到麻烦。还需要注意的是,在用户输入被接受后,它将在while循环中使用

printf("would you like to check another time?(y/n)?");
fflush(stdin);
scanf("% c", &yesno);
while(yesno != 'y' && yesno != 'n')
{
   printf("That was not a valid entry, please re entery your choice.");
   fflush(stdin);
   scanf("% c", &yesno);
}/*End of verification loop*/

我希望用户输入一个字符,在验证它是y或n之后,让它进入while循环,如果该字符是y,它将继续程序,如果不是,它将结束程序

    printf("would you like to check another time?(y/n)?\n");
    fflush(stdin);
    scanf("%c", &yesno);
    while(yesno != 'n' && yesno != 'y')
    {
       printf("That was not a valid entry, please re-enter your choice.\n");
       fflush(stdin);
       scanf("%c", &yesno);

    }
    if (yesno == 'n') return 0; // program terminated here

// else it is automatically 'y' so your program continues here ...
附加的

我刚刚注意到另一个影响代码片段的关键故障(我想接下来的代码行也是如此)

附加的

我刚刚注意到另一个影响代码片段的关键故障(我想接下来的代码行也是如此)

附加的

我刚刚注意到另一个影响代码片段的关键故障(我想接下来的代码行也是如此)

附加的

我刚刚注意到另一个影响代码片段的关键故障(我想接下来的代码行也是如此)


请注意,
fflush
仅为输出流定义。在
stdin
上调用
fflush
会调用未定义的行为。您可以使用
getchar
函数读取和丢弃来自
stdin
的无关输入

printf("would you like to check another time?(y/n)?");

// read and discard any number of leading whitespace characters
scanf("% c", &yesno); 
char ch;
while(yesno != 'y' && yesno != 'n') {
    // stdout is line buffered. Outputting a newline will immediately flush
    // the output to the console
    printf("That was not a valid entry, please reenter your choice.\n");

    // if the input character is not a newline, then read and discard
    // all character up till and including the newline 
    if(yesno != '\n')
        while((ch = getchar()) != '\n');  // note the null statement

    // read the input from the user afresh
    scanf("% c", &yesno);
}

请注意,
fflush
仅为输出流定义。在
stdin
上调用
fflush
会调用未定义的行为。您可以使用
getchar
函数读取和丢弃来自
stdin
的无关输入

printf("would you like to check another time?(y/n)?");

// read and discard any number of leading whitespace characters
scanf("% c", &yesno); 
char ch;
while(yesno != 'y' && yesno != 'n') {
    // stdout is line buffered. Outputting a newline will immediately flush
    // the output to the console
    printf("That was not a valid entry, please reenter your choice.\n");

    // if the input character is not a newline, then read and discard
    // all character up till and including the newline 
    if(yesno != '\n')
        while((ch = getchar()) != '\n');  // note the null statement

    // read the input from the user afresh
    scanf("% c", &yesno);
}

请注意,
fflush
仅为输出流定义。在
stdin
上调用
fflush
会调用未定义的行为。您可以使用
getchar
函数读取和丢弃来自
stdin
的无关输入

printf("would you like to check another time?(y/n)?");

// read and discard any number of leading whitespace characters
scanf("% c", &yesno); 
char ch;
while(yesno != 'y' && yesno != 'n') {
    // stdout is line buffered. Outputting a newline will immediately flush
    // the output to the console
    printf("That was not a valid entry, please reenter your choice.\n");

    // if the input character is not a newline, then read and discard
    // all character up till and including the newline 
    if(yesno != '\n')
        while((ch = getchar()) != '\n');  // note the null statement

    // read the input from the user afresh
    scanf("% c", &yesno);
}

请注意,
fflush
仅为输出流定义。在
stdin
上调用
fflush
会调用未定义的行为。您可以使用
getchar
函数读取和丢弃来自
stdin
的无关输入

printf("would you like to check another time?(y/n)?");

// read and discard any number of leading whitespace characters
scanf("% c", &yesno); 
char ch;
while(yesno != 'y' && yesno != 'n') {
    // stdout is line buffered. Outputting a newline will immediately flush
    // the output to the console
    printf("That was not a valid entry, please reenter your choice.\n");

    // if the input character is not a newline, then read and discard
    // all character up till and including the newline 
    if(yesno != '\n')
        while((ch = getchar()) != '\n');  // note the null statement

    // read the input from the user afresh
    scanf("% c", &yesno);
}

您希望它以什么方式工作?我希望用户输入一个字符,在验证它是y或n之后,让它进入while循环,如果该字符是y,它将继续程序,如果不是y,它将结束程序。@user3259144您可以通过输入条件
if(yesno='n')退出程序
然后
scanf之后返回
(“%c”和&yesno)while..loop
中的code>语句。或者如果输入是
y
,则自动将
while..loop
中断并继续您的程序。您可能需要
%c“
(在
%
前面的空格,而不是后面的空格)。在读取非空白字符之前跳过空白,包括换行符。
scanf(“%c”,&yesno)<代码>%c
应该是
%c
没有空格,您希望它以什么方式工作?我希望用户输入一个字符,在验证它是y或n后,让它转到while循环,如果该字符是y,它将继续程序,如果没有,它将结束它。@user3259144您可以在中退出您的程序,方法是将条件
if(yesno='n')
放在
scanf(“%c”,&yesno)之后,然后
返回
while..loop
中的code>语句。或者如果输入是
y
,则自动将
while..loop
中断并继续您的程序。您可能需要
%c“
(在
%
前面的空格,而不是后面的空格)。在读取非空白字符之前跳过空白,包括换行符。
scanf(“%c”,&yesno)<代码>%c
应该是
%c
没有空格,您希望它以什么方式工作?我希望用户输入一个字符,在验证它是y或n后,让它转到while循环,如果该字符是y,它将继续程序,如果没有,它将结束它。@user3259144您可以在中退出您的程序,方法是将条件
if(yesno='n')
放在
scanf(“%c”,&yesno)之后,然后
返回
while..loop
中的code>语句。或者如果输入是
y
,则自动将
while..loop
中断并继续您的程序。您可能需要
%c“
(在
%
前面的空格,而不是后面的空格)。在读取非空白字符之前跳过空白,包括换行符。
scanf(“%c”,&yesno)<代码>%c
应该是
%c
没有空格,您希望它以什么方式工作?我希望用户输入一个字符,在验证它是y或n后,让它转到while循环,如果该字符是y,它将继续程序,如果没有,它将结束它。@user3259144您可以在中退出您的程序,方法是将条件
if(yesno='n')
放在
scanf(“%c”,&yesno)之后,然后
返回
while..loop
中的code>语句。或者如果输入是
y
,则自动将
while..loop
中断并继续您的程序。您可能需要
%c“
(在
%
前面的空格,而不是后面的空格)。在读取非空白字符之前跳过空白,包括换行符。
scanf(“%c”,&yesno)<代码>%c
应该是
%c
且没有空格