注册 登录
编程论坛 C++教室

c++中用strcpy和strcat报错

peswe 发布于 2006-11-22 22:39, 3782 次点击

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

16 回复
#2
dragonfly2006-11-23 08:32

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编辑过]

#3
peswe2006-12-18 22:39
新手,以前不知道怎么找到自己发的东西,现在才 知道了,故而特地回来感谢下
谢谢!!!
#4
noviceby2006-12-18 23:04

#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编辑过]

#5
peswe2006-12-20 17:01
谢谢了先!!!
#6
peswe2006-12-20 17:04

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

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

拜托了!!!!!


#7
知秋一叶2006-12-23 23:10

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

#8
peswe2006-12-25 21:45
总之,先谢谢了!
#9
peswe2006-12-25 21:51

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

#10
三木2006-12-26 20:12
你把using namespace std;给屏蔽了,不错才怪呢!!!!!!!!!!!!!!!!!!
#11
知秋一叶2006-12-26 23:56

关于名字空间的使用请参见:
《C++ Primer》----8.6 使用名字空间成员-----P631
如果不用using namepace std,还有其他的两种选择。下面是其中之一:

#include <iostream.h>
#include <string.h>

using std::string;
using std::cout;
using std::cin;
using std::endl;

class MyClass{
string str;
public:
MyClass(){}
MyClass(const std::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;
}

#12
forever0432006-12-27 08:54
<string.h> 和 <string> 不是同一个文件,
<string>里面是关于string类的定义,
<string.h>里是C中的字符串处理函数,
可以打开include文件夹自己看一看
#13
一二三四五2006-12-27 11:24
学习了

#14
peswe2007-01-01 09:01

受教了,谢谢各位!!!!!

#15
kai2007-01-01 18:24
如果用C++ 的string 类, 下面的两种写法是有区别的。
写法1: cin>>str;
写法2: getline(cin, str);

前者你只能得到一个单词,比如你输入hello world, 那么只有hello 被变量str 接受了。
后者你能够得到完整的输入,如果输入hello world, 那么str 的内容就是hello world

还有就是12楼的观点完全正确,这也是往往会搞混淆的地方。
#16
yuyunliuhen2007-01-01 23:35

还真没注意这么多,学习了,谢谢!

[此贴子已经被作者于2007-1-1 23:57:42编辑过]

#17
peswe2007-01-02 17:00

学习了

1