下面程序的if语句转switch语句问题
if语句代码:#include"iostream.h"
void main()
{
char c;
while(true)
{
cout<<"MAIN MENU:"<<endl;
cout<<"l: left,r:right,q:quit ->";
cin>>c;
if(c == 'q')
break;
if(c=='l')
{
cout<<"LEFT MENU:"<<endl;
cout<<"select c or d:";
cin>>c;
if(c=='c')
{
cout<<"you chose 'c'"<<endl;
continue;
}
if(c=='d')
{
cout<<"you chose 'd'"<<endl;
continue;
}
else
{
cout<<"you didn't choose c or d!"<<endl;
continue;
}
}
if(c=='r')
{
cout<<"RIGHT MENU:"<<endl;
cout<<"select a or b:";
cin>>c;
if(c=='a')
{
cout<<"you chose 'a'"<<endl;
continue;
}
if(c=='b')
{
cout<<"you chose 'b'"<<endl;
continue;
}
else
{
cout<<"you didn't choose c or d!"<<endl;
continue;
}
}
cout<<"you must type l or r or q!"<<endl;
}
cout<<"quitting menu..."<<endl;
}
我转换的switch语句代码:
#include<iostream.h>
void main()
{
char c;
while(true)
{
cout<<"MAIN MENU:"<<endl;
cout<<"l:left, r:right, q:quit ->";
cin>>c;
switch(c)
{
case'q':break;
case'l':cout<<"LEFT MUNU:"<<endl;
cout<<"selesct c or d:";
cin>>c;
switch(c)
{
case'c':cout<<"you chose'c'"<<endl;continue;
case'd':cout<<"you chose'd'"<<endl;continue;
default:cout<<"you didn't choose c or d:"<<endl;continue;
}
case'r':cout<<"RIGHT MUNU:"<<endl;
cout<<"select a or b:";
cin>>c;
switch(c)
{
case'a':cout<<"you chose 'a'"<<endl;continue;
case'b':cout<<"you chose 'b'"<<endl;continue;
default:cout<<"you didn't choose c or d!"<<endl;continue;
}
default:cout<<"you must type l or r or q!"<<endl;
}
cout<<"quitting menu..."<<endl;
}
}
主要问题是输入q时,不能退出菜单quitting menu..
麻烦告诉我错在哪里,顺利帮我修正一下。
而且问题不止这一个,谁能帮我编个正确的,万分感谢。
[ 本帖最后由 拉C 于 2010-6-2 14:50 编辑 ]