找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 1025|回复: 9

c++中用strcpy和strcat报错

[复制链接]
发表于 2009-11-4 01:54:56 | 显示全部楼层 |阅读模式 IP:江苏扬州
俺是新手,菜鸟一个,刚学C++,
刚编了个字符相加的程序,功能是,将两个字符相加,例如:输入hello,和world两个字符,便输出helloworld
可是总是出错,用strcpy,和strcat函数时,也报错;
最后,编译时没错,可是运行不了,着实郁闷,望哪位高手,帮忙写个程序,让俺看看
谢谢了先!
发表于 2009-11-4 01:54:57 | 显示全部楼层 IP:江苏扬州
Example

/* STRCPY.C: This program uses strcpy
* and strcat to build a phrase.
*/

#include <string.h>
#include <stdio.h>

void main( void )
{
char string[80];
strcpy( string, "Hello world from " );
strcat( string, "strcpy " );
strcat( string, "and " );
strcat( string, "strcat!" );
printf( "String = %s\n", string );
}

Output
String = Hello world from strcpy and strcat!
回复

使用道具 举报

发表于 2009-11-4 01:54:59 | 显示全部楼层 IP:江苏扬州
新手,以前不知道怎么找到自己发的东西,现在才 知道了,故而特地回来感谢下
谢谢!!!
回复

使用道具 举报

发表于 2009-11-4 01:55:00 | 显示全部楼层 IP:江苏扬州
#include <iostream>
#include <string>
using namespace std;
void main ()
{
char char1[15];
char char2[15];
cout << "Enter the first Character string:";
cin >> char1;
cout << "Enter the second Character string:";
cin >> char2;
int len = strlen ( char1 ) + strlen ( char2 );
char *char3 = new char[len + 1];
strcpy ( char3, char1 );
strcat ( char3, char2 );
cout << "New Character string After Merge:" << char3;
}
回复

使用道具 举报

发表于 2009-11-4 01:55:02 | 显示全部楼层 IP:江苏扬州
谢谢了先!!!
回复

使用道具 举报

发表于 2009-11-4 01:55:03 | 显示全部楼层 IP:江苏扬州
那顺便帮我看看下面的程序怎么总是出错:

#include <iostream.h>
#include <string.h>
class zf
{
char * str;
public:
void input()
{
cout<<"输入字符:"<<endl;
cin>>str;
}
void print()
{
cout<<str<<endl;
}
~zf()
{
delete str;
}
friend zf operator + (zf &a,zf &b);
};
zf operator + (zf &a,zf &b)
{
zf temp;
delete temp.str;
temp.str=new char [strlen(a.str)+strlen(b.str)+1];
strcpy(temp.str,a.str);
strcat(temp.str,b.str);
return temp;
}
main()
{
zf a ,b,c;
a.input();
b.input();
c=b+a;
c.print();
return 0;
}

拜托了!!!!!
回复

使用道具 举报

发表于 2009-11-4 01:55:04 | 显示全部楼层 IP:江苏扬州
应该是使用字符串数组吧,资源分配方面有问题~~~~
建议使用string,而不是char*:

#include <iostream>
#include <string>
using namespace std;
class MyClass{
string str;
public:
MyClass(){}
MyClass(const string& str_):str(str_){}
void input(){
cout<<"please input a string: ";
cin>>str;
}
void print() const{
cout<<"The result: ";
cout<<str<<endl;
}
friend MyClass operator+(const MyClass &a,const MyClass &b){
return MyClass(a.str+' '+b.str);
}
~MyClass(){}
};
int main(){
MyClass a,b,c;
a.input();
b.input();
c=a+b;
c.print();
return 0;
}
回复

使用道具 举报

发表于 2009-11-4 01:55:05 | 显示全部楼层 IP:江苏扬州
总之,先谢谢了!
回复

使用道具 举报

发表于 2009-11-4 01:55:06 | 显示全部楼层 IP:江苏扬州
哇嘶!7楼的你好厉害,真的可以了,
可是我经过以下变化就错了好多错误,拜托帮我解释:

#include <iostream.h> // 这里改变了
#include <string.h>
//using namespace std;
class MyClass{
string str;
public:
MyClass(){}
MyClass(const string& str_):str(str_){}
void input(){
cout<<"please input a string: ";
cin>>str;
}
void print() const{
cout<<"The result: ";
cout<<str<<endl;
}
friend MyClass operator+(const MyClass &a,const MyClass &b){
return MyClass(a.str+' '+b.str);
}
~MyClass(){}
};
int main(){
MyClass a,b,c;
a.input();
b.input();
c=a+b;
c.print();
return 0;
}
回复

使用道具 举报

发表于 2009-11-4 01:55:07 | 显示全部楼层 IP:江苏扬州
你把using namespace std;给屏蔽了,不错才怪呢!!!!!!!!!!!!!!!!!!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-10-1 05:31 , Processed in 0.130915 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表