Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试输入带或不带空格的字符串时,scanf()将关闭程序_C - Fatal编程技术网

尝试输入带或不带空格的字符串时,scanf()将关闭程序

尝试输入带或不带空格的字符串时,scanf()将关闭程序,c,C,尝试输入scanf时(“%[^\n]%*c”,&answer)程序挂起一秒钟然后关闭,我可以做些什么来修复此问题 我尝试过使用和不使用&,在不同的地方使用不同的方法来处理空白。在我加入数组*aarray[]并在switch语句中包含while循环之前,这确实起到了作用(在完整的代码中,有多个案例有不同的问题,我不想在每个案例中创建while循环) #包括 虚无琐事(); int随机问题(); void main() { 琐事(); } 整数问题() { int g; g=rand()%29; 如

尝试输入
scanf时(“%[^\n]%*c”,&answer)程序挂起一秒钟然后关闭,我可以做些什么来修复此问题

我尝试过使用和不使用
&
,在不同的地方使用不同的方法来处理空白。在我加入数组
*aarray[]
并在switch语句中包含while循环之前,这确实起到了作用(在完整的代码中,有多个案例有不同的问题,我不想在每个案例中创建while循环)

#包括
虚无琐事();
int随机问题();
void main()
{
琐事();
}
整数问题()
{
int g;
g=rand()%29;
如果(g%2==0)
{
返回g;
}
其他的
{
return--g;
}
}
虚空琐事(虚空){
无效*答案;
char*aarray[]={、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、};
const char*history[]={“哪个英国国王是“疯子”?,“乔治三世”,
“谁发起了新教改革?”,“马丁·路德”,
“谁是第一个看到木星卫星的人?”,“伽利略”,
“在征服英国、西西里岛和马耳他之前,哪个维京人团体在法国定居?”,“诺曼人”,
“什么组织在1258年洗劫了巴格达,结束了伊斯兰黄金时代?”,“蒙古人”,
“罗马和哪个城市打过布匿战争?”,“迦太基”,
“第一次世界大战中使用了什么著名的黄色气体?”,“芥子气”,
“哪首史诗被认为是英语中最古老的?”,“贝奥武夫”,
“哪个古代帝国是由薛西斯、居鲁士和大流士领导的?”,“波斯”,
“复兴党最臭名昭著的成员是谁?”,“萨达姆·侯赛因”,
“哪位意大利冒险家写下了他24年来从威尼斯往返中国的旅程?”,“马可波罗”,
“1922年发现了哪座年轻法老的陵墓?”,“图坦卡蒙”,
“在成为英格兰国王之前,詹姆斯一世是哪个国家的国王?”,“苏格兰”,
“拜占庭帝国的主要语言是什么?”,“希腊语”,
“阿尔·卡彭在1931年犯了什么罪?”,“逃税”
};
chary;
int a,g,i=0,点=0;
printf(“这是一个琐事游戏,可从历史(H)、地理(G)、运动(S)或技术(T)中选择)\n”);
printf(“将有5个随机问题,用户必须输入正确答案。\n”);
printf(“请输入您选择的字母。\n”);
scanf(“%c”、&y);
开关(y){
案例“H”:
对于(a=0;a
scanf(“%[^\n]%*c”,答案);
正在尝试将字符串写入类型为
void*
的未初始化、未分配(更重要的是)不完整指针


您需要将其声明为完整类型(即
char*
),并为其分配内存,可以通过显式声明数组大小
[LEN]
在堆栈上,也可以使用
malloc(LEN)动态地为其分配内存
和以后的
免费
它。

又没有了?主
在哪里?A是我们可以复制/粘贴/编译而不用猜测的东西。我有幻觉,或者仍然是这样的
void*answer;…scanf(%[^\n]*c',&answer)
?^^请避免“魔法”像
29
@Jabberwocky这样的数字表示同意,但这是他在问题中特别提出的问题。感谢您回答我的问题@GovindParmar这为我解决了这个问题。
#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++;
}
}