Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parsing text streams into 2d array

Status
Not open for further replies.

charker2

Programmer
Apr 29, 2002
1
US
I'm working on a project for school and need to read a parsed string into a 2d array. I'm using the string lib substr function to parse the stream to two different variables and need to load into the array. I've tried two nested for loops to increment throught the rows and cols but can't figure out how to get the two varaibles into the array.
array[row][] = var1;
array[][col] = var2;
this assignment type is not working. I would normally have an array of structs or pointers but can not do this with thia assignment. Can any one HELP! thanks
 
I'm not sure about your question.

However; if the variable type of array[][], var1 and var2 are the same, the assignment operator should work.

By the way, you couldn't do "array[row][] = var1;" instead you need "array[row][col] = var1;"
 
You've parsed the string into two different variables... can you explain that? What are the types of these two different variables? How did you parse the string into them? Does each hold half of the string or what? What's the reasoning behind parsing it? Be more specific and someone can help.


I can tell you that your code:

[tt]array[row][] = var1;
array[][col] = var2;
[/tt]

doesn't work because you can only assign to a particular element of an array. That means you could assign a character to array[row][col], or you could copy a string into array[row] using strcpy (assuming you have memory allocated there for it), but you can't do whatever it is your code looks like it's trying to do (as you seem to have already figured out).
 
Appreciate the fact that you state this is for a school assignment. So, investigate at least 3 ways to do this.

1. char array
2. class string: string array ("cats and dogs");
3. STL container: vector, deque, or list and maybe use the replace algorithm.

... anyway ...
Looks like you are trying to use a char array as previously declared something like:
char array[3][] = {"str one", "str two", "str three"};

//In a multidimensional array, I think thay you can leave the last dim empty, and the compiler will figure the size based on the largest element.

If you want to change "str two" to say "str ten",
char var1 = 'e';
char var2 = 'n';
array[1][5] = var1;
array[1][6] = var2;

NOTE: you can only assign 1 char to any index of the array.

I have not tested this, so ... Good luck,
 
If it is a character array, such that you have declared it like this:
const int wordSize = 4;

char array[2][wordSize] = { { "cat" }, //I think you can
{ "dog" } };//declare like this

then you should realize that you don't have a double scripted array of strings. Rather you have a double scripted array of characters like this:

[0] [1] [2] [3]
[0] 'c' 'a' 't' '\0'
[1] 'd' 'o' 'g' '\0'

as mentioned, replacing e.g. element [0][1] will replace the 'a' not the whole word "cat".

As for entering var1 and var2 into the array, I'll assume that both variables are strings such that:

char *var1 = "bat"; //which is equivalent to 'b''a''t''\0'
char *var2 = "boy"; //which is equivalent to 'b''o''y''\0'

to enter var1 and var2 into an array, you can either have a for loop that replace character by character:

for( int i = 0; i < wordSize; i++ )
array[0] = var1; //var1 can be thought of as a
//single-scripted array:
//[0] [1] [2] [3]
//'b' 'a' 't' '\0'

or (and this way isn't recommended because you won't learn as much) you can call the strcpy function like this:

strcpy( array[0], var1 ); //where array[0] is a char * to
//&quot;cat&quot; if declared as above

if you want process the everything with embedded for loops, I recommend having an array of string variables like this:

char *vars[2] = { {&quot;bat&quot;},
{&quot;boy&quot;} }; //I think this works, anyway

so you can loop through vars[0] and vars[1].
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top