[英]How to check if variable is NOT something
Basically I want to check if char a
is not 'y'
or 'n'
. 基本上我想检查char a
是否不是'y'
或'n'
。 Been trying to figure it out for an hour now and could not find anything. 一直试图找出一个小时,却找不到任何东西。
#include<stdio.h>
int yesno(char a){
do{
printf(":");
scanf("%s",&a);
if((a!='y')||(a!='n')){
printf("Incorrect awnser, try again\n");
}
}while((a!='y')||(a!='n'));
}
int main(){
printf("************************************\n");
printf("*Welcome to 'noname' 0.01 *\n");
printf("*Do you want to start y/n? *\n");
printf("************************************\n");
yesno(1);
return 0;
}
Could someone tell me how to do that? 有人可以告诉我该怎么做吗? How to make it check if something is NOT something. 如何检查是否有东西。 This code is what my understanding of C allows me to create, but it's not working correctly, it just loops: Incorrect answer, try again 这段代码是我对C的理解允许我创建的代码,但是它无法正常运行,只会循环: 错误的答案,然后重试
"if((a!='y')||(a!='n')){
printf("Incorrect awnser, try again\n");
}"
You could use a switch-case statement, with the default being the printf() statement. 您可以使用switch-case语句,默认值为printf()语句。
switch(a) {
case 'y':
// do nothing
break;
case 'n':
// do nothing
break;
default:
printf("Incorrect answer, try again\n");
};
As suggested in the comments, you should use the &&
operator instead of ||
如注释中所建议,您应该使用&&
运算符而不是||
to check if a
is neither 'y'
nor 'n'
like 检查a
是否既不是'y'
也不是'n'
if((a!='y') && (a!='n')){
printf("Incorrect awnser, try again\n");
}
}while((a!='y') && (a!='n'));
The condition ((a!='y') || (a!='n'))
will always be true as a
cannot be 'y'
and 'n'
at the same time. 条件((a!='y') || (a!='n'))
将始终为true,因为a
不能同时为'y'
和'n'
。
And in yesno()
, a
is a char
and not a char
array capable of holding strings. 在yesno()
, a
是一个char
而不是一个能够容纳字符串的char
数组。
So 所以
scanf("%s",&a);
should be 应该
scanf("%c",&a);
as the format specifier for a char
is %c
whereas %s
is for strings. 因为char
的格式说明符是%c
而%s
是字符串。
Even if a
were a char
array to store a string, the scanf()
should've been 即使a
是用于存储字符串的char
数组, scanf()
应该
scanf("%s",a);
as array name in C decay into pointer to its base element. 作为C中的数组名称,将衰减为指向其基本元素的指针。
And you could do away with that argument of yesno()
as the initial value of a
is irrelevant since a new value of a
is read in at the beginning of yesno()
and any changes made to a
will not have any repercussion back in main()
as the parameter is passed by value and yesno()
doesn't return a value. 而你可以破除的这样的说法yesno()
作为初始值a
,因为一个新的值是不相关的a
是在开始阅读yesno()
和所做的任何更改a
不会有任何反响回main()
因为参数是通过值传递的,而yesno()
不会返回值。
So, instead of making a
a parameter, you could make it a local variable of yesno()
like 所以,不是使的a
参数,你可以把它的局部变量yesno()
像
void yesno(){//or yesno(void)
char a;
do
{
printf(":");
scanf("%c",a);
if((a!='y') && (a!='n')){
printf("Incorrect awnser, try again\n");
}
}while((a!='y') && (a!='n'));
}
And call it like 并称它为
yesno();
in main()
. 在main()
。
You can use continue and break statements inside the if condition. 您可以在if条件中使用continue和break语句。
#include <stdio.h>
int yesno(char a){
do{
printf(":");
scanf("%s",&a);
if((a=='y')||(a=='n')){
break;
}
else{
printf("Incorrect answer, try again\n");
continue;
}
}while((a!='y')||(a!='n'));
}
int main(){
printf("************************************\n");
printf("*Welcome to 'noname' 0.01 *\n");
printf("*Do you want to start y/n? *\n");
printf("************************************\n");
yesno(1);
return 0;
}
I just changed the if condition and added the else part. 我只是更改了if条件,并添加了else部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.