gpt4 book ai didi

c - 为什么我的语言抽认卡程序仅仅因为一个 if 语句就崩溃了?

转载 作者:太空宇宙 更新时间:2023-11-04 06:39:37 25 4
gpt4 key购买 nike

我只完成了程序的 1 部分,所以选择菜单选项 1一切正常,直到我添加 if(words[i].errorflag==0){} 在 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 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();
}
}

最佳答案

哎呀:

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

k 未初始化。您几乎肯定从一开始就超出了范围。那会出现段错误并导致您的崩溃。

另外,如果 answer[k] != '\0'...,那么循环将是无限的

关于c - 为什么我的语言抽认卡程序仅仅因为一个 if 语句就崩溃了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10268712/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com