[edit]
The line highlighted should actually work. It's ok to assign one std::string to another with the = operator.
Looking through your program, I do see some other issues:
void getData(ifstream& data, string array[100], int n)
{
n=0;
while(!data.eof())
{
data >> array[n];
if(data.peek()=='\n')
{
break;
}
n++;
}
}
You should pass the second argument by reference, else the data will be lost upon exiting the function.
Data.eof() is never a good idea. And since you're looking for "\n"'s maybe you could betetr use getline():
int array_counter = 0;
string buffer;
while (getline(data, buffer))
{
in_array[array_counter] = buffer;
array_counter++;
}
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
Sorry, but parameter string array[100] is equivalent of pointer to array of strings passing, so it's correct way to go...
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
Lines 32,33 of your code
getData(data, a, counterA);
getData(data, b, counterB);
1. counterA and counterB are both 0, and will remain so after the call. Is this counter a limit or a result?
2. the while eof() thing in the function will ensure a gets all the data, and b none of it
3. the while eof() thing (as neik_e points out) means you go round the loop one extra time.
At the very least, getData() needs to limit the amount of data added to the array with
while ( n < 100 && getline(data,buffer) ) {
}
Oh, and replace the goto in main with another while loop.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953