永发信息网

一个C++问题,我无法修改好

答案:2  悬赏:0  手机版
解决时间 2021-07-29 19:02
  • 提问者网友:謫仙
  • 2021-07-28 19:28

#include <iostream.h>
#include <string.h>
#define N 10
class CWork //定义一个类
{
private:
char name[N],addr[N];
char city[N],province[N];
char youbian[6];
public:
CWork(char name[N], char addr[N], char city[N], char province[N],char youbian[6]);
void ChangName();
void Display();
};
CWork::CWork(char name[N], char addr[N], char city[N], char province[N],char youbian[6])
{
char a[N],b[N],c[N],d[N],s[6];
strcpy(name,a);
strcpy(addr,b);
strcpy(city,c);
strcpy(province,d);
strcpy(youbian,s);
}

void CWork::ChangName(char m)
{
strcpy(name,m);

}
void CWork::Display()
{ int i;
cout<<"输入人物信息:"<<endl;
for(i=0;i<N;i++)
cout<<"Name:"<<name[i];
cout<<endl;
for(i=0;i<N;i++)
cout<<"Address:"<<addr[i];
cout<<endl;
for(i=0;i<N;i++)
cout<<"City:"<<city[i]<<endl;
cout<<endl;
for(i=0;i<N;i++)
cout<<"Province:"<<province[i];
cout<<endl;
for(i=0;i<6;i++)
cout<<"Youbian:"<<youbian[i];
cout<<endl;
}

void main()
{
int i,j;
char xming[N],dizhi[N];
char cshi[N],sheng[N];
char ybian[6],n[N];
cout<<"请输入新名:"<<endl;
for(i=0;i<N;i++)
cin>>n[i];
cout<<"请输入姓名:"<<endl; //下面是初始化类
for(i=0;i<N;i++)
cin>>xming[i];
cout<<endl;
cout<<"请输入地址:"<<endl;
for(i=0;i<N;i++)
cin>>dizhi[i];
cout<<endl;
cout<<"请输入城市名:"<<endl;
for(i=0;i<N;i++)
cin>>cshi[i];
cout<<endl;
cout<<"请输入省份:"<<endl;
for(i=0;i<N;i++)
cin>>sheng[i];
cout<<endl;
cout<<"请输入邮编:"<<endl;
for(i=0;i<N;i++)
cin>>ybian[i];
cout<<endl;
class M(xming[N],dizhi[N],cshi[N],sheng[N],ybian[6]);
M.ChangName(n);
M.Display();
}


最佳答案
  • 五星知识达人网友:一把行者刀
  • 2021-07-28 20:50

帮你改好了.测试通过. 你看看.


//cin 和cout可以直接输入输出字符串. 没必要一个一个读入和输出.


//当然,如果你要一个一个读入也可以. 但是要加个判断读入的是否是回车.是回车就结束输入.


//否则每个字符串都要输入10个字符..... 这个太不人性化了!
#include <iostream.h>
#include <string.h>
#define N 10
class CWork //定义一个类
{
private:
char name[N],addr[N];
char city[N],province[N];
char youbian[6];
public:
CWork(char name[], char addr[], char city[], char province[],char youbian[]);
void ChangName(char* ); //声明和定义的参数不一样. 应该以指针的形式传递字符串.
void Display();
};
CWork::CWork(char a[], char b[], char c[], char d[],char s[]) //形参和数据成员别搞混了.
{
strcpy(name,a);
strcpy(addr,b);
strcpy(city,c);
strcpy(province,d);
strcpy(youbian,s);
}


void CWork::ChangName(char *m) //更改参数传递形式.
{
strcpy(name,m);

}
void CWork::Display() //输出直接cout;
{ int i;
cout<<"输入人物信息:"<<endl;


cout<<"Name:"<<name<<endl;



cout<<"Address:"<<addr<<endl;


cout<<"City:"<<city<<endl;


cout<<"Province:"<<province<<endl;


cout<<"Youbian:"<<youbian<<endl;
}


void main() //输入直接cin;
{
int i,j;
char xming[N],dizhi[N];
char cshi[N],sheng[N];
char ybian[6],n[N];
cout<<"请输入新名:"<<endl;
cin>>n;


cout<<"请输入姓名:"<<endl;
cin>>xming;


cout<<"请输入地址:"<<endl;
cin>>dizhi;


cout<<"请输入城市名:"<<endl;
cin>>cshi;


cout<<"请输入省份:"<<endl;


cin>>sheng;


cout<<"请输入邮编:"<<endl;


cin>>ybian;


CWork M(xming,dizhi,cshi,sheng,ybian);
M.ChangName(n);
M.Display();
}
效果如图:


全部回答
  • 1楼网友:深街酒徒
  • 2021-07-28 21:29
你这个问题要表达个什么意思?就是你的 n[N]是什么意思?!
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