How to display from a class?

Pages: 12
The issue I'm having is placing elements from a text file into a 2D Array in a class and then displaying from main, I'm not sure how to do this part:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class dCoords
{
	int i, j;
	int mArray[1][1];
public:
	void display(int i, int j);
	void Coords(int i, int j);
};

void dCoords::Coords(int i, int j)
{

}
void dCoords::display(int i, int j)
{
	ifstream infile;
	infile.open("dCoords.txt");
	if (!infile)
	{
		cout << "Cannot open File";
		exit(1);
	}
	while (!infile.eof())
	{
		for (int i = 0; i < 10; i++)
		{
			for (int j = 0; j< 10; j++)
			{
				infile >> mArray[i][j];
			}
		}
	}
	infile.close();

}

int main
{
   dCoords display(); ////This Part I'm not sure about, I did have it as dCoords.display(); but that gave an error
}
Your first try was closer to the mark than what you've got now. But there are still a couple of things wrong with it.

Firstly, a class is a type - equivalent to, say, int or double. You've defined a type called dCoords. Now, you need to actually create an object of that type, in the same way that you'd create any other variable. You then perform operations on that object.

It's exactly the same as you've already done on line 16. ifstream is a class. infile is the object of type ifstream.

Also, look at the way you've defined your display method:

void display(int i, int j);

You've defined it to take two arguments. You need to actually pass arguments to the method, just as you would any other function.

Last edited on
So its would be:

void display(int &i, int &j);?


as for the type is it just:

dCoords displayAll(int &I, int &j)

Huh? I didn't say anything about changing the definition of the function, or the name of it. I was saying that when you call the function, you actually need to pass some arguments to it.

Actually, now that I look more closely at the definition of display, I don't understand why you've defined it to take arguments at all. Why are you passing in i and j?

EDIT:

Also, why are you looping as if it were a 10 x 10 array, when you've only defined it to be 1 x 1 (see line 4)?
Last edited on
OP: looping on eof() is a bad idea - http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong - and line 33 is redundant - infile will be close once it goes out of scope after line 35
m not sure what you mean by which arguments I've passed.

Ok, would it help if I explained what I'm trying to do?


So I have a map of coordinates, all of which have a cost (white = 1, green= 1, blue= 2, wall = 0) using A* I am trying to make my way through it using a Manhattan Distance as a heuristic and output the path its chosen as text file.

Right now, I just want to display text files and assign them to be used later
First off, your display() function is poorly named. It doesn't display anything. It loads the array.
A better name would be load_array() or Read().

Line 3: Why are i, j member variables? They're not used.

Line 4: How big can your array be? As MikeyBoy said, you've defined it as 1x1. Any attempt to read more than 1 value into it is going to cause an out of bounds reference.

Line 7: Is this supposed to be a constructor? If so, the name is wrong. It should be dCoords.
In any case, it doesn't do anything.

Line 14: Why are i and j passed as arguments. They're not used.

Line 23: Not clear why you're looping on eof() (which is a bad idea anyway). That implies you have more than one 10x10 array in the file. If that's the case you're going to cause an out of bounds reference even if you define the array as 10x10.

Lines 25,27: As previously mentioned, this is going to cause an out of bounds reference for a 1x1 array.

Line 39: This should be:
1
2
  dCoords  obj;
  obj.display ();  // Again this is poorly named 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;

class dCoords
{   int mArray[10][10];
public:
	void LoadFile();
};

//  Read a 10x10 array from a data file
void dCoords::LoadFile ()
{   ifstream infile ("dCoords.txt");
	if (!infile)
	{   cout << "Cannot open File";
		exit(1);
	}
	for (int i = 0; i < 10; i++)
	{   for (int j = 0; j< 10; j++)
		{   infile >> mArray[i][j];
		}
	}
}

int main
{   dCoords obj;
    
    obj.LoadFile (); 
}






Ah ok, thanks for going through that, it really helps


The array is 1x1 ( the text file only contains 2 coordinates, I have 2 other files that are 10x10)

I will be changing the display part to a more suitable name, I figured it may help here in describing my issue
Progress!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void dCoords::LoadFile()
{
	ifstream infile("dCoords.txt");
	if (!infile)
	{
		cout << "Cannot open File";
		exit(1);
	}
	for (int i = 0; i < 1; i++)
	{
		for (int j = 0; j< 1; j++)
		{
			infile >> mArray[i][j];
			cout << i << j << endl;
		}
	}
}


