回 帖 发 新 帖 刷新版面

主题:简单的程序调试不对 ,请指教

一道简单的课后习题,程序如下
//ex5-4
//function returns larger of two distances

#include<iostream>
using namespace std;


//English distance
struct Distance{
    int feet;
    int inches;
};


///////////////////////////////////////////////////////////////////////////////////
//function declaration
Distance compdis(Distance,Distance);
void engdis(Distance);
void iofunc(Distance);

int main(){
    Distance d1,d2,d3;

    iofunc(d1);
    iofunc(d2);

    d3=compdis(d1,d2);
    cout<<"The largest is :"<<engdis(d3)<<endl;

    return 0;
}


//---------------------------------------------------------------------------------
//compdis()
//compares two structures of type Distance,return the larger
Distance compdis(Distance dd1,Distance dd2){
    if((dd1.feet=dd2.feet)&(dd1.inches=dd2.inches)){
        cout<<"the numbers you enter is the same !";
    }else if(dd1.feet>dd2.feet){
        return dd1;
    }else if(dd1.feet<dd2.feet){
        return dd2;
    }else if(dd1.inches>dd2.inches){
        return dd1;
    }else 
        return dd2;
}
//-----------------------------------------------------------------------------------
//engdis()
//display structure of type Distance in feet and inches
void engdis(Distance dd){
    cout<<dd.feet<<"\'"<<dd.inches<<"\""<<endl;
}

//-----------------------------------------------------------------------------------
//iofunc()
//supply the user a interface of input and output
void iofunc(Distance dd){
    cout<<"Please enter feet :"<<endl;
    cin>>dd.feet;
    cout<<"Please enter inches :"<<endl;
    cin>>dd.inches;
}

调试后显示exp5-4.cpp(28) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'void' (or there is no acceptable conversion)  求教 !

还有个问题:
  cout<<"The largest is :"<<engdis(d3)<<endl;这句用cout<<"The largest is :"<<d3.feet<<"\'"<<d3.inches<<endl;替换也就是不用engdis函数,可以否?我调试后数值不对。

谢谢大家了 !







回复列表 (共1个回复)

沙发


A:
void iofunc(Distance dd)
这个函数有问题。形参问题,改这样就OK了。
void iofunc(Distance *dd)
{
    cout<<"Please enter feet :"<<endl;
    cin>>(*dd).feet;
    cout<<"Please enter inches :"<<endl;
    cin>>(*dd).inches;
}
你输入的那些值出了这个函数就无效了,变量生命的问题。

我来回复

您尚未登录,请登录后再回复。点此登录或注册