一道关于 swith() 的问题 谢谢~!

ufdxkm 2009-01-31 10:45:56
#include <iostream>
const int charsize=20;
const int menber=3;

struct bop{
char fullname[charsize];
char title[charsize];
char bopname[charsize];
int preference;
};
int main()
{
using namespace std;
bop person[menber]=
{

{
"pengchang",
"go away",
"Crius.Zc",
3,
},{

"pengruze",
"come and lat's go",
"Rz",
2,
},{
"unkown",
"i don't know",
"zz",
1,
}
};

cout << "Benevolent Order of Programmers Report\n";
cout << "a. display by name\t b. dispaly by title\n"
"c. display by bopname\t d. display by preference\n"
"q. quit\n";

char ch;
cout<<"Enter you choice :";
while(cin>>ch &&ch !='q' &&ch !='Q')
{
while(!(ch>='a' && ch <='d' || ch>='A' && ch<='D'))
{
while(cin.get() !='\n')
continue;
cout<<"That is not a choice ,Choice again:";
cin>>ch;

}
switch(ch)
{
case'a':
case'A':for(int i=0;i<menber;i++)
cout<<person[i].fullname<<endl;break;
case'b':
case'B':for(int i=0;i<menber;i++)
cout<<person[i].title<<endl;break;
case'c':
case'C':for(int i=0;i<menber;i++)
cout<<person[i].bopname<<endl;break;
case'd':
case'D':for(int i=0;i<menber;i++)
{
switch(person[i].preference)
{
case 0:cout<<person[i].fullname<<endl;break;
case 1:cout<<person[i].title<<endl;break;
case 2:cout<<person[i].bopname<<endl;break;
}
}break;

}
while(cin.get()!='\n')
continue;
cout<<"next choice: ";
cin>>ch;
}
cout<<"bye~!"<<endl;


return 0;
}


就是这个 连接 http://topic.csdn.net/u/20070923/18/77cd4bf8-4a81-403a-ad60-b7ab42d869b5.html 里的例子
如果那个switch
case 'q':
case 'Q': cout << "Bye!\n"; exit(EXIT_FAILURE);
不用exit(EXIT_FAILURE); 还可以用什么办法 输入q 就可以退出整个程序
我上边的那个例子如果第一次输入(假如一个)x,会显示
That is not a choice ,Choice again:
然后再输入q,还是输出这句话
因为执行这个循环
while(!(ch>='a' && ch <='d' || ch>='A' && ch<='D'))
{
while(cin.get() !='\n')
continue;
cout<<"That is not a choice ,Choice again:";
cin>>ch;

}

所以 有什么办法可以在输入x后输入q也可以退出?


谢谢大家!


...全文
191 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
waizqfor 2009-01-31
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 eidolondedidi 的回复:]
谢谢各位拉
帮顶的就不加分拉 就20分 不好意思拉~。。。
我看的C++ Primer..PLus只是还没有见过exit()函数,而且我觉得这道题应该不是考exit这个函数的。。
所以希望下面的朋友是否可以不用exit() 作出来。。。 嘻嘻。。
其实我是想学点东西,不是只为作这道题的哈~
[/Quote]
那就用 GO TO 呵呵
ufdxkm 2009-01-31
  • 打赏
  • 举报
回复
大家讨论一下哈
ufdxkm 2009-01-31
  • 打赏
  • 举报
