943,866 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2137
  • C++ RSS
Jan 7th, 2009
0

Creating a dynamic 2D array

Expand Post »
I have a code snippet i've been working on but not getting very far.

What I have is a text file which looks like an XML but i want to get the data between the tags and ignore the tags themselves. I want to count the number of data items between the tags and create a 2D array so if for example I have this:

Quote ...
<tag1>
Data 1
Data 2
Data 3
Data 4
</tag1>
<tag2>
test1
test2
test3
</tag2>
I would like to create an array 4 by 3.

I think i can get the first array right but when i try and get the other bit it gives the wrong answer

C++ Syntax (Toggle Plain Text)
  1. int d1=0, d2=1;
  2.  
  3. while (! filename.eof()) //Loop through lines
  4. {
  5. getline(filename, line);
  6.  
  7. string test = "<";
  8. if(line.find("<",0) == string::npos)
  9. cout << line << endl;
  10.  
  11. unsigned int pos1 = line.find(sub1, 0);
  12. unsigned int pos2 = line.find(sub2, 0);
  13.  
  14. if( pos1 = string::npos)
  15. if(pos2 != string::npos)
  16. d1++;
  17.  
  18. if( pos1 = string::npos)
  19. if(pos2 = string::npos)
  20. d2++;
  21.  
  22.  
  23. }
  24.  
  25. IntArrayPtr *m = new IntArrayPtr[d1];
  26.  
  27. int i;
  28.  
  29. for(i=0; i < d1; i++)
  30. m[i] = new int[d2];
  31.  
  32. cout << d1 << endl;
  33. cout << d2 << endl;
  34.  
  35. filename.close();
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
AdRock is offline Offline
64 posts
since Dec 2008
Jan 8th, 2009
0

Re: Creating a dynamic 2D array

You should consider using a vector, or a list instead of bothering with an array. It would be much easier. Read the whole file line by line in a temporary string then push it back into the vector. Iterate through the vector and you will get all of the lines one by one. Then write a function, which can determinate if it is a tag(opening or closing). If it is just throw it away, or mark it with something. When you are done, all you have to do is read out, the unmarked strings. Oops I forgot, it would only work if the tags are separated with new line, otherwise you would have to make some substraction from the strings with tag.
Last edited by BevoX; Jan 8th, 2009 at 1:50 am.
Reputation Points: 77
Solved Threads: 12
Junior Poster in Training
BevoX is offline Offline
57 posts
since Jan 2009
Jan 8th, 2009
0

Re: Creating a dynamic 2D array

You should split up all the tasks into separate functions. All of it should be part of class.

The file handling would probably look like this. Unless you're concerned about massive files..
C++ Syntax (Toggle Plain Text)
  1. if(file.is_open)
  2. {
  3. while(getline(file, str))
  4. {
  5. lines.push_back(str);
  6. }
  7.  
  8. file.close();
  9. }
You now have the contents.
Time to just get where a tag ends and begins, continue to alter the attributes of the inner-most data based on these tags.
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Jan 8th, 2009
0

Re: Creating a dynamic 2D array

I did think of using seperate functions to break down each bit so i'll look into that.

Also with vectors can they be 2D?

The reason i thought an array would be better becuase i can count the number of tags so i can set the array size but i need to get wheat is in between.

Also i need to be able to reference a location of a cell and disp[lay what is in there.

I have only just started using C++ so i have no clue but i have used Java before a little
Last edited by AdRock; Jan 8th, 2009 at 7:26 am.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
AdRock is offline Offline
64 posts
since Dec 2008
Jan 8th, 2009
0

Re: Creating a dynamic 2D array

Well, a vector or list is similar to an array. They are a kind of sequence containers, and they expect real arguments. So, you can't create a list or vector, which holds lists or vectors.

Read about them:

http://www.cplusplus.com/reference/stl/vector/
http://www.cplusplus.com/reference/stl/list/
Reputation Points: 77
Solved Threads: 12
Junior Poster in Training
BevoX is offline Offline
57 posts
since Jan 2009
Jan 8th, 2009
0

Re: Creating a dynamic 2D array

2D enough for you?
C++ Syntax (Toggle Plain Text)
  1. vector< vector<int> > v;
  2.  
  3. for(int i = 0; i < 10; i++)
  4. v.push_back( vector<int>() );
  5.  
  6. for(int i = 0; i < v.size(); i++){
  7. for(int x = 0; x < 10; x++){
  8. v[i].push_back(x*i);
  9. }
  10. }
  11.  
  12. for(int i = 0; i < v.size(); i++){
  13. for(int x = 0; x < v[i].size(); x++){
  14. cout << v[i][x] << '\t';
  15. }
  16. cout << endl;
  17. }
Chris
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Jan 9th, 2009
0

Re: Creating a dynamic 2D array

Click to Expand / Collapse  Quote originally posted by BevoX ...
Well, a vector or list is similar to an array. They are a kind of sequence containers, and they expect real arguments. So, you can't create a list or vector, which holds lists or vectors.

Read about them:

http://www.cplusplus.com/reference/stl/vector/
http://www.cplusplus.com/reference/stl/list/
That's just wrong information! A vector can hold anything - its a freakin template class!
Reputation Points: 352
Solved Threads: 109
Master Poster
skatamatic is offline Offline
776 posts
since Nov 2007
Jan 11th, 2009
0

Re: Creating a dynamic 2D array

Click to Expand / Collapse  Quote originally posted by skatamatic ...
That's just wrong information! A vector can hold anything - its a freakin template class!
Yeah now I know. I am sorry I didn't want to mislead anyone.
Reputation Points: 77
Solved Threads: 12
Junior Poster in Training
BevoX is offline Offline
57 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Templates and return types
Next Thread in C++ Forum Timeline: extracting a sentence





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC