尝试将字符分配给二维字符数组时,表示数组值的整数在 for 循环中递增
Interger representing a Array value is increasing within a for loop when trying to assign characters to a 2D Character array
在我的程序中,我试图从 .txt 文件输入字符并将它们分配给二维字符数组以形成迷宫。此代码的预期结果是:
xxxxxxx
xA...Bx
xxxxxxx
然而,当我认为在每个循环开始时 Column 应该为 0 时,我却被警告 Column 大于大小(定义为 30)。无论什么 Column 等于 Size 并且我不确定为什么。我已经包含了下面的代码。我是初级程序员,所以如果您有任何建议,请尽可能简单。
非常感谢,
本
#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
在 readMaze
中未初始化使用,因此程序具有未定义的行为。此外,cout << maze << endl;
通过越界读取使程序具有未定义的行为。
考虑将 maze
改成 std::vector<std::vector<char>>
甚至 std::vector<std::string>
以使其更简单。
示例:
#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();
}
在我的程序中,我试图从 .txt 文件输入字符并将它们分配给二维字符数组以形成迷宫。此代码的预期结果是:
xxxxxxx
xA...Bx
xxxxxxx
然而,当我认为在每个循环开始时 Column 应该为 0 时,我却被警告 Column 大于大小(定义为 30)。无论什么 Column 等于 Size 并且我不确定为什么。我已经包含了下面的代码。我是初级程序员,所以如果您有任何建议,请尽可能简单。 非常感谢, 本
#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
在 readMaze
中未初始化使用,因此程序具有未定义的行为。此外,cout << maze << endl;
通过越界读取使程序具有未定义的行为。
考虑将 maze
改成 std::vector<std::vector<char>>
甚至 std::vector<std::string>
以使其更简单。
示例:
#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();
}