954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

String array problem

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 program1_(_for_post)_.cpp (2.03KB)
DevC++4.9.9.2
Light Poster
39 posts since Apr 2008
Reputation Points: 10
Solved Threads: 1
 

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

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;
      }
}
DevC++4.9.9.2
Light Poster
39 posts since Apr 2008
Reputation Points: 10
Solved Threads: 1
 

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
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You