This outputs: 0, 0, it should be the first two elements in the text file though
Last edited on
It's outputting 0 and 0, because the numbers you've told it to output are i and j. Those are the indices you're using to access array elements, and since your array only has one element, of course they will be zero.

There's clearly something you're misunderstanding. You say you want to display two elements, but you've defined your array to be 1 x 1, which means it only holds one element. But since you've resolutely ignored every single question I've asked you so far in this thread - and, for that matter, every single question anyone else has asked you in this thread - it seems rather futile to spend any more effort trying to find out what exactly it is you don't understand.
I've not ignored any questions mate, I'm trying to get my head around it myself, give me a break, its not for the lack of trying.


So, I want to display 2 elements in a 2D array, (in the txt file these numbers are 3 and 4) now I'm assuming that i is 1 element and j is the other element, so that the output should be "Coordinates are: 3 4" now if I change them in the txt file (outside of the program completely) to 2 different numbers, the output should reflect that.

now fro some updated code that I've been working on:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class dCoords
{
	int dArray[1][1];
public:
	void LoadFile();
};


void dCoords::LoadFile()
{
	ifstream infile("dCoords.txt");
	if (!infile)
	{
		cout << "Cannot open File";
		exit(1);
	}
	for (int i = 0; i < 2; i++)
	{
		for (int j = 0; j< 2; j++)
		{
			infile >> dArray[i][j];
			cout << dArray[i][j]<< endl;
		}
	}
}


This is working for me, but when I try with a different txt file (this one is 10x10) and change the for loops to reflect that its now giving me just the same memory address 100 times
Last edited on
I've not ignored any questions mate

How else would you describe not answering a single one of them?

So, I want to display 2 elements in a 2D array, (in the txt file these numbers are 3 and 4)

Then you need to make your array big enough to hold two elements. Right now, you've made it a 1x1 array, i.e. a single element. Think of it as a table with one row and one column. How many cells would be in that table?

now I'm assuming that i is 1 element and j is the other element

No. i and j are indices into your array, so that you can access the elements in it.

In this case, the only element in your array is dArray[0][0].

This is working for me

If so, that's only be sheer chance, because you're writing into memory beyond that which you've allocated for your array - because, as several people have told you already, you've only made your array big enough to hold one element.

Why are you making it a 2-dimensional array anyway? What's the need for two dimensions?
Last edited on
Which ones?

I realized that passing references wasn't needed so that kinda answered itself,

I explained what I was trying to do, so your question of why I was looping 10x10 was a typo on my part, I do need a 10x10 loop later on a different file, so it was accidently left in.

As for AbstractionAnon's questions, I read those as statements to think about rather than questions to be answered.

If I've offended by not answering questions straight away I apologise and wish to move on, hopefully this clears things up a bit

The need fro a 2D array is to hold a set of coordinates (its for an implementation of A* for AI pathfinding, these are the start and end goals)

I would need 4 cells in this case so 1 row/col is the start goal and the other row col is the end goal

I admit that this one is working by chance, cause when I try and expand it to a 10x10 array I get nothing but the same memory address 100 times
folks: sorry for jumping in late but, out of curiousity, SCB3 could you show us how the input file actually looks like just to be sure that we're all talking abt the same thing? Thanks
For the record, questions you didn't answer:

MikeyBoy wrote:
Why are you passing in i and j?
(also, AbstractionAnon asked the same question)
AbstractionAnon wrote:
Why are i, j member variables?
Is this supposed to be a constructor?


SCB3 wrote:
As for AbstractionAnon's questions, I read those as statements to think about rather than questions to be answered.

I can't speak for anyone but myself, but when I ask questions like that, it's not some kind of rhetorical device. It's because, by seeing your answer to the question, it helps us discover just what it is you're trying to achieve, and exactly what it is that you might have misunderstood.

I would need 4 cells in this case so 1 row/col is the start goal and the other row col is the end goal

So, firstly, you need it to be a 2 x 2 array, not a 1 x 1 array.

If you do it that way, you need to be very clear on whether it's the "row" that represents the point, and the numbers within that row that represent the co-ordinates within that point, or vice versa. In other words, are the co-ordinates of the first point:

