I am using switch case for my boolean expression with answer either yes or no. But i want it to loop back if the answer is invalid. How do i use if or while loop with answer in Character can varies up as 'y' 'Y' 'n' 'N'? could the below if statement work?
if (mstatus != 'y', 'Y', 'N', 'n')
{
switch(mstatus)
case 'y':
printf("You have answer yes for married");
break;
case 'Y':
printf("You have answer yes for married");
break;
case 'n':
printf("You have answer no for married");
break;
case 'N':
printf("You have answer no for married");
break;
}
else
{
default:
printf("please re-enter a valid answer");
scanf(" %c", &mstatus);
}
return 0;
答案 0 :(得分:1)
You don't need the if..else
block there at that way, at all.
In case mstatus
is not having either of y', 'Y', 'N', 'n'
, the control will go to the default
case.
You can, however, put the switch
statement block in a while(1)
/ while(someFlag)
loop and (un)set the someFlag
when you want to break out of the looping.
答案 1 :(得分:1)
The general format for the switch
statement is the following :
switch(expression) {
case constant-expression :
statement(s);
break; /* optional */
case constant-expression :
statement(s);
break;
.
.
.
default :
statement(s);
break;
This means that the default
choice will be selected of none of the cases is matched. So you do not need to include the default
case in and else statement. You can have a further look about switch at this link.
If you want to continuously read a character until you match one of the cases, include the switch
inside a for loop, like this :
int flag = 0;
for (; flag == 1;)
{
switch(mstatus)
{
case 'y':
case 'Y':
printf("You have answer yes for married");
flag = 1;
break;
case 'n':
case 'N':
printf("You have answer no for married");
flag = 1;
break;
default:
printf("please re-enter a valid answer");
scanf(" %c", &mstatus);
}
}
答案 2 :(得分:0)
Your if
statement doesn't work as you cannot compare a variable with a range of values. Also, the switch
statement is probably overkill. If you're just looking to loop around until you get a correct answer then you could do something like this:
for(;;)
{
char mstatus;
printf("please enter a marriage status");
scanf(" %c", &mstatus);
if(mstatus == 'y' || mstatus =='Y')
{
printf("You have answer yes for married");
break;
}
else if(mstatus == 'n' || mstatus =='N')
{
printf("You have answer no for married");
break;
}
else
{
printf("please re-enter a valid answer");
}
}
答案 3 :(得分:0)
In the code you provided, you have not added a loop at all. You need to add a loop for this to work.
Also, there is no need for an if, then else statement on top of a switch statement, as mentioned by Saurav.
There are many ways of doing this. One way is :
do
{
printf("Answer (y/n)? :");
scanf(" %c", &mstatus);
repeat = 0;
switch(mstatus)
{
case 'y':
case 'Y':
printf("You have answer yes for married");
break;
case 'n':
case 'N':
printf("You have answer no for married");
break;
default:
printf("please re-enter a valid answer");
repeat = 1;
}
} while (repeat == 1);
答案 4 :(得分:0)
将逻辑包装在一个函数中,这样当你想退出循环时就可以从函数返回:
#include <stdio.h>
int yesno(const char *q)
{
char answer = '\0';
while (1) {
printf("%s? (y/n) ", q);
fflush(stdout);
scanf(" %c", &answer);
switch (answer) {
case 'N':
case 'n':
return 0;
case 'Y':
case 'y':
return 1;
default:
puts("Please answer y or n");
}
}
/* unreachable */
return -1;
}
int main(void)
{
int yes;
yes = yesno("Are you married");
printf("You answered %s\n", yes ? "yes" : "no");
return 0;
}