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

帮我看下,无法输出???

国爆 发布于 2011-09-28 11:21, 830 次点击
#include<iostream>
using namespace std;
class stud{
public:
    void setinformation(long x,char y);
    void showinformation();
private:
    long studnum;
    char studsex;
};
void stud::setinformation(long x,char y){
    studnum=x;
    studsex=y;
}
void stud::showinformation (){
    if(studnum=='g')
       cout<<"mike is a girl and her studnum is:"<<studnum<<endl;
    if(studnum=='b')
        cout<<"mike is a boy and his studnum is:"<<studnum<<endl;
}
int main(){
    stud mike;
    long num;
    char sex;
    cout<<"input num:"<<endl;
    cin>>num;
    cout<<"input sex:"<<endl;
    cin>>sex;
    mike.setinformation(num,sex);
    mike.showinformation();
    return 0;
}
9 回复
#2
czsbc2011-09-28 11:24
if(studnum=='g')
if(studnum=='b')
写错了吧。  
#3
can39811322011-09-28 14:08
num 定义为Long  怎么接收字符
#4
国爆2011-09-28 22:38
回复 2楼 czsbc
我也觉得那有问题,但是怎么改呢,给点建议吧
#5
国爆2011-09-28 22:40
回复 3楼 can3981132
如果我把它直接当成数字,应该也可以吧
#6
czsbc2011-09-28 22:42
if(studsex=='g')
if(studsex=='b')
真怀疑这是不是你自己写的。
#7
lanilao2011-09-28 23:07
同意楼上
#8
lkz1987n2011-09-28 23:47
void stud::showinformation (){
    if(studsex=='g')
       cout<<"mike is a girl and her studnum is:"<<studnum<<endl;
    if(studsex=='b')
        cout<<"mike is a boy and his studnum is:"<<studnum<<endl;
}
#9
guilin08222011-10-01 01:59
将下面代码中的studnum=='g'改成studsex=='g' ,studnum=='b'改成studsex=='g'
void stud::showinformation (){
    if(studnum=='g')
       cout<<"mike is a girl and her studnum is:"<<studnum<<endl;
    if(studnum=='b')
        cout<<"mike is a boy and his studnum is:"<<studnum<<endl;
}
另外,这个程序还在一种情况下不会输出,就是性别不是g或b时,所以建议对性别输出时加上判断有效性的语句。
#10
guilin08222011-10-01 02:09
别外,对性别断判断语句可以这样写,把      
        cout<<"input sex:"<<endl;
        cin>>sex;
改成
    do
    {
        cout<<"input sex:"<<endl;
        cin>>sex;
    }
    while (sex!='g'&&sex!='b');
1