(dArray[0][0], dArray[0][1])

or are they:

(dArray[0][0], dArray[1][0])

This is an easy way to get confused and make mistakes. You might be better off defining a simple class or struct for a point:

1
2
3
4
5
struct Point
{
  int x;
  int y;
};

and then making dArray a one-dimensional array of Points. That would certainly make your code easier to understand, and would make it less likely that you'd make a mistake.
Last edited on
oh in that case, i and j are supposed to represent the numbers in the array so that for example:

i = dArray[10][10]

and

j = dArray[10][10]


this is how I though I could access them later via another Iterator to add them to a Priority Queue.

If you do it that way, you need to be very clear on whether it's the "row" that represents the point, and the numbers within that row that represent the co-ordinates within that point, or vice versa. In other words, are the co-ordinates of the first point:

(dArray[0][0], dArray[0][1])

or are they:

(dArray[0][0], dArray[1][0])


I'm entirely sure what you mean by this, is it like this?

element 1 (first Co-Ordinate)
dArray[0][0]

element 2 (Next Co-Ordinate)
dArray [1][1]

because thats how I think its set up, or at least how I want it to be
Last edited on
MikeyBoy wrote:
How else would you describe not answering a single one of them?


Maybe the OP is trolling?

I am amazed that the respondents have had so much patience with the obfuscation, lack of answers, requests for needing a break.

Maybe this is yet another invention from the many super trolls we have here.

@OP if that is not the case, then think seriously about how you ask your question, and what information you provide. If you want help, then be helpful yourself.
Referring to the last snippet you posted:

Lines 17,20: These are going to cause out of bounds references (as MikeyBoy already pointed out). Your loop is assuming a 2x2 array, while it is only declared as 1x1.

Line 22: This line is okay here for debugging, but you really don't want it here. What you want is a Display () function that displays your array.

In the following snippet, I've made a couple of enhancements.
- I've defined MAX_X and MAX_Y as your array dimensions. This means if you change the size of the array, you only need to change the size in one place. LoadFile() and Display() adjust accordingly.
- I've added a Display() function which will display you array as rows and columns with spaces between the numbers and each row on a new line.
- I've incorporated MikeyBoy excellent suggestion of using a Point structure.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <fstream>
using namespace std;

struct Point
{   int x;
    int y;
};
    
class dCoords
{   
public:
    static const int ARRAY_SIZE = 10;       //  Define array size

	void LoadFile();
	void Display () const;
	
private:
    Point   mArray[ARRAY_SIZE]; 	
};

//  Read Points array from a data file
void dCoords::LoadFile ()
{   ifstream infile ("dCoords.txt");
	if (!infile)
	{   cout << "Cannot open File";
		exit(1);
	}
	for (int i = 0; i < ARRAY_SIZE; i++)     //  use max array size
	{   infile >> mArray[i].x >> mArray[i].y; 
	}
}

void dCoords::Display () const
{   for (int i = 0; i < ARRAY_SIZE; i++)     //  use max array size
	{   cout << mArray[i].x << "," << mArray[i].y << " " << endl;
	}	
}

int main ()
{   dCoords obj;
    
    obj.LoadFile (); 
    obj.Display ();
}

Thanks you so much, it's finally working and I understand why, which is more important, now I can finally get on with the actual A* stuff.

Quick question, I need to output this to a txt file as well, would it be a case of outputting/writing

1
2
3
4
5
6
7
8
9
10
ofstream output;
	output.open("output_1.txt");
	if (!output)
	{
		cout << "Cannot open file";
		exit(1);
	}
	output << "coordinates are:" << mArray[i].x << "," << mArray[i].y << " " << endl;
	
	output.close();


like that, or am I missing something (I know output<<"Cordinates are" is working, but the arrays still need to be added )

@TheIdeasMan

Maybe the OP is trolling?

I am amazed that the respondents have had so much patience with the obfuscation, lack of answers, requests for needing a break.

Maybe this is yet another invention from the many super trolls we have here.

@OP if that is not the case, then think seriously about how you ask your question, and what information you provide. If you want help, then be helpful yourself.


Seriously? I'm was looking for some help and may have misworded or not fully understood what was expected, there's no need to be so negative, especially since this is all still quite new to me
Pages: 12