为什么我的语言抽认卡程序仅仅因为 if 语句而崩溃

  • 本文关键字:因为 程序 if 语句 崩溃 语言 c
  • 更新时间 :
  • 英文 :


我只完成了程序的 1 部分,所以选择菜单选项 1一切正常,直到我添加了 if(words[i].errorflag==0){} in engtotagall();这样用户就可以选择仅在之后回答他的错误。在菜单中选择 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 Programn");
        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 - Exitn");
        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]!='')
            {
                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("nn1 - Take the whole test againn2 - Answer your mistakes only3 - Go back to Menun4 - Exit Programn");
    scanf("%d",choice);
    if(choice==1)
    {
        initialize();
        engtotagall();
    }
    else if (choice==2)
    {
        engtotagall();
    }
}

Eeek:

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

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

另外,如果答案 [k] != '\0',那么这个循环将是无穷无尽的......

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

int i;
for (i = 0; i < strlen(answer, 15); i++) {
    answer[i] = tolower(answer[i]);
}

相关内容

最新更新