| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 526 人关注过本帖
标题:[讨论]这个程序输出的结果不正确
只看楼主 加入收藏
askCpp
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2007-9-23
收藏
 问题点数:0 回复次数:9 
[讨论]这个程序输出的结果不正确


大家看看这个程序,为什么输出的结果一直都在跳,就是重复输出,晃的我眼花

#include <iostream>
using namespace std;

int main()
{
const int size=5;
const int strsize=20;
int i;
char ch;
struct bop
{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};
bop array[size]=
{
{"pan san","master","handsome man",3},
{"ren gui shan","baogong tou","lucky boy",0},
{"chengqian","teacher","lao ma",3},
{"xiong pi","rubblish","trash",2},
{"noman","notitle","no bop name",1}
};
cout<<"Benevolent order of programmers report\n";
cout<<"a. display by name\tb. display by title\n";
cout<<"c. display by bopname\td. display by preference\n";
cout<<"q. quit\n";

cout<<"Enter your choice:";
cin>>ch;
while(ch!='q')
{
while(ch=='a' || ch=='b' || ch=='c' || ch=='d')
{
if(ch=='d')
{
for(i=0;i<size;i++)
{
switch(array[i].preference)
{
case 0:cout<<array[i].fullname<<endl;break;
case 1:cout<<array[i].title<<endl;break;
case 2:cout<<array[i].bopname<<endl;break;
}
}
}
else
{
for(i=0;i<size;i++)
{
switch(ch)
{
case 'a':cout<<array[i].fullname<<endl;break;
case 'b':cout<<array[i].title<<endl;break;
case 'c':cout<<array[i].bopname<<endl;break;
}
}
}
}
cout<<"Next choice:";
cin>>ch;
}
cout<<"Bye!\n";
return 0;
}

[此贴子已经被作者于2007-9-23 17:35:56编辑过]

搜索更多相关主题的帖子: 结果 输出 
2007-09-23 14:52
askCpp
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2007-9-23
收藏
得分:0 

请你们仔细看一下,很简单的一个程序.
array[size]是一个bop结构数组
程序的意思是:当ch分别等于a,b,c时分别输出array结构里面的fullname,title,bopname属性,当ch等于d时,判断array结构里面的preference属性等于0或者1还是2,是0输出fullname,是1输出title,是2输出bopname

我就是搞不懂问什么编译成功后反复输出结果

2007-09-23 17:33
askCpp
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2007-9-23
收藏
得分:0 
你们把程序编译下看,就知道我的意思了
2007-09-23 17:38
wuhaofei
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-9-14
收藏
得分:0 

因为这两个循环都是死循环,楼主的程序没控制好,循环的条件
#include <iostream>
using namespace std;

int main()
{
const int size=5;
const int strsize=20;
int i;
char ch;
struct bop
{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};
bop array[size]=
{
{"pan san","master","handsome man",3},
{"ren gui shan","baogong tou","lucky boy",0},
{"chengqian","teacher","lao ma",3},
{"xiong pi","rubblish","trash",2},
{"noman","notitle","no bop name",1}
};
cout<<"Benevolent order of programmers report\n";
cout<<"a. display by name\tb. display by title\n";
cout<<"c. display by bopname\td. display by preference\n";
cout<<"q. quit\n";

cout<<"Enter your choice:";
cin>>ch;
while(ch!='q')
{
while(ch=='a' || ch=='b' || ch=='c' || ch=='d')
{
if(ch=='d')
{
for(i=0;i<size;i++)
{
switch(array[i].preference)
{
case 0:cout<<array[i].fullname<<endl;break;
case 1:cout<<array[i].title<<endl;break;
case 2:cout<<array[i].bopname<<endl;break;
}
}
}
else
{
for(i=0;i<size;i++)
{
switch(ch)
{
case 'a':cout<<array[i].fullname<<endl;break;
case 'b':cout<<array[i].title<<endl;break;
case 'c':cout<<array[i].bopname<<endl;break;
}
}
break;//跳出内层循环
}
break;//跳出外层循环
}
cout<<"Next choice:";
cin>>ch;
}
cout<<"Bye!\n";
return 0;
}

