c++ (y/n)

来源:百度知道 编辑:UC知道 时间:2024/07/04 16:52:22
我想输入y执行一个操作,输入n执行另一个操作,而输入其他值时不执行任何操作,请问该如何实现?

char ch;
while(cin>>ch)
{
switch(ch)
{
case 'y':
...;
break;
case 'n':
...;
break;
default:
//这里是其他值的处理,你想干嘛都行
}
}

char ch;
print("please input the value:");
ch=getchar();
while(ch)
{
switch(ch)
{
case'y'
执行……
break;
case 'n'
执行……
break;
defult;
}
}