首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

帮忙看个程序....,该怎么处理

2012-02-07 
帮忙看个程序....调试正确运行总出点问题................#include iostream.h#include string.hclass

帮忙看个程序....
调试正确     运行总出点问题................

#include <iostream.h>
#include <string.h>
class   CPerson
{
public:
void   SetData(char   *name,char   *id,bool   isman=1)
{
int   n=strlen(name);
strncpy(pName,name,n);
n=strlen(id);
strncpy(pID,id,n);
bMan=isman;
}
void   Output()
{                                                                                          
cout < < "姓名: " < <pName < <endl;
cout < < "编号: " < <pID < <endl;
char   *str=bMan? "男 ": "女 ";
cout < < "性别: " < <str < <endl;
}
private:
char   pName[20];
char   pID[20];
bool   bMan;
};
class   CStudent:public   CPerson
{
public:
void   SetPersonData(char   *name,char   *id,bool   isman=1)
{
SetData(name,id,isman);
}
void   InputScore(double   score1,double   score2,double   score3)
{
dbScore[1]=score1;
dbScore[2]=score2;
dbScore[3]=score3;
}
void   Print()
{
Output();
for(int   i=0;i <3;i++)
cout < < "成绩 " < <i+1 < < ": " < <dbScore[i] < <endl;
}
private:
double   dbScore[3];
};
void   main()
{
CStudent   stu;
stu.SetData( "LiMing ", "21010211 ");
stu.InputScore(80,76,91);
stu.Print();
}            


[解决办法]
#include <iostream.h>
#include <string.h>
class CPerson
{
public:
void SetData(char *name,char *id,bool isman=1)
{
int n=strlen(name);
strncpy(pName,name,n);
n=strlen(id);
strncpy(pID,id,n);
bMan=isman;
}
void Output()
{
cout < < "姓名: " < <pName < <endl;
cout < < "编号: " < <pID < <endl;
char *str=bMan? "男 ": "女 ";
cout < < "性别: " < <str < <endl;
}
private:
char pName[20];
char pID[20];
bool bMan;
};


class CStudent:public CPerson
{
public:
void SetPersonData(char *name,char *id,bool isman=1)
{
SetData(name,id,isman);
}
void InputScore(double score1,double score2,double score3)
{
dbScore[0]=score1;
dbScore[1]=score2;
dbScore[2]=score3;
}

void Print()
{
Output();
for(int i=0;i <3;i++)
cout < < "成绩 " < <i+1 < < ": " < <dbScore[i] < <endl;
}
private:
double dbScore[3];
};
void main()
{
CStudent stu;
stu.SetData( "LiMing ", "21010211 ");
stu.InputScore(80,76,91);
stu.Print();
}

[解决办法]
int n=strlen(name);
strncpy(pName,name,n);
n=strlen(id);
strncpy(pID,id,n);
两个字符数组好象都没\0;在末尾加上就可以拉


热点排行