m0_62801663
2022-01-04 14:56
采纳率: 50%
浏览 27

这个代码哪里错了啊?为什么Visual c++6.0

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE 1024
char* substr(char *s,int startPos,int endPos){
char t[endPos-startPos];
strncpy(t, s + startPos, endPos);
t[endPos] = '\0';
printf("%s\n", t);
return t;
}
int index(char *a,char c)
{
//printf("index yunx");
int i=0;
while(a[i]!='\0')
{
if(a[i]==c)
break;
i++;
}
return i;
}
int main()
{
char buf[MAX_LINE]; /缓冲区/
FILE *fp; /文件指针/
int len; /行字符个数/
if((fp = fopen("word.txt","r")) == NULL)
{
perror("fail to read");
exit (1) ;
}
//FILE *fpWrite=fopen("newtest.txt","w");
//if(fpWrite==NULL)
//{ return 0; }
char wordArray[100][MAX_LINE];
int i=0,j=0;
while(fgets(buf,MAX_LINE,fp) != NULL)
{
len = strlen(buf);
j=len;
buf[len-1] ='\0'; /去掉换行符/
printf("%s\n",buf);
strcpy(wordArray[i],buf);
i++;
}
printf("in put your want to memory word number:\n");
int memoryWordNumber;
scanf("%d",&memoryWordNumber);
char sutinputword[50];
int wordLength,intindexO;
for(int j=0;j<memoryWordNumber;j++)
{
i = rand() % 10; //产生0~9的随机数,注意10会被整除
char a[50];
strcpy(a,wordArray[i]);
intindexO=index(a,' ');//取空格的位置。如apple 苹果 位置为6
len=strlen(wordArray[i]);
printf("in put the word %s\n",substr(wordArray[i],intindexO,len));
scanf("%s",sutinputword);
if(strcmp(sutinputword,substr(wordArray[i],0,intindexO))==0)//比较学生输入的与word里面的是否相等
printf("right!");
else printf("wrong");
}
return 0;
}

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • fuill 2022-01-04 15:17
    最佳回答

    测试没问题, 你是不是文件没放对
    你在D盘建一个 t.txt 文件试试这个代码

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX_LINE 1024
    char* substr(char *s,int startPos,int endPos)
    {
        char t[endPos-startPos];
        strncpy(t, s + startPos, endPos);
        t[endPos] = '\0';
        printf("%s\n", t);
        return t;
    }
    int index(char *a,char c)
    {
    //printf("index yunx");
        int i=0;
        while(a[i]!='\0')
        {
            if(a[i]==c)
                break;
            i++;
        }
        return i;
    }
    int main()
    {
        char buf[MAX_LINE];
        //缓冲区/
        FILE *fp;
        //文件指针/
        int len;
        //行字符个数/
        if((fp = fopen("D:\\t.txt","r")) == NULL)
        {
            perror("fail to read");
            exit (1) ;
        }
    //FILE *fpWrite=fopen("newtest.txt","w");
    //if(fpWrite==NULL)
    //{ return 0; }
        char wordArray[100][MAX_LINE];
        int i=0,j=0;
        while(fgets(buf,MAX_LINE,fp) != NULL)
        {
            len = strlen(buf);
            j=len;
            buf[len-1] ='\0';
            //去掉换行符/
            printf("%s\n",buf);
            strcpy(wordArray[i],buf);
            i++;
        }
        printf("in put your want to memory word number:\n");
        int memoryWordNumber;
        scanf("%d",&memoryWordNumber);
        char sutinputword[50];
        int wordLength,intindexO;
        for(int j=0; j<memoryWordNumber; j++)
        {
            i = rand() % 10; //产生0~9的随机数,注意10会被整除
            char a[50];
            strcpy(a,wordArray[i]);
            intindexO=index(a,' ');//取空格的位置。如apple 苹果 位置为6
            len=strlen(wordArray[i]);
            printf("in put the word %s\n",substr(wordArray[i],intindexO,len));
            scanf("%s",sutinputword);
            if(strcmp(sutinputword,substr(wordArray[i],0,intindexO))==0)//比较学生输入的与word里面的是否相等
                printf("right!");
            else printf("wrong");
        }
        return 0;
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题