I am facing a very simple problem that many of you will be able to solve, however I'm having a hard time trying to figure a method to program this.

I am trying to create a dynamic array with the many prices of different clothing for a shop I'm creating.

The prices depend on the colour - I know it's stupid, and some of the clothing don't have all the colours available.


I have managed to create in my program, a structure that is very much like this

struct item{
string cloth;
string colour;
double price;};

Then I created

Item list[maxnumber]

(my maxnumber is a constant that I have defined on the beginning of the class)

I will read a text in this format

cloth,colour,price

and send the information to the list I have created.

And I already did that.

So now my list that is structured pretty much like this:

list
-- Item 1 -- Item 2 -- Item 3 -- etc...
---cloth1----cloth2 ---cloh3-----etc...
--color1----color1----color3----etc...
----10---------5-------12-------etc...

What I want to do and only need a suggestion, is the next step where I should create a dynamic array, for example lister [3][3], and then define it as something like this:

cloth: -- cloth1 -- cloth2 -- cloth 3 --
Colour:
|
|
colour1 ---10----------5---------NULL----
|
|
colour2 ---NULL-------NULL-------NULL----
|
|
colour3 ---NULL-------NULL-------12------


according to the example I'm using.

What should I do to be able to do that?

Edited 6 Years Ago by Diogo Martinho: n/a

The Item array you posted is a static array, not a dynamic array. To convert it to a dynamic array you have to make it a pointer and then use new to allocate the array siz

Item* list;
list = new Item[maxnumber];

After that you can put maxnumber number of items in the array. If you need to make the array even bigger you have to reallocate and copy

// allocate new temp array
int newsize = maxnumber + 10; // or whatever value you want to use here
Item* newlist = new Item[newsize];
// copy items from old array to new array
for(int i = 0; i < maxnumber; i++)
   newlist[i] = list[i];
// destroy old array
delete[] list;
// set old array to the new array pointer
list = newlist;
maxnumber = nwsize;

Is it mandatory to use arrays? If not you can look at vectors or other STL containers to make your life easier.

Is it mandatory to use arrays? If not you can look at vectors or other STL containers to make your life easier.

No Not really, I'm just trying to create a dynamic matrix that is why I think using a dynamic array will be the best approach.
If you got a better suggestion I'm all ears.

If you need a dynamically increasing array of structures you can use a vector. Vector is an STL container which will do all the memory management itself and you can add whatever number of elements to it without having to worry about allocating-deallocating memory. Read more here vector. I'm not sure about your matrix requirement but may be you can use a vector of vectors.

I have created a double pointer matrix

double ** matrix;

Then I converted the colour and cloth string to an integer that works like a code for the equivalent colour/cloth.

In the end I used this cycle

for(int i = 0; i<ind;i++){
	cout<<list[i].colour<< " " <<list[i].cloth<<" ";
		cout << list[i].price<<endl;
	
		matrix[list[i].colour][list[i].cloth] = list[i].price;

	}

The build is successful however when I try to compile I get an error saying my matrix hasn't been initialized yet.

Can you guys tell me what I am doing wrong?

Edited 6 Years Ago by Diogo Martinho: n/a

You need to post more of the code because the code you did NOT post is just as important to solving the problem as the code you did post.

#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;




const int maxsize = 200;
int main() {
	struct List{
	int colour;
	int cloth;
	double price;
	
	
	

	};
	double ** matrix;
	

	List lis ;
	List list[maxsize];
	int ind =0;
	string line;
	ifstream file;
	
	file.open("shop.txt");
	if(!file){
	cout<<"File does not exist!"<<endl; return -1;}

	while(!file.eof()){
	getline(file,line,'\n');
	
	if(line.size()>0){
	int ini = 0;
	int pos = line.find(',',ini);
	string colour_liv(line.substr(ini,pos-inic));
	char*auxColour = &colour_liv[0];
	int colourId = atoi(auxColour);
	pos++;
	ini=pos;
	pos=line.find(',',ini);
	string cloth_liv(line.substr(ini, pos-ini));
	char*auxCloth = &cloth_liv[0];
	int clothId = atoi(auxCloth);
	pos++;

	ini = pos;
	pos = line.find(',',ini);
	string price_liv(line.substr(ini,pos-ini));
	char*auxPrice = &price_liv[0];
	double price = atof(auxPrice);
	pos++;
	
	lis.colour=colourID;
	lis.cloth=clothID;
	lis.price=price;
	if(ind<maxsize)
		list[ind++]=lis;
	
	}


}

	file.close();
	for(int i = 0; i<ind;i++){
		cout<<list[i].colour<< " " <<list[i].cloth<<" ";
		cout << list[i].price<<endl;
	
		matrix[list[i].colour][list[i].cloth] = list[i].price;

	
	}


	
	cin.get();
}

