首页 > 解决方案 > scanf() 尝试输入带或不带空格的字符串时关闭程序

问题描述

尝试进入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 循环继续。

标签: c

解决方案


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

您需要将它声明为一个完整的类型(即)char *并为其分配内存,或者通过显式声明数组大小在堆栈上,[LEN]或者动态地声明数组大小。malloc(LEN)free


推荐阅读