What i have to do is read a string into a vector, then cop that vector into an array of character pointers, then for each element in the vector i have to allocate a new character array andcopy the data from the vector element into that character array. Then finally insert a pointer to the character array into the array of character pointers...

This is what i have, with my error messages pointed out in my code... i think i did what i need to do, just with a few new to c++ errors.

also if anyone has advice on how to make this program easier please post it. I feel like im making this way too hard

#include <string>
#include <vector>
#include <iostream>
using namespace std;

int main()
{
  vector <string> hold;
  string stemp;
  int size = 10;
  char *carr[size];

  while( getline( cin, stemp) )
    hold.push_back( stemp );

  for ( int i = 0; i < hold.size(); i++ ){
      char *pca = new char[ 100 ];

      if( i >= size ){
        size += 10;
        char *ctemp = new char[ size ];

        for( int j = 0; j < i; j++ )
          ctemp[j] = carr[j]; <<error: invalid conversion from `char*' to `char'


        delete [] carr;
        carr = ctemp; <<error: incompatible types in assignment of `char*' to `char*[((unsigned int)((int)size))]'

      }

      carr[i] = pca;
      cout << carr[i] << endl;
    }
  return 0;
}

the errors are pretty clear i guess. There is nothing that seem to be complicated about the error messages.

i got rid of the errors, but now my code still doesnt work... any idea why? I'm new at c++

int main()
{
  vector <string> hold;
  string stemp;
  int size = 10;
  char *carr = new char[ size ];

  while( getline( cin, stemp) )
    hold.push_back( stemp );

  for ( int i = 0; i < hold.size(); i++ ){
      char *pca = new char[ 100 ];

      if( i >= size ){
        size += 10;
        char *ctemp = new char[ size ];

        for( int j = 0; j < i; j++ )
          ctemp[j] = carr[j];

        delete [] carr;
        *carr = *ctemp;
     }

      carr[i] = *pca;
      cout << carr[i] << endl;
    }
  return 0;
}

You use vector hold only to get strings from the input.
edit: Also you should avoid using char tables.

Edited 7 Years Ago by Zjarek: n/a

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