C++ Programming Problem

Nessoldaccount

Senior member
Jun 4, 2000
483
0
0
apmatrix<char>world(5,5);

void inputWorld(apmatrix<char>&amp;world)
{
char var;

ifstream infile(&quot;world.txt&quot;);

while(infile >> var)
{
for(int r = 0; r < world.numrows(); r++)
for(int c = 0; c < world.numcols(); c++)
{
world[r][c] = var;
cout << var;
}
}
}

That is the function. When I do the cout << var; (as a test) I get each character like 25 times. ALso the only element stored in the matrix is the bottom-right of the text file which is repeated throughout the entire matrix.

world.txt

u.cde
fghij
klmno
pqrs.
yvw.b

EDIT: Putting the while loop inside the nested for-loops fixes some of the problems. I'll be back w/ more program problems soon.
 

FrogDog

Diamond Member
Jan 12, 2000
4,761
0
0
Damn I wish I knew more about C++, I'm just learning :(

Oh and here's a bump for you :)
 

Nessoldaccount

Senior member
Jun 4, 2000
483
0
0
here's the whole mess.

#include <iostream.h>
#include <apmatrix.h>
#include <apstring.h>
#include <fstream.h>

struct myPosition
{
int row;
int col;
int n, e, s, w;
};

void inputWorld(apmatrix<char>&amp;world);
void dispPosition(apmatrix<char>&amp;world, myPosition&amp;location);
void findExits(apmatrix<char>&amp;world, myPosition&amp;location);

int main()
{
apmatrix<char>world(5,5);
myPosition location;

inputWorld(world);
dispPosition(world, location);

return 0;
}

void inputWorld(apmatrix<char>&amp;world)
{
char var;

ifstream infile(&quot;world.txt&quot;);

for(int r = 0; r < world.numrows(); r++)
for(int c = 0; c < world.numcols(); c++)
while(infile >> var)
world[r][c] = var;
}

void dispPosition(apmatrix<char>&amp;world, myPosition&amp;location)
{
apstring roomNum;
char var;

location.row = 0;
location.col = 0;

roomNum = world[location.row][location.col];
roomNum += &quot;.txt&quot;;

ifstream infile(roomNum.c_str());

while(infile)
{
infile >> var;
cout << var;
}

cout << &quot;Obvious exits: &quot;;

// findExits(world, location);
}

void findExits(apmatrix<char>&amp;world, myPosition&amp;location)
{
if((location.col != world.numcols() - 1) &amp;&amp; (world[location.row][location.col + 1] != '.'))
{
location.w = 1;
cout << &quot;West, &quot;;
}
else location.w = 0;

if((location.col != 0) &amp;&amp; (world[location.row][location.col - 1] != '.'))
{
location.e = 1;
cout << &quot;East, &quot;;
}
else location.e = 0;

if((location.row != 0) &amp;&amp; (world[location.row - 1][location.col] != '.'))
{
location.s = 1;
cout << &quot;South, &quot;;
}
else location.s = 0;

if((location.row != world.numrows() - 1) &amp;&amp; (world[location.row + 1][location.col] != '.'))
{
location.n = 1;
cout << &quot;North, &quot;;
}
else location.n = 0;
}

work with it ;)
 

stomp

Senior member
Oct 9, 1999
769
0
0
I can tell you right off the bat why the matrix is filled with the last character:
... when you are inputing var, you then for/for to fill the entire matrix with that character. you also have the cout within the for/for loops (generating 5 columns * 5 rows = 25 occurrences of var to stdout).

What I think you want is something like:
...
char var;
int r=0, int c=0;
while (infile >> var) {
if (r < world.numrows() &amp;&amp; c < world.numcolumns() ) {
world[r][c] = var;
++r; ++c;
cout << var;
}
}


btw, the way you had it was O(n^3) :D

I never understood AP's thing about their crapmatrices... just use char * and malloc, one can never go wrong unless of course with Xt and you don't do an XtMalloc and for no damn reason you get a &quot;Bus Error&quot; instead of a standard seg fault :D

... ahhh, i miss those kind of debugging days. Now its find memory leaks caused by Motif... and create workarounds for our 6K line program spread over 100 files program that is &quot;mission critical DOD&quot; crap (which it isn't, but they say it is) blah blah.
 

stomp

Senior member
Oct 9, 1999
769
0
0
int r=0, c=0;
for(r; r < world.numrows(); ++r)
for(c; c < world.numrows(); ++c)
infile >> world[r][c];

.... end function.

Of course, if its a true array, you can probably have a pointer just wandering around
char *ptr;
int n = world.numrows() * world.numcols();
ptr = world;
while ((--n))
infile >> (*ptr)


i wonder if that'd work... weird.
 

stomp

Senior member
Oct 9, 1999
769
0
0
I'll get this right one of these days...


char var;
int r=0, int c=0;
while (infile >> var) {
if (var == '\n')
++r;
else {
world[r][c]=var;
++c;
}
}