Thread: vectors question

  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
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Your declaration is wrong, first if you want a vector of char array's (which is impposible although you could do char*'s) you should instead use the string type since you are already using vectors. so why not just include the string library change your loop to this:
    Code:
    vector<string> instructions;
    string strTmp;
    
    while(!jpgfile.eof())
    {    getline(jgpfile,strTmp);
         instructions.push_back(strTmp);
    }
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

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

    thanks alot but.......

    thanks very much your way did work!!!!!!
    howevr i was hoping to avoid the use of strings (not the end of the world if i have to though).
    You said that there was a way to do it with pointers,
    could you please tell me the signature that the vector would need to have it hold a number of pointers pointing to char arrays.
    thanks alot

  4. #4
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Lol that is kinda funny not to sound rude or mean, but a vector is a template type that means you create one of a type putting the type in the < > brackets when you declare it, ex:

    Code:
    vector<string> vstr;
    vecotr<char*>  vpsz;
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

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

    thank you

    brill just got it workin!!!1
    did it the way u suggested using strings
    thanks for all ur help
    rxg00u

  6. #6
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    i'm hoping the two threads were a mistake, maybe a slow connection

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, 12:36 PM
  2. 2d vectors question
    By jw232 in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2008, 06: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