Separating values

Hello all,
I input data from a file into an array of strings, where each string represents a complete line from the file(the elements in each line is separated by commas). Now, I am trying to separate the elements in each line based on commas. (Basically I wanna take the commas out of the lines).
I use a for-loop to go through the lines of the file. For each line, I use a while loop to separate the elements by the commas in the file. However, I get segmentation fault when I run the program, which compiles just fine. I would like to know where am I going wrong using the loop.

**Some lines end with two commas following each other (be mindful of that).
Thank you.



Last edited on
your sure it compiles ?
string name = arrayy[i];
That was a typo (it happened while I was typing it to post it here).
But, anyway, yes it is name = array[i]; // not arrayy[i]
Each array[i] is a string or (a line from a file).
Thanks for noticing that... But, this is not the problem.
i get a segmentation fault every time I run the program... It prints out just half of the lines from the array.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void commaBase(string array[], int size)
{
int x;
string line;
for(int i = 0; i < size; i++)
{
line = array[i];
for(x = 0; x < line.size(); x++)
{
    if(line[x] != ',')
    {
       cout << line[x];
    }
}
cout << endl;
}
cout << "*******************************" << endl;
}
Last edited on
Great!!
Thank you so much.
Topic archived. No new replies allowed.