注册 登录
编程论坛 C++教室

大家帮我改个错

flysky102 发布于 2008-03-16 14:36, 1509 次点击
大家看看我这个怎么回事,写个简单的选择题,但是如果不选c p t q的话程序要一直提示重新输入,但是我写不出来,试过加循环,但是不知道怎么加好,哪位高手能告诉我怎么加个循环
# include <iostream>
int main()
{using namespace std;
 cout<<"Please enter one of the following choices:";
 cout<<"c)carnivore\t"<<"p)paianist"
     <<"t)tree\t"<<"q)game";
 char ch;
 cout<<"please enter  c,p,t or q:";

 cin>>ch;


 switch(ch)
 { case 'c' : cout<<"A maple is carnivore\t";
    break;
   case 'p' : cout<<"A maple is paianist\v";
    break;
   case 't' : cout<<"A maple is tree\t";
    break;
   case 'q' : cout<<"A maple is game\v";
    break;
   default : cout<<"please enter a c,p,t or q:";


}
  system("pause");
  return 0;
}
16 回复
#2
VanHorn2008-03-16 15:25
以下是我的思路。欢迎不同意见的朋友发帖交流。
# include <iostream>
int main()
{
    using namespace std;
    
    int XunHuan=1;
    char ch;

    while(XunHuan)
    {
        
        cout<<"Please enter one of the following choices:";
        cout<<"c)carnivore\t"<<"p)paianist"
            <<"t)tree\t"<<"q)game";
    
        cout<<"please enter  c,p,t or q:";
        
        cin>>ch;
        
        if(ch=='c'||ch=='p'||ch=='t'||ch=='q')
        {
            XunHuan=0;
        }
        else
        {
            XunHuan=1;
            cout<<"You Have Made A Wrong Choice."<<endl;
        }
        
    }
    
    switch(ch)
    { case 'c' : cout<<"A maple is carnivore\t";
    break;
    case 'p' : cout<<"A maple is paianist\v";
        break;
    case 't' : cout<<"A maple is tree\t";
        break;
    case 'q' : cout<<"A maple is game\v";
        break;
    default : cout<<"please enter a c,p,t or q:";
        
        
    }
    
    return 0;
}

[[it] 本帖最后由 VanHorn 于 2008-3-16 15:30 编辑 [/it]]
#3
flysky1022008-03-16 15:51
这个思路不错,可是怎么用循环实现呢?我想用循环实现的
#4
comlative2008-03-16 17:16
没错啊
一楼那位写的就是循环啊,while也是循环啊。
#5
雪城白鸟2008-03-16 17:43
没错吧
循环分为while和for循环
#6
wangxinkai2008-03-16 19:25
char str;
cin>> str;
  cin>>str;
while((srt=='c')?0:(srt=='p'))?0:(srt=='t')?0:(srt=='q'))
{
  
switch(str)
{ case 'c' : cout<<"A maple is carnivore\t";
    break;
   case 'p' : cout<<"A maple is paianist\v";
    break;
   case 't' : cout<<"A maple is tree\t";
    break;
   case 'q' : cout<<"A maple is game\v";
    break;
   default : cout<<"please enter a c,p,t or q:";
    cin>>str;


}

}
你试试,应该成立。
#7
wangxinkai2008-03-16 19:27
((srt=='c')?0:(srt=='p'))?0:(srt=='t')?0:(srt=='q')?0:1)
写错了。。。。
拉一步
#8
wangxinkai2008-03-16 19:41
# include <iostream>
int main()
{using namespace std;
cout<<"Please enter one of the following choices:";
cout<<"c)carnivore\t"<<"p)paianist"
     <<"t)tree\t"<<"q)game";
char ch;
cout<<"please enter  c,p,t or q:";

    do
    {
         cin>>ch;
        switch(ch)
        { case 'c' : cout<<"A maple is carnivore\t";
            break;
           case 'p' : cout<<"A maple is paianist\v";
            break;
           case 't' : cout<<"A maple is tree\t";
            break;
           case 'q' : cout<<"A maple is game\v";
            break;
           default : cout<<"please enter a c,p,t or q:";
            
            
        }
    }
while(((ch=='c')?0:(ch=='p')?0:(ch=='t')?0:(ch=='q')?0:1));


  return 0;
}
#9
flysky1022008-03-16 20:12
比较喜欢8楼的程序,简洁些,谢谢了
#10
flysky1022008-03-16 20:13
while(((ch=='c')?0:(ch=='p')?0:(ch=='t')?0:(ch=='q')?0:1)); //不知道这步还有别的简洁方式吗?
#11
wangxinkai2008-03-16 21:45
好像没有什么比三元更简单的表达式了吧。。。。
#12
jsl20072008-03-17 09:18
# include <iostream>
int main()
{
    using namespace std;
cout<<"Please enter one of the following choices:";
cout<<"c)carnivore\t"<<"p)paianist"
     <<"t)tree\t"<<"q)game";
char c;
cout<<"please enter  c,p,t or q:";

cin>>c;

while((c!='c')||(c!='p')||(c!='t')||(c!='q')){
switch(c)
{ case 'c' : cout<<"A maple is carnivore\t";
    break;
   case 'p' : cout<<"A maple is paianist\v";
    break;
   case 't' : cout<<"A maple is tree\t";
    break;
   case 'q' : cout<<"A maple is game\v";
    break;
   default : cout<<"please enter a c,p,t or q:";


}
cin>>c;
}
  system("pause");
  return 0;
}
#13
tyboy232008-03-17 10:44
支持8楼和12楼的兄弟
我喜欢`a<b?0:1;这种用法
#14
wangxinkai2008-03-17 11:12
12楼的少输出一次。。
#15
q1077580892008-03-17 13:51
# include <iostream>
int main()
{using namespace std;
cout<<"Please enter one of the following choices:\n";
cout<<"c)carnivore\t"<<"p)paianist\n"
     <<"t)tree\t"<<"q)game"<<endl;
char ch;
int i;
i=1;
while(i)
    {cout<<"please enter  c,p,t or q:";

cin>>ch;


    switch(ch)
    { case 'c' : cout<<"A maple is carnivore\t";i=0;
    break;
   case 'p' : cout<<"A maple is paianist\v";i=0;
    break;
   case 't' : cout<<"A maple is tree\t";i=0;
    break;
   case 'q' : cout<<"A maple is game\v";i=0;
    break;
   default :;


        }
}
  system("pause");
  return 0;
}

欢迎大家加入 c++讨论群 群号:38332730
#16
flysky1022008-03-17 21:13
while(i) //为什么这么写?循环结束的条件是什么呢?
#17
q1077580892008-03-18 17:42
while(i)  构成一个死循环,  结束条件是i=0
1