如何修复设置 2D 字符数组时的"initializer-string for array of chars is too long"和"excess elements in char array i

  • 本文关键字:array long too is excess elements char in chars of 数组 c
  • 更新时间 :
  • 英文 :


我正在尝试制作一个2D数组,它将在数组的每一行中存储一个问题和一个答案,我需要做什么来设置它,以及能够调用该数组中的行。

我试图将括号更改为数组名称右侧(例如[15][2],[15],没有括号),并研究了一些堆叠式流问题,这些问题对我的问题不太适用。在switch语句的'H'情况下,我还得到了"下标值既不是数组也不是指针也不是矢量"的错误。在加载过程中,我还收到了几个"标量初始化器周围的大括号"、"标量初始化程序中的多余元素"one_answers"从指针生成整数而不进行强制转换[-Wint conversion]"警告。如果我犯了任何明显的错误,我深表歉意。我从Python学习C,但仍在努力理解它。

int trivia() {
char history[15] = { {"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 sport[15] = { {"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 geography[15] = { {"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 technology[15] = { {"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;
char a[2];
char answer;
int g, i = 0, points = 0;
printf("This is a trivia game, choose from History, Geography, Sport or Technology nThere will be 5 random questions, and the user will have to enter the correct answer.nThe user will be scored out of 5 at the end. nPlease enter G, H, S, or T to choose which set of questions.");
scanf("%c", &y);
switch(y){
case 'H' :
while (i<5){
g = (rand() % (15 + 1 - 1) + 1);
a[1] = history[g][1];
a[2] = history[g][2];
printf("%c", a[1]);
printf("What is your answer");
scanf("%c", &answer);
if (answer == a[2]){
printf("Correct! 1 point");
points++;
} else {
printf("Incorrect! 0 points");
}
i++;
}
printf("You got %d points.", &points);
menu();
break;
case 'G' :
break;
case 'S' :
break;
case 'T' :
break;
default:
printf("That was an incorrect input.");
trivia();
return 0;

我希望这个程序能够构建,然后能够运行H案例,并根据我得到的正确答案给我打分。

就像在其Answer中提到的@usr一样,您可能最好使用指针数组来执行此任务。

以下是您的一个阵列示例:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#define GETLEN(array) sizeof array / sizeof array[ 0 ]
const char *separator   ( const size_t num );
int main( void )
{
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",
};
for ( size_t i = 0 ; i < GETLEN( history ) ; i++ )
{
printf("%s%sn", separator(i), history[i] );
}
}
const char *separator ( const size_t num )
{
if ( num % 2 == 0 )
{
return "Question:t";
}
return "Answer:tt";
}

输出:

Question:   Which English king was "mad"?
Answer:     George III
Question:   Who started the Protestant Reformation?
Answer:     Martin Luther
Question:   Who was the first person to see the moons of Jupiter?
Answer:     Galileo
Question:   What Viking group settled in France before conquering England, Sicily, and Malta?
Answer:     The Normans
Question:   What group sacked Baghdad in 1258, ending the Islamic Golden Age?
Answer:     The Mongols
Question:   Against what city did Rome fight the Punic Wars?
Answer:     Carthage
Question:   What yellow gas was famously used in WWI?
Answer:     Mustard Gas
Question:   What epic poem is thought to be the oldest in the English language?
Answer:     Beowulf
Question:   What ancient empire was led by Xerxes, Cyrus, and Darius?
Answer:     Persia
Question:   Who was the most notorious member of the Ba'ath Party?
Answer:     Saddam Hussein
Question:   What Italian adventurer wrote about his 24 year journey from Venice to China and back?
Answer:     Marco Polo
Question:   What young pharaoh's tomb was discovered in 1922?
Answer:     Tutankhamun
Question:   Before becoming king of England, what country was James I the king of?
Answer:     Scotland
Question:   What was the primary language of the Byzantine Empire?
Answer:     Greek
Question:   For what crime was Al Capone convicted of in 1931?
Answer:     Tax Evasion

您正在初始化一个带有字符串文字列表的char数组。您想要的是指针数组

例如:

char *history[15] = { "Which English king was "mad"?","George III",
"Who started the Protestant Reformation?","Martin Luther",
...

其他数组也是如此。

最新更新