怎么不可以修改名字??

xjtlu_joe 2009-04-20 11:46:53
要修改名字,但到return_name()就不运行了。。。

还有程序有点繁,望高手给点修改意见。。。

谢~



#include<iostream>
#include<string>
using namespace std;

class record{
private:
int staffn;
char firstn[20],familyn[20];
int sal;
public:
record()
{
staffn=999; strcpy(firstn,"New"); strcpy(familyn,"Staff"); sal=0;
}
record(int staffn_in, char firstn_in[20], char familyn_in[20], int sal_in)
{
staffn=staffn_in;
strcpy(firstn, firstn_in);
strcpy(familyn, familyn_in);
sal=sal_in;
}
void show(void)
{
cout<<endl;
cout<<"The staff number is:"<<staffn<<endl;
cout<<"The staff's first name is: "<<firstn<<endl;
cout<<"The staff's family name is: "<<familyn<<endl;
cout<<"The staff's salary is: "<<sal<<endl;
cout<<"End."<<endl;
}

string return_name(char *str1, char *str2)
{


cout<<"Please enter the staff's first name>"<<endl;
cin>>str1;
strcpy(firstn,str1);
cout<<"Please enter the staff's family name>"<<endl;
cin>>str2;
strcpy(familyn,str2);
string str3(str1);
str3 +=str2;
return str3;
}

void changestaffn(void)
{
int sn;
cout<<"Please enter the the staff number>"<<endl;
cin>>sn;
staffn=sn;
cout<<"Action finished with "<<firstn<<" "<<familyn<<endl;
}

void changesal(void)
{
int s;
cout<<"please enter the staff's salary>"<<endl;
cin>>s;
sal=s;
cout<<"Action finished with "<<firstn<<" "<<familyn<<endl;
}

void action(void)
{
int choice;
cout<<"what do you want to do with "<<firstn<<" "<<familyn<<endl;
cout<<"***********************************************"<<endl;
cout<<"1. Display. 2. change the staff's name."<<endl;
cout<<"3. change staff number. "<<endl;
cout<<"4. change staff's salary. 5. exit. "<<endl;
cout<<"***********************************************"<<endl;
cin>>choice;
if(choice==1)
show();
else if(choice==2)
string return_name(char *str1, char *str2);
else if(choice==3)
changestaffn();
else if(choice==4)
changesal();
}

friend int operator + (const record &left, const record &right)
{
return (left.sal + right.sal);
}

};

class manager //Another class to manipulate the objects of class person ?ACTING AS A MANAGER.

{
private:
record p1; //declare a object
record p2;

public:
void run(void) // choose between different person
{
int userchoice=1;
while(userchoice>0 && userchoice<3)
{
cout<<"input your choice: 1 for p1, 2 for p2, 3 to exit"<<endl;
cin>>userchoice;
if (userchoice==1) p1.action();
if (userchoice==2) p2.action();
}
}
};


int main()
{
manager m1;
m1.run();
system("pause");

};
...全文
193 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Qlaiaqu 2009-04-21
  • 打赏
  • 举报
回复
问题终于解决,只是还不明白加 cin.get(); 的原因,为什么前面str1那边不加呢?
还有感觉程序有点繁杂,有什么供修改的建议呢 。。。
/////////////////////////
抱歉,那一行是多余的,呵呵,当时我误认为是输入时没有对间隔符处理好,所以一开始就加了这一行,当然现在是多余的。
mengde007 2009-04-20
  • 打赏
  • 举报
回复
strcpy(firstn,str1);
cout < <"Please enter the staff's family name>" < <endl;
cin>>str2;
strcpy(familyn,str2);
string str3(str1);
刚才好像说过了吧:

strcpy(firstn,str1,19);
firstn[19]='\0';
strcpy(familyn,str2,19);
familyn[19]='\0';
防止越界的。
  • 打赏
  • 举报
回复
描述一下功能吧,楼主.
nuoshueihe 2009-04-20
  • 打赏
  • 举报
