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)

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

... 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;
}
}
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |