| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1192 人关注过本帖
标题:新手编程,一个关于while循环无法跳出的问题,请高手帮忙,谢谢。
只看楼主 加入收藏
piaoyuayi
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2008-9-29
收藏
 问题点数:0 回复次数:17 
新手编程,一个关于while循环无法跳出的问题,请高手帮忙,谢谢。
#include<iostream>
#include<cctype>
using std::cin;
using std::cout;
using std::endl;
int main()
{
    int num1=0;
    int num2=0;
    double average=0;
    int yes_no=0;
    int count=0;
    while(std::tolower(yes_no)!='n')
    {
        cout<<"Enter a number please"<<endl;
             cin>>num1;
             num2+=num1;
             count++;
             cout<<"Do you want to enter anoter number"<<"("<<"y/n"<<")"<<endl;
             cin>>yes_no;
             cout<<endl;
    }
         average=num2/count;
    cout<<"The plused number is"<<num2<<endl;
    cout<<"The average number is"<<average<<endl;
    return 0;
}
        当输入了Y后进入死循环,cin语句不能正常执行非常郁闷。请帮忙解说下,我也能用其他语句解决,但是不知道为什么这样就不行。高手帮忙
搜索更多相关主题的帖子: double average include please 
2008-09-29 14:51
blueboy82006
Rank: 5Rank: 5
来 自:幻想世界
等 级:贵宾
威 望:16
帖 子:1227
专家分:57
注 册:2007-7-23
收藏
得分:0 
你tolower的用法不对啊...
它的声明为:
int tolower( int c );
而且也不在STD里啊..

其实你只要把yes_no声明为char就行了,何必那么麻烦...

2008-09-29 15:04
piaoyuayi
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2008-9-29
收藏
得分:0 
#include<iostream>
#include<cctype>
using std::cin;
using std::cout;
using std::endl;
int main()
{
    int num1=0;
    int num2=0;
    double average=0;
    int yes_no=0;
    int count=0;
    while(yes_no!='n')
    {
        cout<<"Enter a number please"<<endl;
             cin>>num1;
             num2+=num1;
             count++;
             cout<<"Do you want to enter anoter number"<<"("<<"y/n"<<")"<<endl;
             cin>>yes_no;
             cout<<endl;
    }
         average=num2/count;
    cout<<"The plused number is"<<num2<<endl;
    cout<<"The average number is"<<average<<endl;
    return 0;
}
不用tolower 也不对啊,跳不出循环
2008-09-29 15:36
piaoyuayi
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2008-9-29
收藏
得分:0 
#include<iostream>
#include<cctype>
using std::cin;
using std::cout;
using std::endl;
int main()
{
    int num1=0;
    int num2=0;
    double average=0;
    int yes_no=0;
    int count=0;
    while(yes_no!='n')
    {
        cout<<"Enter a number please"<<endl;
             cin>>num1;
             num2+=num1;
             count++;
             cout<<"Do you want to enter anoter number"<<"("<<"y/n"<<")"<<endl;
             cin>>yes_no;
             cout<<endl;
    }
         average=num2/count;
    cout<<"The plused number is"<<num2<<endl;
    cout<<"The average number is"<<average<<endl;
    return 0;
}
不用tolower 也不对啊,跳不出循环
2008-09-29 16:02
blueboy82006
Rank: 5Rank: 5
来 自:幻想世界
等 级:贵宾
威 望:16
帖 子:1227
专家分:57
注 册:2007-7-23
收藏
得分:0 
#include<iostream>
#include<cctype>
using std::cin;
using std::cout;
using std::endl;
int main()
{
    int num1=0;
    int num2=0;
    double average=0;
    char yes_no='\0';//////////char
    int count=0;
    while(yes_no!='n')
    {
        cout<<"Enter a number please"<<endl;
             cin>>num1;
             num2+=num1;
             count++;
             cout<<"Do you want to enter anoter number"<<"("<<"y/n"<<")"<<endl;
             cin>>yes_no;
             cout<<endl;
    }
         average=num2/count;
    cout<<"The plused number is"<<num2<<endl;
    cout<<"The average number is"<<average<<endl;
    return 0;
}

2008-09-29 16:11
blueboy82006
Rank: 5Rank: 5
来 自:幻想世界
等 级:贵宾
威 望:16
帖 子:1227
专家分:57
注 册:2007-7-23
收藏
得分:0 
都说要改char了....
这次要是跳不出来就是有鬼....

2008-09-29 16:12
piaoyuayi
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2008-9-29
收藏
得分:0 
为什么要改成CHAR那为什么int 就不行啊??
2008-09-29 17:35
piaoyuayi
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2008-9-29
收藏
得分:0 
能说明下吗,char和int不过是占用字节大小不同为很么会影响到循环哪??
2008-09-29 17:37
blueboy82006
Rank: 5Rank: 5
来 自:幻想世界
等 级:贵宾
威 望:16
帖 子:1227
专家分:57
注 册:2007-7-23
收藏
得分:0 
你试一下:
int a;
cin>>a;
cout<<a<<endl;
//////////////////////
你输入一个n,看看输出是什么?
另外,要知道字符的比较实际是比较ACCIS码
n的ACCIS码是110
你要是再不明白,我就没办法了...

2008-09-29 17:57
piaoyuayi
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2008-9-29
收藏
得分:0 
明白了,谢谢拉。
2008-09-29 18:05
快速回复:新手编程,一个关于while循环无法跳出的问题,请高手帮忙,谢谢。
数据加载中...
 
   



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

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