输入:如何从文本文件中获取值并将其输入到二维数组中? 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 –