PDA

View Full Version : C++, Two Dimensional Arrays



Latrinsorm
04-22-2004, 06:30 PM
are starting to piss me off. I get this message:

error C2440: '=' : cannot convert from 'char [2]' to 'char [100]'

when I try to write to a matrix. The function thing looks like this "void check (char maze[][100],int &x,int &y)" so I know where the 100 is coming from, the 2 beats the hell out of me, and it won't let me not put a number in for the subscript. If I put 2 in, I get "error C2106: '=' : left operand must be l-value", which normally means switch them around, but the same error happens with them switched. Finally, if I put the write to matrix jigger in single quotes, I get "error C2440: '=' : cannot convert from 'char' to 'char [2]'".

So yeah. Help plz?

imported_Kranar
04-22-2004, 06:46 PM
You'll have to show the entire function, and more specifically where that error is occuring.

Latrinsorm
04-22-2004, 06:54 PM
int main()
{
char maze[100][100];
int row, col, x, y;
ifstream input;
ofstream output;
input.open("maze.txt");
input >> row >> col;
for (x=0;x < row;x++){
for (y=0;y < col;y++){
input.get(maze[x][y]);
}
}
x = 1;
y = 1;
while ((x < row-1) && (y < col - 1)){
check(maze,x,y);
}
}

void check (char maze[][100],int &x,int &y)
{
maze[x,y] = '.';
}

There's some other stuff, but the compiler craps out on the first line of void check. I'm reasonably sure I did some stuff wrong in main, but I can figure that out when I can run it.

edit: is there a way to make that look nice?

[Edited on 4-22-2004 by Latrinsorm]

TheEschaton
04-22-2004, 07:00 PM
There's no pretty printing in the PC!

Anyways, on to your problem, I don't know exactly how C++ does its syntax, but I'm almost positive the error is probably syntax. For "check", you want the input to be a maze of an open-ended amount in the first array, and 100 in the second array?

-TheE-

imported_Kranar
04-22-2004, 07:01 PM
<< edit: is there a way to make that look nice? >>

Use the [ code ] feature.



void check (char maze[][100],int &x,int &y) {
maze[x][y] = '.';
}


That will get rid of the compiler error.

FinisWolf
04-22-2004, 07:07 PM
:puke:

<--- Hates programming with a passion!

Finiswolf

imported_Kranar
04-22-2004, 07:12 PM
One other point of advice:

void check (char maze[][100],int &x,int &y)

You are passing x and y as int references, but check never modifies them and according to the logic of your program check will never need to modify them. It is thus in your interest then to do either this:

void check (char maze[][100], const int &x, cont int &y)

or this:

void check (char maze[][100], int x, int y)

Passing in references without the const modifier is in general... dangerous.

Latrinsorm
04-22-2004, 07:55 PM
Kranar == the king.

I modify x and y later on.

E: you were right.

Finis: It's the little things... that make you want to rip your brain out and kick it once you figure them out or they are revealed to you. Oyes.

Fengus
04-23-2004, 02:56 AM
Originally posted by Latrinsorm


x = 1;
y = 1;
while ((x < row-1) && (y < col - 1)){
check(maze,x,y);
}



More advice, you know your start and end points for the loop, so use a for, more efficient.



for(x = 1, y = 1; x < row-1 && y < col - 1; oh btw you forget your increments?)
check(maze, x, y);


Which is another nice thing, it becomes more obvious when you've forgotten to increment your loop variables. And also you know this will always happen last which is another common loop bug. (ie copy-paste after the increment accidentially in a while/do and mess your loop up)

This might be why you are passing references to check(), I would recommend against incrementing your indexs there. Not sure why you even have that function, but maybe its required by your teacher.

[Edited on 4-23-2004 by Fengus]

Fengus
04-23-2004, 02:57 AM
Why is code double spaced?? Ugg thats awful.

Latrinsorm
04-23-2004, 01:21 PM
Originally posted by Fengus
but maybe its required by your teacher.
You are correct. Not only that, they won't let us use global variables either. It's so annoying. And you also got the part where I increment the loop variables in the other program. The reason is I wrote it like a sane person would do it first (as one big program) and then just cut and pasted subroutines (or whatever they're called). Interchangeable parts are nice and all, but I still don't see the purpose.

Another random thing they want me to do is have the user input a string, then use that string as the argument of an ifstream.open thing, which of course I don't know how to do and the (in my mind) logical idea of simply putting ifstream.open(string) doesn't work. Of course, how one would do that is left out of the lectures and the book (58 bucks down the drain for that POS) so I guess we have to deduce how to do it.

Of course, I wouldn't mind getting a hint from anyone here. :saint:

Jonty
04-23-2004, 01:30 PM
Originally posted by Latrinsorm
Interchangeable parts are nice and all, but I still don't see the purpose.

It's more organized and easy to understand and debug. I used to think the same thing you do until I started writing bigger programs. They're just giving you practice even though it's pointless in the programs you're currently writing.


Of course, how one would do that is left out of the lectures and the book (58 bucks down the drain for that POS) so I guess we have to deduce how to do it.

Don't decue, google. That's what I always did because school text books suck.

Sorry, but I won't be much help. I've very dusty on my C++(even though it was my favorite language when I went to school), and I'm not going to google it for you to find out.

Latrinsorm
04-23-2004, 01:42 PM
Originally posted by Jonty
Don't decue, google. That's what I always did because school text books suck.Again, why didn't I think of that?

Sure enough, Creighton U. saves the day. :w00t:

TheEschaton
04-23-2004, 04:02 PM
You go to Creighton Latrin? <ponders> Then how do you get the Daily Show when I do?


Hmmmmmmm


-TheE-

Latrinsorm
04-23-2004, 04:19 PM
Originally posted by TheEschaton
You go to Creighton Latrin?I go to their website. Specifically a .pdf file that was very helpful.