2007-09-23 18:43
askCpp
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2007-9-23
收藏
得分:0 

楼上的兄台你好
加上break,程序的结果还不是我想要的那样,是不是我程序有错误?
还有怎么看出来的死循环?

2007-09-23 19:02
askCpp
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2007-9-23
收藏
得分:0 

好了,现在问题搞定了
wuhaofei,只需要加一个break,是为了退出第二个while循环
谢谢你的提醒
正确的程序如下:
#include <iostream>
using namespace std;

int main()
{
/******************ch6-4******************/
const int size=5;
const int strsize=20;
int i;
char ch;
struct bop
{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};
bop array[size]=
{
{"pan san","master","handsome man",1},
{"ren gui shan","baogong tou","lucky boy",0},
{"chengqian","teacher","lao ma",1},
{"xiong pi","rubblish","trash",2},
{"noman","notitle","no bop name",1}
};
cout<<"Benevolent order of programmers report\n";
cout<<"a. display by name\tb. display by title\n";
cout<<"c. display by bopname\td. display by preference\n";
cout<<"q. quit\n";

cout<<"Enter your choice:";
cin>>ch;
while(ch!='q')
{
while(ch=='a' || ch=='b' || ch=='c' || ch=='d')
{
if(ch=='d')
{
for(i=0;i<size;i++)
{
switch(array[i].preference)
{
case 0:cout<<array[i].fullname<<endl;break;
case 1:cout<<array[i].title<<endl;break;
case 2:cout<<array[i].bopname<<endl;break;
}
}
}
else
{
for(i=0;i<size;i++)
{
switch(ch)
{
case 'a':cout<<array[i].fullname<<endl;break;
case 'b':cout<<array[i].title<<endl;break;
case 'c':cout<<array[i].bopname<<endl;break;
}
}
}
break;
}
cout<<"Next choice:";
cin>>ch;
}
cout<<"Bye!\n";
return 0;
}

2007-09-23 19:40
magicypj
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2007-9-15
收藏
得分:0 

你也可以把cout<<"Next choice:";cin>>ch;加到第二个while循环里

while(ch=='a' || ch=='b' || ch=='c' || ch=='d')
{
if(ch=='d')
{
for(i=0;i<size;i++)
{
switch(array[i].preference)
{
case 0:cout<<array[i].fullname<<endl;break;
case 1:cout<<array[i].title<<endl;break;
case 2:cout<<array[i].bopname<<endl;break;
}
}
}
else

{
for(i=0;i<size;i++)
{
switch(ch)
{
case 'a':cout<<array[i].fullname<<endl;break;
case 'b':cout<<array[i].title<<endl;break;
case 'c':cout<<array[i].bopname<<endl;break;
}
}
}
// break;
cout<<"Next choice:";
cin>>ch;

}

2007-09-23 20:57
china25qd
Rank: 1
等 级:新手上路
帖 子:161
专家分:0
注 册:2007-9-6
收藏
得分:0 

#include <iostream>
using namespace std;

int main()
{
const int size=5;
const int strsize=20;
int i;
char ch;
struct bop
{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};
bop array[size]=
{
{"pan san","master","handsome man",3},
{"ren gui shan","baogong tou","lucky boy",0},
{"chengqian","teacher","lao ma",3},
{"xiong pi","rubblish","trash",2},
{"noman","notitle","no bop name",1}
};
cout<<"Benevolent order of programmers report\n";
cout<<"a. display by name\tb. display by title\n";
cout<<"c. display by bopname\td. display by preference\n";
cout<<"q. quit\n";

cout<<"Enter your choice:";
cin>>ch;
try {
while(ch=='a' || ch=='b' || ch=='c' || ch=='d')
{
if(ch=='d')
{
for(i=0;i<size;i++)
{
switch(array[i].preference)
{
case 0:cout<<array[i].fullname<<endl;break;
case 1:cout<<array[i].title<<endl;break;
case 2:cout<<array[i].bopname<<endl;break;
}
}
}
else
{
for(i=0;i<size;i++)
{
switch(ch)
{
case 'a':cout<<array[i].fullname<<endl;break;
case 'b':cout<<array[i].title<<endl;break;
case 'c':cout<<array[i].bopname<<endl;break;
}
}
}
if (ch != 'q')
throw "Next choice:";
}
}

catch (char *msg)
{
cout << msg ;
cin>>ch;
}
cout<<"Bye!\n";
return 0;
}
还差一个循环,今天来不及做了,明天有机会补上

