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

Attachments
#include<iostream>
#include<fstream>
#include<string>

using namespace std;


void getData(ifstream&, string[100], int);
void AUB(string[100],string[100],string[100], int, int, int);
void ANB(string[100],string[100], string[100]);
void A_B(string[100],string[100], string[100]);
void print(string[100], int);
int main()
{
  ifstream data;
  string fileName;
  int counterA=0, counterB=0, counterC=0, menu=0;
  string a[100],b[100], c[100];
  
  begin:
  cout << "Enter file name: ";
  getline(cin, fileName);
  cout << endl;
  data.open(fileName.c_str());
  if (!data.is_open())
    {  
      cout << "Error opening file\n\n";
      goto begin;
    }
  else
    {
      getData(data, a, counterA);
      getData(data, b, counterB);
    } 
  cout << "What would you like to do? (Enter the choice number)\n";
  cout << "1) A U B\n";
  cout << "2) A N B\n";
  cout << "3) A - B\n";
  cin >> menu;
  switch(menu)
    {
      case 1:
        AUB(a,b,c, counterA,counterB, counterC);
      break;
      default:
        cout << "Invalid Entry\n";
      break;
    }
      
      
      
  data.close();  
  system("PAUSE");
  return 0;
}
  
/* GetData: 
Pre Condition:
Post Condition: 
*/
void getData(ifstream& data, string array[100], int n)
{  
  n=0;
  while(!data.eof())
    {
      data >> array[n];
      if(data.peek()=='\n')
        {
          break;
        }
      n++;        
    }
}

void AUB(string a[100], string b[100], string c[100], int counterA, int counterB, int counterC)
{
  //All elements in A and B
  int x=0,y=0,z=0;
  while(x <= counterA)
    {
      if (a[x].compare(b[y])==0)
        {
          //strcpy(c[z],a[x]);
          c[z] = a[x];
          z++;
        }
      if (y < counterB)
        {
          y++;
        }
      else
        {
          y = 0;
        }
      x++;
    }
  print(c, z-1);
}
      
void print(string c[100], int counterC)
{
  int x = 0;
  while (counterC >= x)
    {
      cout << c[x] << " ";
      x++;
    }
}

[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.

This article has been dead for over six months. Start a new discussion instead.