标题:大家帮我改个错
只看楼主
flysky102
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2007-12-26
 问题点数:0 回复次数:16 
大家帮我改个错
大家看看我这个怎么回事,写个简单的选择题,但是如果不选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;
}
搜索更多相关主题的帖子: game following include 选择题 
2008-03-16 14:36
VanHorn
Rank: 1
等 级:新手上路
帖 子:152
专家分:0
注 册:2008-1-8
得分:0 
以下是我的思路。欢迎不同意见的朋友发帖交流。
# 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]]
2008-03-16 15:25
flysky102
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2007-12-26
得分:0 
这个思路不错,可是怎么用循环实现呢?我想用循环实现的

I think I could do better.
2008-03-16 15:51
comlative
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2008-3-13
得分:0 
没错啊
一楼那位写的就是循环啊,while也是循环啊。
2008-03-16 17:16
雪城白鸟
Rank: 1
来 自:吉林
等 级:新手上路
帖 子:22
专家分:0
注 册:2008-3-15
得分:0 
没错吧
循环分为while和for循环
2008-03-16 17:43
wangxinkai
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2008-3-16
得分:0 
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;


}

}
你试试,应该成立。
2008-03-16 19:25
wangxinkai
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2008-3-16
得分:0 
((srt=='c')?0:(srt=='p'))?0:(srt=='t')?0:(srt=='q')?0:1)
写错了。。。。
拉一步
2008-03-16 19:27
wangxinkai
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2008-3-16
得分:0 
# 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;
}
2008-03-16 19:41
flysky102
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2007-12-26
得分:0 
比较喜欢8楼的程序,简洁些,谢谢了

I think I could do better.
2008-03-16 20:12
flysky102
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2007-12-26
得分:0 
while(((ch=='c')?0:(ch=='p')?0:(ch=='t')?0:(ch=='q')?0:1)); //不知道这步还有别的简洁方式吗?

I think I could do better.
2008-03-16 20:13



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-203667-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.031482 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved