gpt4 book ai didi

C++:为 ‘char [4]’ 数组错误获取太多初始化程序

转载 作者:行者123 更新时间:2023-11-30 02:32:03 27 4
gpt4 key购买 nike

当我尝试编译我正在处理的一些源代码时遇到此错误。我是编程的新手,出于某种原因,数组一直给我带来困难:

airline2.cpp:9:84: error: too many initializers for ‘char [4]’
char array_seats[][4] = {{'1', '2', '3', '4', '5', '6', '7'},{'A', 'B', 'C', 'D'}};

这是我的源代码:

#include <iostream>
using namespace std;

int main()
{
char row, letter, answer;
int letter2;

char array_seats[][4] = {{'1', '2', '3', '4', '5', '6', '7'},{'A', 'B', 'C', 'D'}};

for (int row = 0; row <= 6; row++)
{
for (int letter = 0; letter <= 4; letter++)
{
cout << array_seats[row][letter] << " ";
}
cout << endl;
}

do
{
cout << "Enter your desired seat row:\n";
cin >> row;
cout << "Enter a seat letter:\n";
cin >> letter;

if(letter == 'A' || letter == 'a')
letter2 = 1;
else if(letter == 'B' || letter == 'b')
letter2 = 2;
else if(letter == 'C' || letter == 'c')
letter2 = 3;
else if(letter == 'D' || letter == 'd')
letter2 = 4;

if(array_seats[row-1][letter2] == 'X')
cout << "Sorry, this seat is taken.\n" << endl;

else
{
array_seats[row-1][letter2] = 'X';
cout << "Seat reservation successful.\n" << endl;
}

for (int row = 0; row <= 6; row++)
{
for (int letter = 0; letter <= 4; letter++)
{
cout << array_seats[row][letter] << " ";
}
cout << endl;
}

cout << "Would you like to try again? (Y/N)\n";
cin >> answer;

}while (answer == 'Y' || answer == 'y');

cout << "Have a good flight!\n";

return 0;

}

我尝试将错误设置为 5 并在末尾添加“\0”,但似乎没有任何效果。有帮助吗?

最佳答案

char array_seats[][4] = {{'1', '2', '3', '4', '5', '6', '7'},{'A', 'B', 'C', 'D'}};

{'1', '2', '3', '4', '5', '6', '7'} 大于 4

关于C++:为 ‘char [4]’ 数组错误获取太多初始化程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36879548/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com