为什么编译总不过啊
为什么不能顺利执行 各位大侠优化一下
#include"stdio.h"
#include"ctype.h"
#include"string.h"
extern char *a[1000];
#define ENGLISH(i) a[2*i]
#define CHINESE(i) a[2*i+1]
//
int readfile(FILE *fp){//读入文件
int i=0;
while(!feof(fp)){
i++;
fgets(a[2*i],32,fp);
fgets(a[2*i+1],32,fp);
}
return i;
}
//
void getname(int lim,char bf[])//得到文件名
{
int c,i=0;
printf("Word file name:\n");
while(i<lim&&(c=getchar())!=EOF&&!isspace(c))
bf[i++]=c;
if(c!='\n')
while(getchar()!='\n');//吃掉剩余字符
bf[i]='\0';
}//
int next(){
int c;
printf("Anather file name? input y/n\n");
while (!isspace((c=getchar())));
while(getchar()!='\n');
return(c=='y'||c=='Y');
}
//
void findout(int sum)//验证函数
{
char m[32],a;
int n,l=0;
printf("Input the word you wanted!!!\n");
while(isspace(getchar()));
while(!isspace(a=getchar())&&l<31){
m[l]=a;
++l;
}
m[l]='\0';
while(getc(stdin)!='\n');
for(n=0;n<sum;++n){
if (strcmp(m, a[2*n]) == 0)
printf("%s",CHINESE(n));
else printf("There is no the word!sorry!!!\n");
}
}
//
int main()
{
FILE* fp;
int term;
char bp[256];
do{
getname(256,bp);
if((fp=fopen(bp,"r"))==NULL)
fprintf(stderr,"Wrong file name!!!\n");
term=readfile(fp);
printf("zhe number is %d",term);
fclose(fp);
if(term==0)continue;
do{
findout(term);
}while(1);
}while (next());
}
[解决办法]
char m[32],a;
(strcmp(m, a[2*n]) == 0)
a的定义,后面的参数怎么能对,很明显啊
[解决办法]
a 未定义;
findout中内部变量隐藏了全局变量,使得后面应用时出现二义性。
你可以这样:[code=c/c++]
void findout(int sum)//验证函数
{
char m[32];
int n,l=0;
printf("Input the word you wanted!!!\n");
do {
int a = getchar();
if(isspace(a)&&!l)continue;
m[l]=a;
}while(!isspace(m[l])&&l++ <30);
m[l]='\0';
while(getc(stdin)!='\n');
[/code]但是,你的a依然没有地方定义,作为1000字符串指针来说,整个程序缺乏空间的申请与释放,要改的地方还很多。
[解决办法]
偶遇到类似问题都是用
“每次用/*...*/注释掉不同部分再重新编译,直到定位到具体语法出错的位置。”
的方法解决的。
[解决办法]
你字符串数组a未分配空间啊,
- C/C++ code
set(int len){ for(int i=0;i<1000;++i) a[i]=new char[len+1];}free(){ for(int i=0;i<1000;++i) if(a[i])delete []a[i];};