| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1527 人关注过本帖
标题:元老们,帮小雏鸟看一哈哪里错了
只看楼主 加入收藏
情义控灬浮云
Rank: 1
等 级:新手上路
帖 子:61
专家分:0
注 册:2017-8-3
结帖率:80.95%
收藏
 问题点数:0 回复次数:4 
元老们,帮小雏鸟看一哈哪里错了
编写一个程序,程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于平均值
#include<iostream>
#include<cctype>
#include<array>
using namespace std;
int main()
{
    array<double,60>arr;
    int i=0;
    char ch;
    double sum,average;
    for(i=0;i<60;i++)
    {
        cin>>ch;
      if(isdigit(ch))
         { arr[i]=ch;
          sum=+ch;
      }
      else
          break;
    }
    average=sum/i;
    while(i>=0)
        if(arr[i]>average)
            cout<<arr[i--]<<endl;
    cout<<average<<endl;
    return 0;
}
我输入几个数字和一个字母,她没反应
搜索更多相关主题的帖子: 数字 输入 include sum average 
2018-07-29 10:48
Jonny0201
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:52
帖 子:488
专家分:2603
注 册:2016-11-7
收藏
得分:0 
int isdigit( int ch );
Checks if the given character is a numeric character (0123456789).
The behavior is undefined if the value of ch is not representable as unsigned char and is not equal to EOF.
2018-07-29 20:26
Jonny0201
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:52
帖 子:488
专家分:2603
注 册:2016-11-7
收藏
得分:0 
刚看太快了, 没怎么仔细看, 问题不在那个 isdigit
你的最后一个 while 写的有问题
我重新写了一下
程序代码:
#include <iostream>
#include <vector>
#include <numeric>

using namespace std;
int main(int argc, char *argv[]) {
    vector<double> arr;
    double num;
    while(cin >> num) {
        arr.push_back(num);
    }
    double average = accumulate(arr.cbegin(), arr.cend(), 0) / arr.size();
    for(auto &c : arr) {
        if(c > average) {
            cout << c << endl;
        }
    }
    cout << average << endl;
}


[此贴子已经被作者于2018-7-29 21:01编辑过]

2018-07-29 20:54
情义控灬浮云
Rank: 1
等 级:新手上路
帖 子:61
专家分:0
注 册:2017-8-3
收藏
得分:0 
回复 3楼 Jonny0201
我的while哪里错了啊  运行到那的时候 i应该是在输入末尾的,然后递减啊
2018-07-29 21:58
Jonny0201
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:52
帖 子:488
专家分:2603
注 册:2016-11-7
收藏
得分:0 
回复 4楼 情义控灬浮云
如果 if 没进去 i 就递增不了, 就会一直卡在这里
2018-07-30 01:51
快速回复:元老们,帮小雏鸟看一哈哪里错了
数据加载中...
 
   



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

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