0

我只完成了程序的 1 部分,所以选择菜单选项 1 一切正常,直到我 在 engtotagall(); 中添加if(words[i].errorflag==0){} ; 这样用户只有在之后才可以选择回答他的错误。在菜单中选择 1 后程序崩溃。这是为什么?我不明白为什么会这样。

#include <stdio.h>
#include <stdlib.h>
int clear(void);
int initialize(void);
int engtotagall(void);
int tagtoengall(void);
int engtotagnoun(void);
int tagtoengnoun(void);
int engtotagverb(void);
int tagtoengverb(void);
int engtotagothers(void);
int tagtoengothers(void);

struct flshcard
{
    char english[10];
    char tagalog[10];
    char speechprt;
    char errorflag;
}words[10];

int main()
{
    while(1)
    {
        char choice;
        printf("Language Flashcard Practice Program\n");
        printf("1 - English -> Tagalog (All Words)\n2 - Tagalog -> English (All Words)\n3 - English -> Tagalog (Nouns)\n4 - Tagalog -> English (Nouns)\n5 - English -> Tagalog (Verbs)\n6 - Tagalog -> English (Verbs)\n7 - English -> Tagalog (Others)\n8 - Tagalog -> English (Others)\n9 - Exit\n");
        choice=getchar();
        if(choice=='1')
        {
            clear();
            initialize();
            engtotagall();
        }

        else if(choice=='9')
        {
            exit(EXIT_SUCCESS);
        }
        else
        {
            clear();
            printf("\n%cPlease input a valid option.\n",7);
        }
    }
}

/* Start of function for clearing the pages */
int clear(void)
{
    int i;
    for(i=0;i<25;i++)
    {
        printf("\n");
    }
}

/* Start of the function for initializing the words */
int initialize(void)
{
    /* Making errorflag 0 in all words array */
    int i;
    for(i=0;i<10;i++)
    {
        words[i].errorflag=0;
    }
    /* words[0] struct */
    strcpy(words[0].english,"cards");
    strcpy(words[0].tagalog,"baraha");
    words[0].speechprt='n';
    /* words[1] struct */
    strcpy(words[1].english,"punch");
    strcpy(words[1].tagalog,"suntok");
    words[1].speechprt='v';
    /* words[2] struct */
    strcpy(words[2].english,"ugly");
    strcpy(words[2].tagalog,"pangit");
    words[2].speechprt='o';
    /* words[3] struct */
    strcpy(words[3].english,"child");
    strcpy(words[3].tagalog,"bata");
    words[3].speechprt='n';
    /* words[4] struct */
    strcpy(words[4].english,"dance");
    strcpy(words[4].tagalog,"sayaw");
    words[4].speechprt='v';
    /* words[5] struct */
    strcpy(words[5].english,"small");
    strcpy(words[5].tagalog,"maliit");
    words[5].speechprt='o';
    /* words[6] struct */
    strcpy(words[6].english,"cat");
    strcpy(words[6].tagalog,"pusa");
    words[6].speechprt='n';
    /* words[7] struct */
    strcpy(words[7].english,"jump");
    strcpy(words[7].tagalog,"talon");
    words[7].speechprt='v';
    /* words[8] struct */
    strcpy(words[8].english,"dumb");
    strcpy(words[8].english,"bobo");
    words[8].speechprt='o';
    /* words[9] struct */
    strcpy(words[9].english,"frog");
    strcpy(words[9].tagalog,"palaka");
    words[9].speechprt='n';
}
/* Start of function for English to Tagalog (All Words) */
int engtotagall(void)
{
    int i,k,choice;
    char answer[15];
    for(i=0;i<10;i++)
    {
        if(words[i].errorflag==0) /* I just added this and its now crashing */
        {
            printf("\nWhat is the tagalog for %s?\n",words[i].english);
            gets(answer);
            while(answer[k]!='\0')
            {
                tolower(answer[k]);
            }
            if(strcmp(answer,words[i].tagalog)==0)
            {
                printf("\nYou are correct!!\n");
            }
            else
            {
                printf("\nWrong answer!!\n");
                words[i].errorflag=1;
            }
        }
    }
    printf("\n\n1 - Take the whole test again\n2 - Answer your mistakes only\3 - Go back to Menu\n4 - Exit Program\n");
    scanf("%d",choice);
    if(choice==1)
    {
        initialize();
        engtotagall();
    }
    else if (choice==2)
    {
        engtotagall();
    }
}
4

2 回答 2

2

爱克:

    while(answer[k]!='\0')
    {
        tolower(answer[k]);
    }

k未初始化。 你几乎可以肯定从一开始就在这里越界。这将导致故障并导致您的崩溃。

另外,如果 answer[k] != '\0'...

于 2012-04-22T14:28:13.713 回答
2

这是while您尝试将字符串转换为小写的循环。你不初始化k,也不改变它的值,所以在绝大多数情况下,while循环将永远持续下去。你可能想要的是一个for循环,像这样:

int i;
for (i = 0; i < strlen(answer, 15); i++) {
    answer[i] = tolower(answer[i]);
}
于 2012-04-22T14:28:19.427 回答