读书人

C++新手求教还望各位老师多多指点!

发布时间: 2012-09-10 22:20:12 作者: rapoo

C++新手求教,还望各位老师多多指点!
char the_Exit;
cout << "Are you sure eixt? y/n";
cin >> the_Exit;
if (the_Exit == n)
break;
else
continue;
我想通过这段程序来实现退出功能。为什么会报错呢?如何才能完成输入y/n退出呢?

[解决办法]

C/C++ code
#include <iostream>using namespace std;int main(){    char the_Exit;        char y = 'n';    while (true)    {        cout << "Are you sure eixt? y/n  \n";        cin >> the_Exit;        if (the_Exit == y)            break;        else            continue;    }}
[解决办法]
#include <iostream>

using namespace std;

int main()
{


char the_Exit;

char y = 'n';
while (true)
{
cout << "Are you sure eixt? y/n \n";
cin >> the_Exit;

if (the_Exit == y)
continue;
else
break;
}

}
[解决办法]
你的变量类型好乱啊,编译器没有报错?再设一个整型变量吧。还有按你写的,continue语句也没什么意义,不用写。下面是我写的,Visual c++ 6.0 通过了,楼主参考一下。
#include <iostream>
int main()
{
using namespace std;
char the_Exit;
char y,n;
int Exit;
while (true)
{
cout << "Are you sure eixt? y/n"<<endl;
cin >> the_Exit;

if (the_Exit=='y')
Exit=0;
else
Exit = 1;

if (Exit==0)
break;
}
return 0;
}

最好用下面这个,很简洁
#include <iostream>
using namespace std;
int main()
{
char the_Exit;
char y,n;
while (true)
{
cout << "Are you sure eixt? y/n"<<endl;
cin >> the_Exit;

if (the_Exit=='y')
break;
}
return 0;
}

读书人网 >C++

热点推荐