Edited 6 Years Ago by Diogo Martinho: n/a

Are you sure that's your current code? The code you posted does not compile. You have undeclared identifiers on Lines 39, 57, and 58. According to your previous post, it should compile.

Seriously, read the formatting guidelines that WaltP gave you a link to. You code is almost impossible to read and your indentation levels are not correct. There are several indentations missing. If you actually take the time to format your code properly, it makes the code easier to read and it makes it easier to spot logic errors. Re-format your code properly and you will see that you are missing a closing brace near the end of your code. Your if that starts on Line 30 never gets closed properly. If you had paid closer attention to your indentation(s), and if your IDE is worth anything, a red flag should have gone up when everything you placed between Line 77 and Line 81 was at the wrong indentation level.

The warning you receive about matrix is because you have declared the "double-pointer" but you haven't given it anything to point at. It's an extremely dangerous "dangling" pointer. You need to add nested initialization loops (preferrably) immediately after your declaration. If you don't know how, search the forums a little, there are myriad examples out there from just the last week or 2. If you don't address that, whenever you try to access that array/matrix you are attempting to dereference a dangling pointer, not to an actual legal element of an array/matrix. This is never a good thing.

Edited 6 Years Ago by Fbody: n/a

Are you sure that's your current code? The code you posted does not compile. You have undeclared identifiers on Lines 39, 57, and 58. According to your previous post, it should compile.

Seriously, read the formatting guidelines that WaltP gave you a link to. You code is almost impossible to read and your indentation levels are not correct. There are several indentations missing. If you actually take the time to format your code properly, it makes the code easier to read and it makes it easier to spot logic errors. Re-format your code properly and you will see that you are missing a closing brace near the end of your code. Your if that starts on Line 30 never gets closed properly. If you had paid closer attention to your indentation(s), and if your IDE is worth anything, a red flag should have gone up when everything you placed between Line 77 and Line 81 was at the wrong indentation level.

The warning you receive about matrix is because you have declared the "double-pointer" but you haven't given it anything to point at. It's an extremely dangerous "dangling" pointer. You need to add nested initialization loops (preferrably) immediately after your declaration. If you don't know how, search the forums a little, there are myriad examples out there from just the last week or 2. If you don't address that, whenever you try to access that array/matrix you are attempting to dereference a dangling pointer, not to an actual legal element of an array/matrix. This is never a good thing.

I typed it from the paper not from the computer when I made that reply.

It's colourId and clothId and ini not inic.
Those are the errors.

And for this exercice I should only use a cpp, a Main that is why it is structured like that.

Edited 6 Years Ago by Diogo Martinho: n/a

You don't have to retype anything in order to post it here. using your IDE, just copy the code into the clipboard (almost all IDEs will let you do that) and paste it here into the editor between code tags. The code tags will preserve all the spacing you see in your IDE.

>>It's colourId and clothId and ini not inic.
Yeah, I know. I just wanted to make sure you did.

>>And for this exercice I should only use a cpp, a Main that is why it is structured like that.
That's really not relevant to the issue.

I'll take back my previous comment about missing braces. I just found the missing brace hidden at the end of Line 31. I do still stand by my comments about accurate and consistent formatting though. If you had been accurate and consistent, that brace would have been easier to spot.

Have you initialized your matrix yet? Or is it still a dangling pointer?

Edited 6 Years Ago by Fbody: n/a

Have you initialized your matrix yet? Or is it still a dangling pointer?

I already reached that conclusion that is why I'm asking how should I initialize the matrix. I thought a dynamic matrix would allow me to add objects to it without having to worry with the initialization.
Apparently not otherwise the program would run smoothly and I am trying to figure out how should I initialize it.

Edited 6 Years Ago by Diogo Martinho: n/a

To initialize an array, you need a loop. For a multidimensional array, you need nested loops.

//declare a base pointer-to-pointer to use as the array's name
dataType **array;

//attach an array of pointers to the base pointer
array = new dataType *[rows];

//attach sub-arrays of objects to the pointers in the base array
for (int i = 0; i < rows; ++i) {
  array[i] = new dataType[cols];

  //initialize the objects
  for (int j = 0; j < cols; ++j) {
    array[i][j] = someValue;
  }

}

You can also use initialization lists, but they get messy fast.

Edited 6 Years Ago by Fbody: n/a

This article has been dead for over six months. Start a new discussion instead.