很郁闷 大家帮帮忙!

wawa_ai_chengxu 2009-06-08 05:34:52
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#define N 2
void swap(char *a,char *b)
{char *t;
strcpy(t,a);
strcpy(a,b);
strcpy(b,t);
}
void main()
{int i;
char *e[2];
char *c[2];
e[1]="today";c[1]="今天";
e[2]="word";c[2]="单词";
int k=0;
if(strcmp(e[1],e[2])>0) {swap(e[1],e[2]); swap(c[1],c[2]);}
cout<<e[1]<<" "<<c[1]<<endl;
cout<<e[2]<<" "<<c[2]<<endl;
//for(j=0;j<2;j++) //用这两句为什么就不行
//cout<<e[i]<<" "<<c[i]<<endl;
}
...全文
33 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiangjiantui 2009-06-08
  • 打赏
  • 举报
回复
要养成编程好习惯~
wawa_ai_chengxu 2009-06-08
  • 打赏
  • 举报
回复
谢谢大家 有好多细节都注意。
wesweeky 2009-06-08
  • 打赏
  • 举报
回复
mark
majun01 2009-06-08
  • 打赏
  • 举报
回复
#include <iostream.h> 
#include <fstream.h>
#include <string.h>
#define N 2
void swap(char *&a,char *&b)
{
char *t;
t=a;//strcpy(t,a);
a=b;//strcpy(a,b);
b=t;//strcpy(b,t);
}
void main()
{
int i;
char *e[2];
char *c[2];
e[0]="today";c[0]="今天";
e[1]="word";c[1]="单词";
int k=0;
cout<<e[0]<<" "<<c[0]<<endl;
cout<<e[1]<<" "<<c[1]<<endl;
if(strcmp(e[1],e[0])>0)
{
swap(e[0],e[1]);
swap(c[0],c[1]);
}
for(i=0;i <2;i++)
cout<<e[i]<<" "<<c[i]<<endl;
}
wind_breez 2009-06-08
  • 打赏
  • 举报
回复
内存使用错误。swap里t没有申请空间;e与c两个指针数组里的指针也没有申请空间,指向的是字符串常量。
koulongfei 2009-06-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 xueyayunbingxue 的回复:]
你没有using namespace std;
[/Quote]

不懂不要乱说.
人家头文件是iostream.h,就用不着using namespace std;
chenzhp 2009-06-08
  • 打赏
  • 举报
回复
#include <iostream> 
#include <fstream>
#include <string>
using namespace std;
#define N 2
void swap(char *a,char *b)
{char *t;
strcpy(t,a);
strcpy(a,b);
strcpy(b,t);
}
int main()
{int j=0;
char *e[2];
char *c[2];
e[1]="today";c[1]="½ñÌì";
e[2]="word";c[2]="µ¥´Ê";
int k=0;
if(strcmp(e[1],e[2])>0) {swap(e[1],e[2]); swap(c[1],c[2]);}
//cout <<e[1] <<" " <<c[1] <<endl;
//cout <<e[2] <<" " <<c[2] <<endl;
for(j=1;j <=2;j++) //如果j从0开始,e[0]从何而来???j应该从1开始自增
cout <<e[j] <<" " <<c[j] <<endl;
return 0;
}
koulongfei 2009-06-08
  • 打赏
  • 举报
回复
姐姐,拜托把i与j区分清楚再来发帖子
xueyayunbingxue 2009-06-08
  • 打赏
  • 举报
回复
你没有using namespace std;

64,685

社区成员

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

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