[英]Interger representing a Array value is increasing within a for loop when trying to assign characters to a 2D Character array
in my program I am attempting to input characters from a.txt file and assign them to a 2D Char array to form a Maze.在我的程序中,我试图从 a.txt 文件中输入字符并将它们分配给 2D Char 数组以形成迷宫。 The expected outcome of this code is:此代码的预期结果是:
xxxxxxx
xA...Bx
xxxxxxx
However I am instead warned that Column is greater than size (defined as 30) when I believe it should be 0 at the start of every loop.但是,当我认为在每个循环开始时它应该是 0 时,我反而被警告说 Column 大于 size (定义为 30)。 No matter what Column is equal to Size and im not sure why.无论什么 Column 等于 Size 并且我不知道为什么。 I've included the code below.我已经包含了下面的代码。 I am beginner programmer so if you have any advice could you please make it as simple as possible.我是初学者程序员,所以如果您有任何建议,请尽可能简单。 Many thanks, Ben非常感谢,本
#include<fstream>
#include<iostream>
#include<string>
#include <vector>
//Maze Size
#define SIZE 30
using namespace std;
void readMaze(string fileName);
void inputMaze();
int main()
{
inputMaze();
}
void readMaze(string fileName)
{
int rows;
int columns = 0;
//vector<vector<char>> maze;
char maze[SIZE][SIZE];
ifstream input(fileName);
char data;
while (input.get(data)) //While loop used to store each individual data to the string.
{
for (int rows = 0; rows < 20; rows++)
{
columns = 0;
while (data != '\n')
{
if (rows > SIZE)
{
cout << "ROWS GREATER THAN SIZE";
break;
}
else if (columns > SIZE)
{
cout << "COLUMNS GREATER THAN SIZE";
break;
}
else
{
maze[rows][columns] = data;
columns++;
data = input.get();
}
}
data = input.get();
}
}
cout << "The Maze being solved is: " << endl;
cout << maze << endl;
input.close();
}
void inputMaze()
{
string userinput;
cout << "Plese input a .txt file name" << endl;
cin >> userinput; //User inputs the name of a .txt file --> goes to readMaze()
readMaze(userinput);
}
rows
is used uninitialized in readMaze
so the program has undefined behavior. rows
在readMaze
中未初始化使用,因此程序具有未定义的行为。 Also, cout << maze << endl;
还有, cout << maze << endl;
makes the program have undefined behavior by reading out of bounds.通过读取越界使程序具有未定义的行为。
Consider making maze
a std::vector<std::vector<char>>
or even a std::vector<std::string>
to make it simpler.考虑将maze
设置为std::vector<std::vector<char>>
甚至std::vector<std::string>
以使其更简单。
Example:例子:
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
std::vector<std::string> readMaze(std::istream& input) {
std::vector<std::string> maze;
std::string line;
while(std::getline(input, line)) { // read a complete line at a time
maze.push_back(line); // and save it in the vector
}
return maze;
}
void inputMaze() {
std::string userinput;
std::cout << "Plese input a .txt file name\n";
if(std::cin >> userinput) {
std::ifstream is(userinput);
if(is) {
auto maze = readMaze(is);
std::cout << "The Maze being solved is:\n";
std::copy(maze.begin(), maze.end(),
std::ostream_iterator<std::string>(std::cout, "\n"));
}
// the file will be closed automatically when "is" goes out of scope
}
}
int main() {
inputMaze();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.