C++编程中,怎么输入"Y"则继续运行下面代码?输入"N"就关闭程序

2025-02-06 12:11:21
推荐回答(2个)
回答1:

#include
#include
#include

using namespace std;

int main(int argc,char *argv[])
{
char ch;
for(int i=0;;i++)
{
cout <<"Enter:(Y-continue,N-exit):";
cin >>ch;
if(ch == 'N')
{
return 0;
}
else if(ch == 'Y')
{
continue; //换成要执行的代码
}
}

system("PAUSE");
return EXIT_SUCCESS;
}
仅供参考

回答2:

这样:
char a = 'Y';

...

do
{
...

cout<<"please input 'Y' or 'N'";
}whlie(getchar() == a);