| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3771 人关注过本帖
标题:c++中用strcpy和strcat报错
只看楼主 加入收藏
peswe
Rank: 1
等 级:新手上路
帖 子:197
专家分:0
注 册:2006-11-22
收藏
 问题点数:0 回复次数:16 
c++中用strcpy和strcat报错

俺是新手,菜鸟一个,刚学C++,
刚编了个字符相加的程序,功能是,将两个字符相加,例如:输入hello,和world两个字符,便输出helloworld
可是总是出错,用strcpy,和strcat函数时,也报错;
最后,编译时没错,可是运行不了,着实郁闷,望哪位高手,帮忙写个程序,让俺看看
谢谢了先!

搜索更多相关主题的帖子: strcat strcpy 
2006-11-22 22:39
dragonfly
Rank: 5Rank: 5
等 级:贵宾
威 望:17
帖 子:1024
专家分:0
注 册:2006-3-20
收藏
得分:0 

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!


[此贴子已经被作者于2006-11-23 8:34:36编辑过]


2006-11-23 08:32
peswe
Rank: 1
等 级:新手上路
帖 子:197
专家分:0
注 册:2006-11-22
收藏
得分:0 
新手,以前不知道怎么找到自己发的东西,现在才 知道了,故而特地回来感谢下
谢谢!!!

C斗士~~~fighting!!!!
2006-12-18 22:39
noviceby
Rank: 1
等 级:新手上路
帖 子:51
专家分:0
注 册:2006-12-13
收藏
得分:0 

#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;
}


[此贴子已经被作者于2006-12-18 23:06:57编辑过]


我是菜鸟我怕谁?
2006-12-18 23:04
peswe
Rank: 1
等 级:新手上路
帖 子:197
专家分:0
注 册:2006-11-22
收藏
得分:0 
谢谢了先!!!

C斗士~~~fighting!!!!
2006-12-20 17:01
peswe
Rank: 1
等 级:新手上路
帖 子:197
专家分:0
注 册:2006-11-22
收藏
得分:0 

那顺便帮我看看下面的程序怎么总是出错:

#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;
}

拜托了!!!!!



C斗士~~~fighting!!!!
2006-12-20 17:04
知秋一叶
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-3-11
收藏
得分:0 

应该是使用字符串数组吧,资源分配方面有问题~~~~
建议使用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;
}

2006-12-23 23:10
peswe
Rank: 1
等 级:新手上路
帖 子:197
专家分:0
注 册:2006-11-22
收藏
得分:0 
总之,先谢谢了!

C斗士~~~fighting!!!!
2006-12-25 21:45
peswe
Rank: 1
等 级:新手上路
帖 子:197
专家分:0
注 册:2006-11-22
收藏
得分:0 

哇嘶!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;
}


C斗士~~~fighting!!!!
2006-12-25 21:51
三木
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-12-15
收藏
得分:0 
你把using namespace std;给屏蔽了,不错才怪呢!!!!!!!!!!!!!!!!!!
2006-12-26 20:12
快速回复:c++中用strcpy和strcat报错
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016256 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved