c - 设置二维字符数组时如何修复 "initializer-string for array of chars is too long"和 "excess elements in char array initializer"

标签 c

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

我曾尝试更改数组名称右侧的括号(例如 [15][2]、[15]、无括号),并查看了一些对我不太适用的 stackoverflow 问题问题。在 switch 语句的“H”情况下,我也收到“下标值既不是数组也不是指针也不是 vector ”错误。在加载过程中,我还收到了几个“围绕标量初始值设定项的大括号”、“标量初始值设定项中的多余元素”和“从指针生成整数而不进行强制转换 [-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 案例,并根据我得到的正确答案数量给我打分。

最佳答案

就像@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%s\n", separator(i), history[i] );
    }
}

const char *separator ( const size_t num )
{
    if ( num % 2 == 0 )
    {
        return "Question:\t";
    }
    return "Answer:\t\t";
}

输出:

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

关于c - 设置二维字符数组时如何修复 "initializer-string for array of chars is too long"和 "excess elements in char array initializer",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54027043/

相关文章:

c - 使用 C 预处理器在参数名称中加星号

c - rtp中的媒体如何同步?

c - 如何修复段错误 11 错误?

c - 如何按照其他代码在 Visual Studio C89 中声明可变长度数组

c - 如何释放 C 中的 union 数组?

c - 使用 CTRL+C 终止程序后运行某些内容

编译器找不到已安装的库头文件

c - 在 C 中使用清理属性释放二维数组的通用函数

c - STM32 - 从多个任务中读取 I/O

c - 尝试找到 middel 但程序崩溃了 c 中带有两个指针的链表