如何在输入方面使用循环(C 语言)?
How to use loops in terms of input (in C language)?
我一直在尝试让这段代码起作用,但循环似乎不起作用?我是 C 的新手,我对这种语言的语法有点困惑。但是我的循环没有像我想要的那样运行。我希望 if 和 else 语句起作用,但无论输入什么(正确或错误)它总是输出 "thank you".
#include <stdio.h>
#include <stdlib.h>
int confirm()
{
char c;
printf("Confirm (y/n): ");
scanf("%c", &c);
while (scanf("%c", &c))
{
if (c = 'Y' && 'y' && 'N' && 'n')
{
printf("\nthank you");
break;
}
else
{
printf("\nInput not recognised, try again. \n");
printf("\nConfirm (y/n): ");
scanf("%c", &c);
}
}
}
int main(int argc, char* agrv[])
{
confirm();
return 0;
}
当输出不正确时,它不会要求输入另一个输出。它只是从 if 语句结束,因此循环不是 运行?
请帮忙。
您的循环没有任何问题 - 错误的是 if
语句。
此代码可以编译,但它没有执行您想要的操作:
if (c = 'Y' && 'y' && 'N' && 'n')
=
是赋值;您需要 ==
才能进行比较
&&
表示"AND";你需要 ||
,这意味着 "OR"
- 您将逻辑表达式而非常量与
&&
或 ||
组合
条件应该是
if (c == 'Y' || c == 'y' || c == 'N' || c == 'n')
另请注意,当您使用 %c
读取单个字符时,您的程序 "sees" 所有字符,包括白色 space。这是一个问题,因为缓冲区中剩余的 '\n'
将在 Y
或 N
之前传递给您的程序。要解决此问题,请在格式字符串 %c
之前添加 space:
scanf(" %c", &c)
// ^
// |
// Here
您的代码还忽略了它读取的第一个字符。我认为这不是故意的,所以在循环之前删除 scanf
的调用。您还应该从循环中删除第二个 scanf
,在循环 header.
中留下对 scanf
的唯一调用
int confirm()
{
char c;
printf("Confirm (y/n): ");
//scanf("%c", &c);// <---------- needless
while (scanf("%c", &c)) //<----while loop will do `scanf("%c",&c)`, so previous line should be remove.
{
if (c == 'Y' || c == 'y' || c == 'N' || c == 'n')// <- &&(AND); ||(OR). Also, be careful that don't be lazy, [c == 'Y' || 'y' || 'N' || 'n'] can't to communicate with computer
{
printf("\nthank you");
break;
}
else
{
printf("\nInput not recognised, try again. \n");
printf("\nConfirm (y/n): ");
scanf("%c", &c);
}
}
}
我一直在尝试让这段代码起作用,但循环似乎不起作用?我是 C 的新手,我对这种语言的语法有点困惑。但是我的循环没有像我想要的那样运行。我希望 if 和 else 语句起作用,但无论输入什么(正确或错误)它总是输出 "thank you".
#include <stdio.h>
#include <stdlib.h>
int confirm()
{
char c;
printf("Confirm (y/n): ");
scanf("%c", &c);
while (scanf("%c", &c))
{
if (c = 'Y' && 'y' && 'N' && 'n')
{
printf("\nthank you");
break;
}
else
{
printf("\nInput not recognised, try again. \n");
printf("\nConfirm (y/n): ");
scanf("%c", &c);
}
}
}
int main(int argc, char* agrv[])
{
confirm();
return 0;
}
当输出不正确时,它不会要求输入另一个输出。它只是从 if 语句结束,因此循环不是 运行? 请帮忙。
您的循环没有任何问题 - 错误的是 if
语句。
此代码可以编译,但它没有执行您想要的操作:
if (c = 'Y' && 'y' && 'N' && 'n')
=
是赋值;您需要==
才能进行比较&&
表示"AND";你需要||
,这意味着 "OR"- 您将逻辑表达式而非常量与
&&
或||
组合
条件应该是
if (c == 'Y' || c == 'y' || c == 'N' || c == 'n')
另请注意,当您使用 %c
读取单个字符时,您的程序 "sees" 所有字符,包括白色 space。这是一个问题,因为缓冲区中剩余的 '\n'
将在 Y
或 N
之前传递给您的程序。要解决此问题,请在格式字符串 %c
之前添加 space:
scanf(" %c", &c)
// ^
// |
// Here
您的代码还忽略了它读取的第一个字符。我认为这不是故意的,所以在循环之前删除 scanf
的调用。您还应该从循环中删除第二个 scanf
,在循环 header.
scanf
的唯一调用
int confirm()
{
char c;
printf("Confirm (y/n): ");
//scanf("%c", &c);// <---------- needless
while (scanf("%c", &c)) //<----while loop will do `scanf("%c",&c)`, so previous line should be remove.
{
if (c == 'Y' || c == 'y' || c == 'N' || c == 'n')// <- &&(AND); ||(OR). Also, be careful that don't be lazy, [c == 'Y' || 'y' || 'N' || 'n'] can't to communicate with computer
{
printf("\nthank you");
break;
}
else
{
printf("\nInput not recognised, try again. \n");
printf("\nConfirm (y/n): ");
scanf("%c", &c);
}
}
}