-
Ok i have anouther question. This one stumps me. Does the fstream fuctions "file.open(file.txt)" not work with #define? I keep getting 98 errors and then i took away the #define stuff and only errors i got where ones i would expect from values not being identified. Whats going on?
-
A #define will work simular in function to a variable that you cannot change. Your compiler will change all occurances in your code from 'SizeOfMazeX' to '10' before it compiles your code. Its just an easy way to keep track of things. Without the define, if we were to change the size of the maze we would have to change all the for...loops as well as the definition of the maze array. This makes life easier (and safer).
You shouldnt be getting any errors... Unless your trying to change the value of SizeOfMazeX or SizeOfMazeY? Possibly by reading to them from a file?
-
ok file is 400 characters long.....meaning 20x20. I already set the size of maxX to 20 and size of maxY to 20. yet i still get error messages. I dont get it.
-
Could you post your updated code in full?
-
C:\Program Files\Microsoft Visual Studio\MyProjects\Project 2\Main.cpp(29) : error C2039: 'Maze' : is not a member of 'basic_ifstream<char,struct std::char_traits<char> >'
thats one of them.
-
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;
#define maxX 20;
#define maxY 20;
#define border 0;
#define open 1;
#define blocked 2;
#define finish 3;
char mouseX = 0;
char mouseY = 0;
char maze[maxX][maxY];
ifstream InMaze;
ofstream Maze;
int main (void)
{
InMaze.open("maze.txt");
Maze.open("result.txt");
int x, y;
if (maze[x][y] == 'x')
maze[x][y] = border;
else if (maze[x][y] == 'o')
maze[x][y] = open;
else if (maze[x][y] == 'm')
{
mouseX = x;
mouseY = y;
maze[x][y] = open;
}
else if (maze[x][y] == 'c')
maze[x][y] = finish;
do
{
if (maze[mouseX][mouseY-1] != blocked)
mouseY --;
else if (maze[mouseX][mouseY+1] != border)
mouseY ++;
else if (maze[mouseX-1][mouseY] != border)
mouseX --;
else if (maze[mouseX+1][mouseY] != border)
mouseX ++;
}while (maze[mouseX][mouseY] != blocked);
return 0;
}
/*
void ****()
{
int x, y;
for (x = 0; x < maxX; x++)
{
for (y = 0; y < maxY; y++)
{
InMaze >> maze[x][y];
}
}*/
-
im thinking visual studio has been running to long. Mabe I should restart it. I dunno.
-
Right away I notice something. #defines are not terminated with a semi-colan.
This:
#define maxX 20; //<--- Semi-Colan = bad
should be this:
#define maxX 20
That should solve it... Works now?
P.S. I'm having the occasional problem connecting to cprogramming.com right now.... not sure why. Replies may become delayed or cease completly. This submission has failed 3 times already... waiting.... still waiting... we'll see what happens...
-
I now have 80 errors. It went down 18 error messages.
-
its saying that damn neer every varable is not a member of the ifstream char, struct, and all kinds of other stuff.
-
They all relate to your use of iostream:
ifstream InMaze;
ofstream Maze;
InMaze.open("maze.txt");
Maze.open("result.txt");
I dont have time to explain all that stuff, I'm sorry. Its 12:30 here and I'm one of those poor SOBs that have to get up at 6:30 and go to work. You might want to look up info on file I/O. Check out fopen, fread, and fwrite for (In my opinion) a superior method of file I/O... unless as I suspect, you're required to use iostream... Then look for an iostream tutorial (i'll try to find you one).
Also, you cant do this:
int x, y;
if (maze[x][y] == 'x')
maze[x][y] = border;
else if (maze[x][y] == 'o')
maze[x][y] = open;
else if (maze[x][y] == 'm')
x and y aren't set to anything so we dont know what element of the array we're accessing. It will crash. At least do this:
int x = 0, y = 0;
but I think you want to reset _all_ the maze so you need to loop through it like before:
for (x = 0; x < SizeOfMazeX; x++)
and all that stuff.
-
A couple of tutorials on iostream on this page:
http://www.programmersheaven.com/zone3/cat34/
scroll down to find them. Dont know if they're good or not, I just did a search.
/* Edit */
I'm afraid I must retire. I'll help again tomorrow if you still require it then. Good luck.
-
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#define maxX 20
#define maxY 20
void readin();
void maze();
ifstream InFile;
ofstream Outfile;
char mazey[maxX][maxY];
int main (void)
{
cout << "This is my maze program!" << endl;
cout << "All information will be printed to the result.txt file" << endl;
readin();
maze();
return 0;
}
void readin()
{
InFile.open("maze.txt");
Outfile.open("result.txt");
}
void maze()
{
int x = 0;
int y = 0;
char open=1;
char border=0;
char cheese=3;
char mouseX=0;
char mouseY=0;
for (x = 0; x < maxX; x++)
{
for (y = 0; y < maxY; y++)
{
InFile >> mazey[x][y];
if (mazey[x][y]=='x')
mazey[x][y] = border;
else
if (mazey[x][y]=='o')
mazey[x][y] = open;
else
if (mazey[x][y]=='m')
{
mouseX = x;
mouseY = y;
mazey[x][y] = open;
}
else
if (mazey[x][y]=='c')
mazey[x][y] = 0;
do
{
if (mazey[mouseX][mouseY-1] != border)
{
mouseY--;
Outfile <<mouseY;
}
else
if (mazey[mouseX][mouseY+1] != border)
{
mouseY++;
Outfile << mouseY;
}
else
if (mazey[mouseX-1][mouseY] != border)
{
mouseX--;
Outfile << mouseX;
}
else
if (mazey[mouseX+1][mouseY] != border)
{
mouseX++;
Outfile << mouseX;
}
}while (mazey[mouseX][mouseY] != 0);
}
}
}
can anyone help me with this? Any one know whats wrong with this code?
-
Plenty. First off, whats this:
if (mazey[x][y]=='c')
mazey[x][y] = 0;
You're assigning this element a value of 0 which is the same value as your border. You are, in effect, saying
if (mazey[x][y]=='c')
mazey[x][y] = border;
Which we know is bogus since 'x' is your borders. Change that to = cheese.
Second, you're closing braces for your for...loops are in the wrong place.
}while (mazey[mouseX][mouseY] != 0);
} //<----- Move these guys up to
} //<----- just above the do statement.
Third; did you check out those tutorials on iostream? Were they any good? I'm just on a break here and only have a few seconds, so I'm afraid I still cant dedicate much time, sorry...
-
Um na those tutorials where'nt as good as my book. So i looked that stuff up. that maze[x][y]=0 was a typo. I forgot to take it out, i meant cheese. and where should have the loop been?