[此贴子已经被作者于2007-9-23 21:51:56编辑过]


抱膝怀天下 闲坐观四海
2007-09-23 21:50
wuhaofei
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-9-14
收藏
得分:0 
回复:(askCpp)楼上的兄台你好加上break,程序的结果...

怎么看出死循环?这个问题还是自己看看c++的书籍吧,最好自己能发现和解决问题

2007-09-24 21:52
PcrazyC
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:5652
专家分:0
注 册:2006-10-20
收藏
得分:0 
#include <iostream>
using namespace std;

int main()
{
const int size=5;
const int strsize=20;
int i;
char ch;
struct bop
{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};
bop array[size]=
{
{"pan san","master","handsome man",3},
{"ren gui shan","baogong tou","lucky boy",0},
{"chengqian","teacher","lao ma",3},
{"xiong pi","rubblish","trash",2},
{"noman","notitle","no bop name",1}
};
cout<<"Benevolent order of programmers report\n";
cout<<"a. display by name\tb. display by title\n";
cout<<"c. display by bopname\td. display by preference\n";
cout<<"q. quit\n";

cout<<"Enter your choice:";
cin>>ch;
while(ch!='q')
{
while(ch=='a' || ch=='b' || ch=='c' || ch=='d') //这个while是个死循环,里面没有改变CH变量
{
if(ch=='d')
{
for(i=0;i<size;i++)
{
switch(array[i].preference)
{
case 0:cout<<array[i].fullname<<endl;break;
case 1:cout<<array[i].title<<endl;break;
case 2:cout<<array[i].bopname<<endl;break;
}
}
}
else
{
for(i=0;i<size;i++)
{
switch(ch)
{
case 'a':cout<<array[i].fullname<<endl;break;
case 'b':cout<<array[i].title<<endl;break;
case 'c':cout<<array[i].bopname<<endl;break;
}
}
}
}
cout<<"Next choice:";
cin>>ch;
}
cout<<"Bye!\n";
return 0;
}

我想你的意思应该是这样

#include <iostream>
using namespace std;

int main()
{
const int size=5;
const int strsize=20;
int i;
char ch;
struct bop
{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};
bop array[size]=
{
{"pan san","master","handsome man",3},
{"ren gui shan","baogong tou","lucky boy",0},
{"chengqian","teacher","lao ma",3},
{"xiong pi","rubblish","trash",2},
{"noman","notitle","no bop name",1}
};
cout<<"Benevolent order of programmers report\n";
cout<<"a. display by name\tb. display by title\n";
cout<<"c. display by bopname\td. display by preference\n";
cout<<"q. quit\n";

cout<<"Enter your choice:";
cin>>ch;
while(ch!='q')
{
if(ch=='a' || ch=='b' || ch=='c' || ch=='d') //这个while换成if
{
if(ch=='d')
{
for(i=0;i<size;i++)
{
switch(array[i].preference)
{
case 0:cout<<array[i].fullname<<endl;break;
case 1:cout<<array[i].title<<endl;break;
case 2:cout<<array[i].bopname<<endl;break;
}
}
}
else
{
for(i=0;i<size;i++)
{
switch(ch)
{
case 'a':cout<<array[i].fullname<<endl;break;
case 'b':cout<<array[i].title<<endl;break;
case 'c':cout<<array[i].bopname<<endl;break;
}
}
}
}
cout<<"Next choice:";
cin>>ch;
}
cout<<"Bye!\n";
return 0;
}


雁无留踪之意,水无取影之心
2007-09-25 00:30
快速回复:[讨论]这个程序输出的结果不正确
数据加载中...
 
   



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

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