vectors question

This is a discussion on vectors question within the C++ Programming forums, part of the General Programming Boards category; hello this is probabley a simple probelm to overcome but i have spent ages trying to figure out what to ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    22

    vectors question

    hello
    this is probabley a simple probelm to overcome but i have spent ages trying to figure out what to do and have not got anywhere

    i am reading in a file a line at a time e.g

    while (! jppfile.eof() ) {
    jppfile.getline (buffer,200);
    cout << buffer ;
    }
    where jppfile is a stream, now this works great and i now want to store them (each line) in a vector; simple i thought:
    vector <char[200]> instructions; //200 that is how long buffer is

    i then proceede to push them onto the vector:
    instructions.push_back(buffer);

    But this is the error being given to me:
    cannot convert from 'const char [200]' to 'char [200]'

    I persumed that this is something to do with the fact that bufer is never actually 200 characters long, and the vector is expecting 200 characters so conflict is arising. Is there something obvious that i have missed or is there a solution to the problem that is a bit more complicated
    sorry to bother people
    cheers


  2. #2
    Something Clever ginoitalo's Avatar
    Join Date
    Dec 2001
    Posts
    187
    It looks like your trying to put a const char[] into a char[] which is a type mismatch,

    so you could either make your vector of const type, if you don't need to modify it

    or

    cast the incoming const char[] to a char[] using (char),
    which takes the const off of the incoming array, and allows for easy vector editing.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    114
    Store not arrays of chars buffer in the vector, but the pointers to the buffer

    Code:
    std::vector<char*> instructions;
    
    .
    .
    .
    char buffer[200];
    .
    .
    .
    instructions.pushback(&buffer);
    This will work great now. One little problem though. You will overwrite your data everytime you read a new line. Do this:

    Code:
    std::vector<char*> instructions;
    char *szBuffer;
    
    while (! jppfile.eof() ) { 
        szBuffer = new char[201]; // 201?? make room for 200 chars and the ending '\0'
        memset(szBuffer, 0, 201); // clear the buffer
        jppfile.getline (szBuffer, 200);
        cout << szBuffer;
        instructions.push_back(szBuffer); //szBuffer is a pointer!
    } 
    
    // do this when you no longer need the instructions:
    std::vector<char*>::iterator it;
    for(it = instructions.begin(); it != instructions.end(); it++){
        delete *it;
    }
    or something...

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    22

    thank you

    thanks for the advice
    i think i will be using pointers to store in the vector i think that will be the easiest way for the situation i am in
    thanks again
    rxg00u

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question about Vectors
    By ataman in forum C++ Programming
    Replies: 4
    Last Post: 06-02-2008, 01:36 PM
  2. 2d vectors question
    By jw232 in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2008, 07:31 PM
  3. question about stacks (and vectors too for that matter)
    By Silvercord in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2003, 12:26 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. vector of vectors question
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 02-10-2003, 03:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21