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

帮忙下,数组的问题!!!

yjg19841211 发布于 2010-07-28 22:14, 676 次点击
#include<iostream>
#include<cstring>
const int limit=10;
int shur(int arr[],int size);
double avg(int arr[],int size);
void show(int arr[],int size);
int main()
{
    using namespace std;
    int golf[limit];
    int n;
    n=shur(golf,limit);
    double a;
    a=avg(golf,n);
    show(golf,n);
    cout<<n<<endl;
    cout<<a<<endl;
    return 0;
}
int shur(int arr[],int size)
{
    using namespace std;
    int temp;int i=0;
    cin>>temp;
    while(i<size&&isdigit(temp))
    {
        cout<<"enter......\n";
        arr[i]=temp;
        i++;
        cin>>temp;
   
    }
    return i;
   
}
double avg(int arr[],int size)
{
    int sum=0;
    for(int i=0;i<size;i++)
    {
        sum+=arr[i];
    }
    return sum/size;
}
void show(int arr[],int size)
{
    using namespace std;
    for(int j=0;j<size;j++)
    {
        cout<<arr[j]<<endl;
    }
}
4 回复
#2
pangding2010-07-28 23:45
问题是什么?
#3
JINyuanbao2010-07-29 11:10
你这个isdigit()用错了,它返回的是int型,而不是bool型
所以你while条件永远是假的。
#4
yjg198412112010-07-29 18:08
isdigit()如果括号里面的是数字的话不是会返回TURE吗?
这本来是道题目
输入10个成绩,读到一个数组中,到我这样写运行有误,不能提早结束输入
#5
zgxyz20082010-07-31 10:23
isdigit()函数是说当你输入的数的值为0—9返回非0值,否则返回0,而isdigit(int c)中c应该是对应的ASCII码,0—9的ASCII码为48—57,只有当你输入的temp在这个范围内,才会返回真,换句话说当temp分数不在48—57程序会退出while循环,所以isdigit无法实现你想要的功能。在avg函数中size你并没有判断是否为零,shur函数中有可能会返回零,分母为零会发生错误....
1