回复
你的错误有2点
1:在return_name调用函数的时候,没有传入实际参数
2:在return_name中返回了局部变量
xjtlu_joe 2009-04-20
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 fengxuxing 的回复:]
C/C++ code
#include <iostream>
#include <string>
using namespace std;

class record
{
private:
int staffn;
char firstn[20],familyn[20];
int sal;
public:
record()
{
staffn=999; strcpy(firstn,"New"); strcpy(familyn,"Staff"); sal=0;
}

record(int staffn_in, char firstn_in[20], char familyn_in[20], int sal_in)
{
staffn…
[/Quote]

问题终于解决,只是还不明白加 cin.get(); 的原因,为什么前面str1那边不加呢?
还有感觉程序有点繁杂,有什么供修改的建议呢 。。。
Qlaiaqu 2009-04-20
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>
using namespace std;

class record
{
private:
int staffn;
char firstn[20],familyn[20];
int sal;
public:
record()
{
staffn=999; strcpy(firstn,"New"); strcpy(familyn,"Staff"); sal=0;
}

record(int staffn_in, char firstn_in[20], char familyn_in[20], int sal_in)
{
staffn=staffn_in;
strcpy(firstn, firstn_in);
strcpy(familyn, familyn_in);
sal=sal_in;
}

void show(void)
{
cout <<endl;
cout <<"The staff number is:" <<staffn <<endl;
cout <<"The staff's first name is: " <<firstn <<endl;
cout <<"The staff's family name is: " <<familyn <<endl;
cout <<"The staff's salary is: " <<sal <<endl;
cout <<"End." <<endl;
}

string return_name() //参数设计的很混乱
{
char str1[20], str2[20];
cout <<"Please enter the staff's first name>" <<endl;
cin>>str1;
strcpy(firstn,str1);
cout <<"Please enter the staff's family name>" <<endl;
cin.get();////////////////////////////////////////////////////加这一行
cin>>str2;
strcpy(familyn,str2);
string str3(str1);
str3 +=str2;
return str3;
}

void changestaffn(void)
{
int sn;
cout <<"Please enter the the staff number>" <<endl;
cin>>sn;
staffn=sn;
cout <<"Action finished with " <<firstn <<" " <<familyn <<endl;
}

void changesal(void)
{
int s;
cout <<"please enter the staff's salary>" <<endl;
cin>>s;
sal=s;
cout <<"Action finished with " <<firstn <<" " <<familyn <<endl;
}

void action(void)
{
int choice;
cout <<"what do you want to do with " <<firstn <<" " <<familyn <<endl;
cout <<"***********************************************" <<endl;
cout <<"1. Display. 2. change the staff's name." <<endl;
cout <<"3. change staff number. " <<endl;
cout <<"4. change staff's salary. 5. exit. " <<endl;
cout <<"***********************************************" <<endl;
cin>>choice;
if(choice==1)
show();
else if(choice==2)
return_name();
else if(choice==3)
changestaffn();
else if(choice==4)
changesal();
}

friend int operator + (const record &left, const record &right)
{
return (left.sal + right.sal);
}

};

class manager //Another class to manipulate the objects of class person ?ACTING AS A MANAGER.

{
private:
record p1; //declare a object
record p2;

public:
void run(void) // choose between different person
{
int userchoice=1;
while(userchoice>0 && userchoice <3)
{
cout <<"input your choice: 1 for p1, 2 for p2, 3 to exit" <<endl;
cin>>userchoice;
cin.get();
if (userchoice==1) p1.action();
if (userchoice==2) p2.action();
}
}
};


int main()
{
manager m1;
m1.run();
system("pause");

};
Qlaiaqu 2009-04-20
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>
using namespace std;

class record
{
private:
int staffn;
char firstn[20],familyn[20];
int sal;
public:
record()
{
staffn=999; strcpy(firstn,"New"); strcpy(familyn,"Staff"); sal=0;
}

record(int staffn_in, char firstn_in[20], char familyn_in[20], int sal_in)
{
staffn=staffn_in;
strcpy(firstn, firstn_in);
strcpy(familyn, familyn_in);
sal=sal_in;
}

void show(void)
{
cout <<endl;
cout <<"The staff number is:" <<staffn <<endl;
cout <<"The staff's first name is: " <<firstn <<endl;
cout <<"The staff's family name is: " <<familyn <<endl;
cout <<"The staff's salary is: " <<sal <<endl;
cout <<"End." <<endl;
}

string return_name() //参数设计的很混乱
{
char str1[20], str2[20];
cout <<"Please enter the staff's first name>" <<endl;
cin>>str1;
strcpy(firstn,str1);
cout <<"Please enter the staff's family name>" <<endl;
cin.get();////////////////////////////////////////////////////加这一行
cin>>str2;
strcpy(familyn,str2);
string str3(str1);
str3 +=str2;
return str3;
}

void changestaffn(void)
{
int sn;
cout <<"Please enter the the staff number>" <<endl;
cin>>sn;
staffn=sn;
cout <<"Action finished with " <<firstn <<" " <<familyn <<endl;
}

void changesal(void)
{
int s;
cout <<"please enter the staff's salary>" <<endl;
cin>>s;
sal=s;
cout <<"Action finished with " <<firstn <<" " <<familyn <<endl;
}

void action(void)
{
int choice;
cout <<"what do you want to do with " <<firstn <<" " <<familyn <<endl;
cout <<"***********************************************" <<endl;
cout <<"1. Display. 2. change the staff's name." <<endl;
cout <<"3. change staff number. " <<endl;
cout <<"4. change staff's salary. 5. exit. " <<endl;
cout <<"***********************************************" <<endl;
cin>>choice;
if(choice==1)
show();
else if(choice==2)
return_name();
else if(choice==3)
changestaffn();
else if(choice==4)
changesal();
}

friend int operator + (const record &left, const record &right)
{
return (left.sal + right.sal);
}

};

class manager //Another class to manipulate the objects of class person ?ACTING AS A MANAGER.

{
private:
record p1; //declare a object
record p2;

public:
void run(void) // choose between different person
{
int userchoice=1;
while(userchoice>0 && userchoice <3)
{
cout <<"input your choice: 1 for p1, 2 for p2, 3 to exit" <<endl;
cin>>userchoice;
cin.get();
if (userchoice==1) p1.action();
if (userchoice==2) p2.action();
}
}
};


int main()
{
manager m1;
m1.run();
system("pause");

}; [/code]
xjtlu_joe 2009-04-20
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 pengzhixi 的回复:]
编译OK啊,不知道你说啥错误!
[/Quote]

不可以修改名字啊。。。string return_name()里的功能,改变staff的名字。。。

而且不可以循环运行,改变一个变量就结束了。。。。

overload那边也有些问题。。。
pengzhixi 2009-04-20
  • 打赏
  • 举报
回复
编译OK啊,不知道你说啥错误!
sherrik 2009-04-20
  • 打赏
  • 举报
回复
up
信念 2009-04-20
  • 打赏
  • 举报
回复
看着真乱,也不给个变量描述。。。
xjtlu_joe 2009-04-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 mengde007 的回复:]
strcpy(firstn,str1);
cout < <"Please enter the staff's family name>" < <endl;
cin>>str2;
strcpy(familyn,str2);
string str3(str1);
刚才好像说过了吧:

strcpy(firstn,str1,19);
firstn[19]='\0';
strcpy(familyn,str2,19);
familyn[19]='\0';
防止越界的。
[/Quote]

运行不了啊,用了你的方法。。。
xjtlu_joe 2009-04-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hairetz 的回复:]
描述一下功能吧,楼主.
[/Quote]

运行不了啊,用了你的方法。。。string return_name(char *str1, char *str2)

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