Converting a char array to a string array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Lana rose
    New Member
    • Nov 2006
    • 8

    #1

    Converting a char array to a string array

    Hi

    I have written a function that will search through a document and extract some data. The function takes one letter at a time and then stores it in a char array 'array1'.Now I need to convert it into a string array but I am having great trouble getting the code right.

    I am trying to use the 'assign' function but it does not seem to be working.

    I hope that you can help me!

    Code:
    void SearchforVars(string Line,int initial, int &start_position, int &end_position, int one, int two)
    {
    	char array1[100];
    	string varname;
    
    	for (int i= initial;i < Line.length(); i++)
    	{
    					
    		if ((Line[i] != one) && (Line[i] != two))
    		{
    			start_position = i;
    			break;
    		}
    	}
    		
    	int x = 0; 
    
    	for (int n=start_position;n < Line.length(); n++)
    	{
    		if (Line[n] == one) 
    		{
    			end_position = n;
    			break;
    		}
    		
    		dataout << Line[n];
    		array1[x] = Line[n];
    		//cout << array1[x];
    		
    	}
    	varname.assign(array1);
    	cout << varname;
    
    }
    Thanks!
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    A string is a char array, which is why your function stores it in one. A string array refers to an array full of strings (which I suspect is not what you mean), A String is merely an array of characters , so an array of characters is a string (but not a string array) [and a string array is an array of strings]

    Perhaps if you elaborated to show the problem(s) you have with the current code (?)

    Comment

    • Lana rose
      New Member
      • Nov 2006
      • 8

      #3
      Hi

      Thanks for your help, sorry if i didn't explain my problem very well. I am in fact trying to produce an array that holds thousands of strings. At present my code parses a document and retrieves 11 different types of data and stores them in a text file. I then want to uses this data in a different application. But before I do this I want to store the different data in 11 different arrays.

      So far my code reads in the data and puts it into an array. I then want to put the data from that array into specific arrays for each data type once it has been extracted.

      Can you think of the best way to do this?

      Thanks

      Comment

      Working...