-
February 24th, 2008, 11:43 PM
#1
Getting input from a file
So, I'm working on a project to solve a maze thats given to it by an input file. This is for school.
I am retarded and I can't make heads or tails of the groundwork that she provided for us (starting code).
Here it is:
Code:
#include <string>
typedef struct
{
short int doorEncoding; // Range 0..15.
// north = doorEncoding & 0x08
// east = doorEncoding & 0x04
// south = doorEncoding & 0x02
// west = doorEncoding & 0x01
// Note that the result of these operations is not
// necessarily 1, but some non-zero value if there
// is in that particular direction. 0 else.
bool visited;
int parentRow;
int parentCol;
char direction; // From parent.
} MazeCell;
typedef struct
{
int row;
int col;
} CellPosition; // useful as StackItem or QueueItem.
class Maze
{
public:
Maze(const string& fileName); // Load from file.
void Solve(); // Solve the maze.
void PrintSolution();
private:
vector<vector<MazeCell> > maze; // maze square
int rows; // number of rows
int cols; // number of columns
int mouseRow; // row position of mouse
int mouseCol; // col position of mouse
int cheeseRow; // row position of cheese
int cheeseCol; // col position of cheese
int squaresVisited;
};
Code:
Maze::Maze(const string& fileName):
maze(0), // init maze
rows(0),
cols(0),
mouseRow(0),
mouseCol(0),
cheeseRow(0),
cheeseCol(0),
squaresVisited(0)
{
// declare variable to open file etc.
// read m and n and init rows and cols.
// read and init mouse and cheese positions.
// Now we know the size of maze. let us init.
// Reserve space for rows (= n) many rows.
maze.resize(n);
// reserve space for cols (= n) many columns in each row.
for (int rowNum = 0; rowNum < rows; rowNum++)
{
maze[rowNum].resize(cols);
}
// Now we can use maze[0][0] ... maze[n-1][m-1].
// Read and initialize all the cells.
}
What I'm confused on is the last part
Specifically, what does this do?
Code:
maze(0), // init maze
rows(0),
cols(0),
mouseRow(0),
mouseCol(0),
cheeseRow(0),
cheeseCol(0),
squaresVisited(0)
Does this set everything to the default value of 0 to start off with?
And this...
Code:
ifstream ("myfile.txt")
std::cin >> m;
What is this doing? Anything? As far as I can tell myfile doesn't actually point to anything yet and std::cin >> m; is trying to get input from the user right?
I need to know this before I start
-
February 25th, 2008, 09:01 AM
#2
Re: Getting input from a file
Yes, the code you were asking about initializes the variables of that class.
You can only do that in the constructor though.
That is a kind of pre-initialization that occurs before the first line of the function (the constructor) is executed.
Use it if you want - I still like to initialize variables the old C way - through direct assignment, but this has its uses too, as you will see as you use it.
I hope this helps.
-
February 25th, 2008, 09:15 AM
#3
Re: Getting input from a file
As for the other piece of code:
[CODE]
ifstream ("myfile.txt")
std::cin >> m;
[\CODE]
This doesn't do anything.
This won't even compile.
I think you left out some necessary code.
I can tell you how to actually make this work and what it should do.
ifstream stands for Input File Stream - it opens a stream capable of reading input from the file of your choice - in this case "myfile.txt"
However, this is not the correct syntax to use it.
You need to declare an ifstream variable to read into, like this:
[CODE]
ifstream in("myfile.txt", ios::in);
[\CODE]
Then to read into the variable like this:
[CODE]
std::string a;
in >> a;
[\CODE]
And yes, cin gets the input from the user.
Let me know if you are still confused.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|