輸入:如何從文本文件中獲取值並將其輸入到二維數組中? C++
Text File Values: 1 1 A 2 2 B 3 3 C 0 0 X
輸出:
3 TILES on Scrabble Board
ROW COL LETTER
=== === ======
1 1 A
2 2 B
3 3 C
=== === ======
4 x 4 SCRABBLE BOARD
1 2 3 4
+ - - - - +
row1=> | A |
row2=> | B |
row3=> | C |
row4=> | |
+ - - - - +
1 2 3 4
(1)存儲所有板字符到陣列
char Board[9][9]; // Capacity is 9x9 = 81.
RULE:板單元格可以包含至多一個值; 顯示錯誤時的消息: - 所述細胞已包含標記 - 該標記不是一個字母 - 行超出範圍[1,boardsize] - 鞍部在該範圍之外[1,boardsize]
ERROR: REJECTED CELL <row> <col> <symbol> CELL MARKED
ERROR: REJECTED CELL <row> <col> <symbol> NOT A LETTER
ERROR: REJECTED CELL <row> <col> <symbol> BAD ROW
ERROR: REJECTED CELL <row> <col> <symbol> BAD COL
(2)讀取輸入文件後,顯示電路板陣列。 (僅圖示)
(3)顯示在scrable板的話:
HORIZONTAL: xxxx yyyyyyy zzzzz 3 WORDS
VERTICAL: aaa bbb ccc ddd 4 WORDS
7 SCRABBLE WORDS
問題:如何輸入文件的值例如:11.一種成char板[9] [9]?我想11代表ROW和COL和符號A要與1 1
實例相關聯:
Board[1][1] = A
Board[2][2] = B
新代碼:
int main()
{
//-------------------------------------------- --------------------------
// Declare variables
//----------------------------------------------------------------------
char filename[80];
int boardsize, row, col;
char symbol;
char Board[9][9];
ifstream inF;
//-| ----------------------------------------------------------------------
//-| Print the copyright notice declaring authorship.
//-| ----------------------------------------------------------------------
cout << endl << "(c) 2017, twilson" << endl << endl;
//-| ----------------------------------------------------------------------
//-| 1. Get file name and open file.
//-| ----------------------------------------------------------------------
cout << "Enter name of input file: ";
cin >> filename;
cout << endl;
inF.open(filename);
if (inF.fail())
{
cout << "FATAL ERROR: Can not open file " << "'" << filename << "'" << endl;
exit(1);
}
//-| ----------------------------------------------------------------------
//-| 2. Get board size.
//-| ----------------------------------------------------------------------
cout << "Enter board size [1-9]: ";
cin >> boardsize;
cout << endl;
//-| ----------------------------------------------------------------------
//-3. Read in file values and output ROW, COL, and LETTER on scrabble board.
//-| ----------------------------------------------------------------------
int T = 0;
int nextLine = 0;
int Tiles = 0;
// Read in file and count each tile
int a = 0;
while(inF >> row >> col >> symbol)
{
if(row > 0 && col > 0)
{
if(row == row && col == col)
{
cout << "REJECTED CELL " << row << " " << col << " "
<< symbol << " CELL MARKED" << endl;
}
else if(!isalpha(symbol))
{
cout << "REJECTED CELL " << row << " " << col << " "
<< symbol << " NOT A LETTER" << endl;
}
else if(row > boardsize)
{
cout << "REJECTED CELL " << row << " " << col << " "
<< symbol << " BAD ROW" << endl;
}
else if(col > boardsize)
{
cout << "REJECTED CELL " << row << " " << col << " "
<< symbol << " BAD COL" << endl;
}
else
Tiles++;
}
}
你讀'INT行,詮釋山口,焦炭Val',驗證行和山坳,檢查電池是免費的,查瓦爾是一個字母。 (!inF.eof()) \t { \t \t inf >> row >> col> [1] [col-1] = Val' –
我的代碼到目前爲止: >符號; \t \t \t 如果\t(行> 0 && COL> 0) \t \t { \t \t \t如果(!因而isalpha(符號)) \t \t \t { \t \t \t \t COUT << 「REJECTED CELL」 <<行<< 「」 << COL << 「」 \t \t \t \t \t <<符號<<「NOT A LETTER「<< endl; \t \t \t} \t \t \t否則如果(行> boardsize) \t \t \t { \t \t \t \t COUT << 「REJECTED CELL」 <<行<< 「」 << COL << 「」 \t \t \t \t \t <<符號<< 「BAD ROW」 << ENDL; \t \t \t} \t \t \t否則如果(COL> boardsize) \t \t \t { \t \t \t \t COUT << 「REJECTED CELL」 <<行<< 「」 << COL << 「」 \t \t \t \t \t <<符號<< 「BAD COL」 << ENDL; \t \t \t} \t \t \t \t \t \t別的 \t \t \t \t瓷磚++; \t \t} –
把你的代碼放到你的問題中,並閱讀https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong或https://stackoverflow.com/a/ 5605159/8491726 –