【问题标题】:Output not matching input C++输出与输入 C++ 不匹配
【发布时间】:2011-03-13 20:59:19
【问题描述】:

我正在尝试(无论如何现在),从数据文件中显示迷宫。第一组数字是大小,第二组数字是入口,第三组数字是出口坐标。

我有一个名为“MyMaze1.dat”的简单输入文件:

7 20 
0 18
6 12
****************** *
*     *      ***** *
* ***** ***        *
* ***** *****   ** *
* *             *  *
* *******  *       *
************ *******

我用来阅读这个迷宫的代码:

void MazeClass::ReadMaze(ifstream& mazedata){
    mazedata >> row >> column;
    GetExit(mazedata);
    GetEntrance(mazedata);
    maze= new char*[row];
    for (unsigned i=0; i<row;i++)
    {
        maze[i]=new char[column];
    }
    /*
        maze=new char[column];
        *maze[i]=new char[column];
    */
    for (int y=0;y<column;y++)
    {//Keeping the maze inside boundries (step 1)
        for (int x=0;x<row;x++)//(Step 2)
        {
            maze[x][y]=mazedata.get();
        }
    }
}

我用来显示迷宫的代码是:

void MazeClass::Display(){
    cout << "Entrance is: " << entx << ' ' << enty << endl;
    cout << "Exit is: " << exitx << ' ' << exity << endl;
    cout << "Maze is: " << endl;
    for (int y=0;y<column;y++)
    {
        for (int x=0;x<row;x++)
        {
            cout << maze[x][y];
        }
        cout << endl;
    }
}

输出如下:

Entrance is: 6 12
Exit is: 0 18
Maze is: 

******
*******
***** *

*     
*      
***** *

* ****
* ***  
      *

* ****
* *****
   ** *

* *   

   *  *

* ****
***  * 
      *

******
****** 

提前感谢您的所有帮助。

【问题讨论】:

    标签: c++ class io fstream dynamic-memory-allocation


    【解决方案1】:
    • 读取迷宫数据时,您不会跳过 EOL (\n)。
    • 你正在交换 x 和 y

    for (int y=0; y < rows; y++) // rows outer
    {
        for (int x=0;x < columns ;x++)// columns inner
        {
            maze[y][x]=mazedata.get(); // Swapped y and x
        }
        mazedata.get(); // skip \n
    }
    

    Display 需要相同的更改

    【讨论】:

    • 在对显示和输入循环应用更改后,它已修复。谢谢。
    【解决方案2】:

    在您的打印输出中,第一个循环应该是行,然后是列

    即,先做一行及其所有列,然后再做下一行……等等

    【讨论】:

    • 在打印输出 for 循环中交换行和列会导致 seg 错误,并且不显示任何内容。 >迷宫是:分段错误
    • 在应用 Erik 的修复和您的修复后,输出现在是正确的。谢谢。
    【解决方案3】:

    你的迷宫读数不太正确。

    // outer loop reads rows.
    for (int x=0;x<row;x++)
    {
        // Go across the columns first.
        for (int y=0;y<column;y++)
        {
            maze[x][y]=mazedata.get();
        }
        // After you have read all the data you need to skip the newline (and any blanks)
        mazedata.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
    

    显示路由也需要同样的调整。除非你想把迷宫印在侧面。

    void MazeClass::Display()
    {
        cout << "Entrance is: " << entx << ' ' << enty << endl;
        cout << "Exit is: " << exitx << ' ' << exity << endl;
        cout << "Maze is: " << endl;
    
        for (int x=0;x<row;x++)
        {
            for (int y=0;y<column;y++)
            {
                cout << maze[x][y];
            }
            cout << endl;
        }
        cout << endl;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-11-19
      • 1970-01-01
      • 2015-01-18
      • 1970-01-01
      • 1970-01-01
      • 2018-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多