Hey, ive debugged my program to this line, and im not sure what to use, strcpy or some other thing, but i need to get the string from a[x] in to the new string c[z]. Any suggestions?

string a[100];
string c[100];

      if (a[x].compare(b[y])==0)
        {
	  c[z] = a[x];  //doesnt work
          z++;
        }

I have also attached the full program if needed. Thank you
-Scott

Dani AI

Generated

Quick diagnosis: the line c[z] = a[x] is fine — std::string supports assignment and there’s no need for strcpy. As observed, the string assignment itself will copy the contents, and correctly pointed out that a parameter declared as string array[100] decays so the function can modify the array elements visible to the caller. The real problem in the thread is elsewhere: index/counter handling and the file-reading logic.

The posted getData resets n inside the function while n is passed by value, so the caller’s counters (counterA, counterB) never get updated. That explains why printing inside getData looks fine but later code behaves as if no data was stored. Two robust fixes: either return the number of items read, or pass the counter by reference (or better, return a container). For example, make getData return an int count and call counterA = getData(...) so the caller knows how many entries were filled.

Use the right input method for the job and guard indices. If whole lines are needed, use std::getline; if tokenized words are intended, use operator>>. Avoid while(!data.eof()) and fragile peek()=='\n' checks. Consider std::vector<std::string> to avoid fixed-size limits and off-by-one risks. Finally, add simple runtime checks while debugging: initialize z and the counters to 0, print x/z before assignment, assert bounds (e.g., 0 <= x,z < array_size), and remove the goto in favor of structured loops — those small changes will make the root cause obvious.

Recommended Answers

All 4 Replies

[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++;
}

Sorry, but parameter string array[100] is equivalent of pointer to array of strings passing, so it's correct way to go...

The function getdat works fine... if you print the data to screen using a for loop, you can see that the information is being stored correctly in the arrays.

void getData(ifstream& data, string array[100], int n)
{  
  n=0;
  while(!data.eof())
    {
      data >> array[n];
      if(data.peek()=='\n')
        {
          break;
        }
      n++;        
    }
    //test!
    for (int x=0;x<=n;x++)
      {
        cout << array[x] << endl;
      }
}

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.