c++新手的问题(2)!

zeroman20 2004-03-21 10:33:04
不用strcpy(),把b[]字母放到a[]后面
#include <iostream.h>
main()
{
int i,j,n=0,m=0;
char a[20];
char b[20];

cout<<"请输入一些字母:"<<endl;
cin>>a;
cout<<"请再输入一些字母:"<<endl;
cin>>b;

for (i=n-1;i<=m+n;i++)
{
for(j=0;j<=m;j++)
{
a[i]=b[j];
}
}
for(i=0;i<=20;i++)
cout<<a[i]<<endl;
}

...全文
73 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
happyersun 2004-03-23
  • 打赏
  • 举报
回复
didididao
liyanbo1984 2004-03-22
  • 打赏
  • 举报
回复
应该是这样的
for(i=n-1,j=0;j<m;i++,j++)
{

a[i]=b[j];

}
两个数组应该一起向前进才对啊
letifly 2004-03-22
  • 打赏
  • 举报
回复
为防数据外溢,建议建立一个大一些的数组,或者加上校验,否则很容易出错。

具体方法,用遍历b[]在'\0'处依次填加为a[]的元素,你那么聪明,难不倒的。
jp1984 2004-03-22
  • 打赏
  • 举报
回复
#include<iostream>
#include<vector>
using namespace std;
template<typename T>
void join(vector<T>& vA,const verctor<T>& vB)

{
//get the size of vB for the function size()
int i,sizeB=vB.size();
for(i=0;i<sizeB;i++) //use index to call the element of vB,and use push_back()
vA.push_back(vB[i]);
}
void main(){
.....

}
Seasharka 2004-03-22
  • 打赏
  • 举报
回复
把herryhuang(Herry) 做的

for(; !(*pc); pc++);改为 for(; (*pc)!='\0'; pc++);

for(; ! b[i]; *pc++ = b[i++]); 改为 for(;b[i]!='\0'; *pc++ = b[i++]);

就可以了,但有乱码!!!!!

怎么解决掉乱码???????
xiyuan0418 2004-03-22
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;

int main()
{
int i = 0;
char a[100];
char b[50];
char* pc = a;
cout<<"请输入一些字母:"<<endl;
cin>>a;
cout<<"请再输入一些字母:"<<endl;
cin>>b;
for(; (*pc); pc++);
for(; b[i]; *pc++ = b[i++]);
*pc='\0':
cout << a << endl;
return 0;
}

for(; !(*pc); pc++);
for(; ! b[i]; *pc++ = b[i++]);
这里错了 ····加!干吗啊???
再加一句
*pc='\0':
干吗要用using namespace std;

这里根本没用到!!!
Seasharka 2004-03-22
  • 打赏
  • 举报
回复
herryhuang(Herry)

老大你的也错了,不行

for(; !(*pc); pc++);

不能动
herryhuang 2004-03-22
  • 打赏
  • 举报
回复
Seasharka(前途)

试一下再说!
hdp4820 2004-03-22
  • 打赏
  • 举报
回复
还有一个问题,当你的输入大于数组大小时,就会发生内存错误了,没有那么多的空间.
可以加上点东西:
if(strlen(a)>100)
hdp4820 2004-03-22
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;

int main()
{
int i = 0;
char a[100];
char b[50];
char* pc = a;
cout<<"请输入一些字母:"<<endl;
cin>>a;
cout<<"请再输入一些字母:"<<endl;
cin>>b;
for(; (*pc); pc++); //看看是不是不为空,为空就停.
for(; b[i]; *pc++ = b[i++]);//和上面一个道理.
*pc++='\0';//为末尾加一个结束符,不然像上面那个兄台讲的一样有乱码
cout << a << endl;
return 0;
}

这样就对了,我在VC6上编译通过,运行成功.
happlyman 2004-03-21
  • 打赏
  • 举报
回复
好好看我上面写?号的地方!
zeroman20 2004-03-21
  • 打赏
  • 举报
回复
不能运行~~ 嘿嘿怎么会事
happlyman 2004-03-21
  • 打赏
  • 举报
回复
int i,j,n=0,m=0; //??????????
char a[20];
char b[20];

cout<<"请输入一些字母:"<<endl;
cin>>a;
cout<<"请再输入一些字母:"<<endl;
cin>>b;

for (i=n-1;i<=m+n;i++) //??????????????
{
for(j=0;j<=m;j++)
{
a[i]=b[j];
}
}
for(i=0;i<=20;i++)
cout<<a[i]<<endl;
n = 0 m = 0哪n-1 m+n???
carambo 2004-03-21
  • 打赏
  • 举报
回复
用vector啥!
用标准库好啥!
herryhuang 2004-03-21
  • 打赏
  • 举报
回复
没看懂你的程序,先告诉你肯定不对。

#include <iostream>
using namespace std;

int main()
{
int i = 0;
char a[100];
char b[50];
char* pc = a;
cout<<"请输入一些字母:"<<endl;
cin>>a;
cout<<"请再输入一些字母:"<<endl;
cin>>b;
for(; !(*pc); pc++);
for(; ! b[i]; *pc++ = b[i++]);
cout << a << endl;
return 0;
}

64,648

社区成员

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

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