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

关于函数的使用问题

情义控灬浮云 发布于 2018-08-01 17:46, 1400 次点击
编写一个程序,不断要求用户输入两个数,直到其中一个为0.对于每两个数,计算其调和平均数返回给main()。
#include<iostream>
double fun(int a,int b);
int main()
{
    using namespace std;
    int x,y;
    cout<<"please enter two number:\n";
    while(cin>>x!=0&&cin>>y!=0)
        cout<<fun(x,y)<<endl;
    system("pause");
    return 0;
}
double fun(int a,int b)
{
    double ave;
    ave=(double)(2/(1/a+1/b));
    return(ave);
}
    请问坛友们我这个哪边有问题啊,一次都运行不了,编译成功了虽然。
1 回复
#2
rjsp2018-08-01 19:25
while(cin>>x!=0&&cin>>y!=0)
应该是
while( (cin>>x,x!=0) && (cin>>y,y!=0) )

1/a+1/b
应该是
1.0/a + 1.0/b
1