回复
谢谢各位拉
帮顶的就不加分拉 就20分 不好意思拉~。。。
我看的C++ Primer..PLus只是还没有见过exit()函数,而且我觉得这道题应该不是考exit这个函数的。。
所以希望下面的朋友是否可以不用exit() 作出来。。。 嘻嘻。。
其实我是想学点东西,不是只为作这道题的哈~
xiaoyisnail 2009-01-31
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 sagegz 的回复:]
帮你+了2行代码,现在可以解决输入X后再输入Q不能结束程序的问题了.
C/C++ code#include<iostream>usingnamespacestd;constintcharsize=20;constintmenber=3;structbop{charfullname[charsize];chartitle[charsize];charbopname[charsize];intpreference;
};intmain(){
bop person[menber]={{"pengchang","go away","Crius.Zc",3},{"pengruze","come and lat's go","Rz",2},{"unkown","i don't know","zz",1}};
cout…
[/Quote]

up
waizqfor 2009-01-31
  • 打赏
  • 举报
回复

while(!(ch>='a' && ch <='d' || ch>='A' && ch <='D'))
{
while(cin.get() !='\n')
continue;
cout < <"That is not a choice ,Choice again:";
cin>>ch;

if(ch=='q'||ch=='Q')//这里加个判断 是不是q然后推出程序
exit(1);

}
向良玉 2009-01-31
  • 打赏
  • 举报
回复
正确的[Quote=引用 6 楼 sagegz 的回复:]
帮你+了2行代码,现在可以解决输入X后再输入Q不能结束程序的问题了.

C/C++ code
#include <iostream>
using namespace std;

const int charsize=20;
const int menber=3;

struct bop{
char fullname[charsize];
char title[charsize];
char bopname[charsize];
int preference;
};
int main(){
bop person[menber]={{"pengchang","go away","Crius.Zc",3},{"pengruze","come and lat…
[/Quote]
向良玉 2009-01-31
  • 打赏
  • 举报
回复
正确的[Quote=引用 6 楼 sagegz 的回复:]
帮你+了2行代码,现在可以解决输入X后再输入Q不能结束程序的问题了.

C/C++ code
#include <iostream>
using namespace std;

const int charsize=20;
const int menber=3;

struct bop{
char fullname[charsize];
char title[charsize];
char bopname[charsize];
int preference;
};
int main(){
bop person[menber]={{"pengchang","go away","Crius.Zc",3},{"pengruze","come and lat…
[/Quote]
sagegz 2009-01-31
  • 打赏
  • 举报
回复
帮你+了2行代码,现在可以解决输入X后再输入Q不能结束程序的问题了.

#include <iostream>
using namespace std;

const int charsize=20;
const int menber=3;

struct bop{
char fullname[charsize];
char title[charsize];
char bopname[charsize];
int preference;
};
int main(){
bop person[menber]={{"pengchang","go away","Crius.Zc",3},{"pengruze","come and lat's go","Rz",2},{"unkown","i don't know","zz",1}};
cout << "Benevolent Order of Programmers Report\n";
cout << "a. display by name\t b. dispaly by title\n"
"c. display by bopname\t d. display by preference\n"
"q. quit\n";
char ch;
cout<<"Enter you choice :";
cin>>ch;
while(ch !='q' && ch !='Q'){
while(!(ch>='a' && ch <='d' || ch>='A' && ch<='D')){
while(cin.get() !='\n')
continue;
cout<<"That is not a choice ,Choice again:";
cin>>ch;
if(ch=='q' || ch== 'Q') //这里加2行
exit(-1);
}
switch(ch){
case'a':
case'A':for(int i=0;i<menber;i++)
cout<<person[i].fullname<<endl;break;
case'b':
case'B':for(int i=0;i<menber;i++)
cout<<person[i].title<<endl;break;
case'c':
case'C':for(int i=0;i<menber;i++)
cout<<person[i].bopname<<endl;break;
case'd':
case'D':for(int i=0;i<menber;i++)
{
switch(person[i].preference)
{
case 0:cout<<person[i].fullname<<endl;break;
case 1:cout<<person[i].title<<endl;break;
case 2:cout<<person[i].bopname<<endl;break;
}
}break;
}
while(cin.get()!='\n')
continue;
cout<<"next choice: ";
cin>>ch;
}
cout<<"bye~!"<<endl;
return 0;
}
ufdxkm 2009-01-31
  • 打赏
  • 举报
回复
加break 调处? 还是跳出一层循环阿。
还是执行那个循环。。
barbara2008 2009-01-31
  • 打赏
  • 举报
回复
强制return和你开玩笑的,呵呵
ufdxkm 2009-01-31
  • 打赏
  • 举报
回复
强制return
最后那句 bye~! 不能输出了阿
barbara2008 2009-01-31
  • 打赏
  • 举报
回复
while(!(ch>='a' && ch <='d' || ch>='A' && ch<='D'))
{
while(cin.get() !='\n')
continue;
cout<<"That is not a choice ,Choice again:";
cin>>ch;
//这里加个判断,跳出或者饭返回

}
barbara2008 2009-01-31
  • 打赏
  • 举报
回复
直接强制return
tankinc 2009-01-31
  • 打赏
  • 举报
回复
if(ch=='q'||ch=='Q')//这里加个判断 是不是q然后推出程序
exit(1);
这是最省力的方法。
xuruichen 2009-01-31
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 waizqfor 的回复:]
C/C++ code
while(!(ch>='a' && ch <='d' || ch>='A' && ch <='D'))
{
while(cin.get() !='\n')
continue;
cout < <"That is not a choice ,Choice again:";
cin>>ch;

if(ch=='q'||ch=='Q')//这里加个判断 是不是q然后推出程序
exit(1);

}
[/Quote]
correct
9527- 2009-01-31
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;

const int charsize=20;
const int menber=3;

struct bop{
char fullname[charsize];
char title[charsize];
char bopname[charsize];
int preference;
};
int main(){
bop person[menber]={{"pengchang","go away","Crius.Zc",3},{"pengruze","come and lat's go","Rz",2},{"unkown","i don't know","zz",1}};
cout << "Benevolent Order of Programmers Report\n";
cout << "a. display by name\t b. dispaly by title\n"
"c. display by bopname\t d. display by preference\n"
"q. quit\n";
char ch;
cout<<"Enter you choice :";
cin>>ch;
while(ch !='q' && ch !='Q'){
while(!(ch>='a' && ch <='d' || ch>='A' && ch<='D')){
while(cin.get() !='\n')
continue;
cout<<"That is not a choice ,Choice again:";
cin>>ch;
if(ch=='q' || ch== 'Q') //这里加2行
return( 0 ); //exit(-1);//这样做是你要的效果吗
}
switch(ch){
case'a':
case'A':for(int i=0;i<menber;i++)
cout<<person[i].fullname<<endl;break;
case'b':
case'B':for(int i=0;i<menber;i++)
cout<<person[i].title<<endl;break;
case'c':
case'C':for(int i=0;i<menber;i++)
cout<<person[i].bopname<<endl;break;
case'd':
case'D':for(int i=0;i<menber;i++)
{
switch(person[i].preference)
{
case 0:cout<<person[i].fullname<<endl;break;
case 1:cout<<person[i].title<<endl;break;
case 2:cout<<person[i].bopname<<endl;break;
}
}break;
}
while(cin.get()!='\n')
continue;
cout<<"next choice: ";
cin>>ch;
}
cout<<"bye~!"<<endl;
return 0;
}
sagegz 2009-01-31
  • 打赏
  • 举报
回复
现在看来也只能用goto了,不过我只会汇编里的JMP
等待牛人了.

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