尝试输入scanf(" %[^\n]%*c", &answer);时,程序挂起一秒钟,然后关闭,该怎么办才能解决此问题。

我尝试了使用&和不使用*aarray[]的情况,并采用了不同的方式在空白处和不同地方使用。在我包含数组并在switch语句中包含while循环之前,这确实起作用了(在完整的代码中,有多个情况不同的问题,而我不想在每个循环中创建一个while循环。)

#include <stdio.h>
void trivia();
int randomquestion();
void main()
{
  trivia();
}
int randomquestion()
{
    int g;
    g = rand() % 29;
    if(g%2 == 0)
    {
        return g;
    }
    else
    {
        return --g;
    }
}
void trivia(void) {
void *answer;
char *aarray[] = {"","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""};
const char *history[] = { "Which English king was \"mad\"?","George III",
                        "Who started the Protestant Reformation?","Martin Luther",
                        "Who was the first person to see the moons of Jupiter?","Galileo",
                        "What Viking group settled in France before conquering England, Sicily, and Malta?","The Normans",
                        "What group sacked Baghdad in 1258, ending the Islamic Golden Age?","The Mongols",
                        "Against what city did Rome fight the Punic Wars?","Carthage",
                        "What yellow gas was famously used in WWI?","Mustard Gas",
                        "What epic poem is thought to be the oldest in the English language?","Beowulf",
                        "What ancient empire was led by Xerxes, Cyrus, and Darius?","Persia",
                        "Who was the most notorious member of the Ba'ath Party?","Saddam Hussein",
                        "What Italian adventurer wrote about his 24 year journey from Venice to China and back?","Marco Polo",
                        "What young pharaoh's tomb was discovered in 1922?","Tutankhamun",
                        "Before becoming king of England, what country was James I the king of?","Scotland",
                        "What was the primary language of the Byzantine Empire?","Greek",
                        "For what crime was Al Capone convicted of in 1931?","Tax Evasion"
                        };
char y;
int a, g, i = 0, points = 0;
printf("This is a trivia game, choose from History (H), Geography (G), Sport (S) or Technology (T) \n");
printf("There will be 5 random questions, and the user will have to enter the correct answer.\n");
printf("Please enter the letter of your choice. \n");
scanf(" %c", &y);
switch(y){
    case 'H' :
        for(a=0; a<29; a++)
        {
            aarray[a] = history[a];
        }
        break;
    default:
        printf("That was an incorrect input. \n");
        trivia();
}
while ( i <5)
{
    g = randomquestion();
    printf("\n%s :", aarray[g]);
    scanf(" %[^\n]%*c", answer);
    printf("\nYour answer is %s", answer);
    if(strcmp(aarray[++g],answer) == 0)
            {
                printf("\nCorrect!");
                ++points;

            }
            else
            {
                printf("\nIncorrect, the answer was %s.", aarray[g]);
            }
    i++;
}
}


我希望它接受输入,然后继续进行while循环。

最佳答案

scanf(" %[^\n]%*c", answer);试图将字符串写入类型为void *的未初始化,未分配(更重要的是)不完整的指针。

您需要将其声明为完整类型(即char *)并为其分配内存,方法是在堆栈上通过显式声明数组大小[LEN]或使用malloc(LEN)及其后的free动态地对其进行分配。

10-07 19:13
查看更多