我只完成了程序的一部分,因此请选择菜单选项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 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();
}
}
答案 0 :(得分:2)
Eeek:
while(answer[k]!='\0')
{
tolower(answer[k]);
}
k 未初始化。你几乎肯定是从这里开始的。这将导致错误并导致您的崩溃。
如果回答[k]!='\ 0'......
,那么循环将是无穷无尽的答案 1 :(得分:2)
这是while
循环,您尝试将字符串转换为小写。您没有初始化k
,也没有更改其值,因此在绝大多数情况下,while循环将永远继续。你可能想要的是一个for
循环,如下所示:
int i;
for (i = 0; i < strlen(answer, 15); i++) {
answer[i] = tolower(answer[i]);
}