想知道是否可以在2維數組中打印行和列的名稱?我所擁有的是一個有13行的座位計劃,名爲Row1 - > Row13,列中有字母A - > F ...我可以獲得座位計劃以顯示沒有問題,但我想打印出行和列名也。以下是我希望輸出如何的例子。C++打印2維數組中的行和列名稱
Seat A B C D E F
Row1
Row2
等等。我現在只使用2行,直到我可以獲得所需的輸出。謝謝。
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
const int num_of_rows = 13;
const int num_of_col = 6;
enum row { Row1, Row2, Row3, Row4, Row5, Row6, Row7, Row8, Row9, Row10, Row11, Row12, Row13 };
enum seat { A, B, C, D, E, F };
char seatingPlan[num_of_rows][num_of_col];
int main()
{
int row;
char seatingPlan[13][6];
seatingPlan[Row1][A] = '*';
seatingPlan[Row1][B] = '*';
seatingPlan[Row1][C] = 'X';
seatingPlan[Row1][D] = '*';
seatingPlan[Row1][E] = 'X';
seatingPlan[Row1][F] = 'X';
seatingPlan[Row2][A] = '*';
seatingPlan[Row2][B] = 'X';
seatingPlan[Row2][C] = '*';
seatingPlan[Row2][D] = 'X';
seatingPlan[Row2][E] = '*';
seatingPlan[Row2][F] = 'X';
for (row = 0; row < num_of_rows; row++)
{
for (seat = 0; seat < num_of_col; seat++)
cout << setw(5) << seatingPlan[row][seat];
cout << endl;
}
system("pause");
return 0;
}
謝謝山姆。我理解了第一部分關於擺脫枚舉並將其改爲整數。得到了改變。第二部分關於創建一個函數來切換整個人類可讀性的麻煩。我是一名學生,他們給我們的課本相當無用,我告訴學校。如果你可以請開始我的功能,我將不勝感激。每次我嘗試一些東西時,我會收到一個錯誤消息:錯誤C2015:常量中有太多字符 – Raj
這是一個提示。如果「座位」是座位列索引,那麼您認爲「A'+座位」這個表達能爲您帶來什麼?你無法真正期望任何教科書能教你如何邏輯思考。一本教科書會給你的是語言的語法和描述。就邏輯思維而言,這取決於你。 –