如何从文本文件中获取值并输入到二维数组中? C++

Posted

技术标签:

【中文标题】如何从文本文件中获取值并输入到二维数组中? C++【英文标题】:How do I take values from text file and input into a 2D array? c++ 【发布时间】:2017-09-17 04:28:44 【问题描述】:

输入:

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.

规则:一个板单元最多可以包含一个值; 在以下情况下显示错误消息: - 单元格已经包含一个标记 - 标记不是字母 - 该行超出范围 [1,boardsize] - col 超出范围 [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) 读取输入文件后,显示 Board 数组。

(3) 在拼字板上显示文字(仅作图示):

HORIZONTAL: xxxx yyyyyyy zzzzz 3 WORDS
VERTICAL:   aaa bbb ccc ddd  4 WORDS
7 SCRABBLE WORDS

问题:如何将文件值例如:1 1 A 输入 char Board[9][9]?我希望 1 1 代表 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 row, int col, char Val,验证行和列,检查单元格是否空闲,检查 Val 是一个字母。如果一切正常,您分配Board[row-1][col-1] = Val 到目前为止我的代码:while(!inF.eof()) inF >> row >> col >> symbol; if(row > 0 && col > 0) if(!isalpha(symbol)) cout boardsize) cout boardsize) cout 将您的代码放入您的问题中并阅读***.com/questions/5431941/… 或此***.com/a/5605159/8491726 谢谢。另外,我将代码添加到问题中并更改了 while 循环。 【参考方案1】:

在第 2 项和第 3 项之间,您必须初始化您的板(这样您以后可以检查该地点是否已标记

    for (int i = 0; i < boardsize; i++)
        for (int j = 0; j < boardsize; j++)
            Board[i][j] = ' ';

您的 while 循环应如下所示(实际错误打印已删除 - 只剩下条件)

    while (inF >> row >> col >> symbol)
    
        if(row == 0 && col == 0 && symbol == 'X')
            //end condition? - not clear from your explanation
            break;
        
        if (row < 1 || row >  boardsize)
        
            // BAD ROW error
        
        else if (col <1 || col > boardsize)
        
            // BAD COL error
        
        else if (!isalpha(symbol)) 
            //NOT A LETTER error
        
        else if (Board[row - 1][col - 1] != ' ')  //This check requires the Board to be properly initialized
        
            //CELL MARKED error
        
        else 
            //All is good
            Board[row - 1][col - 1] = symbol;
            Tiles++;
        
    

【讨论】:

非常感谢!

以上是关于如何从文本文件中获取值并输入到二维数组中? C++的主要内容,如果未能解决你的问题,请参考以下文章

C#,抛出错误试图从二维数组中获取值并在 TryParse 中使用它

如何从文本字段中获取值并读取按键事件

在C++中如何读取文本中的数据存储为数组变量?

如何从仅使用空格作为分隔符的文件中获取要写入的二维数组

c++获取文本框的值

如何从 .t​​xt 中提取文本并将其存储到动态二维数组中?