Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 318
  • Last Modified:

Vectors...if you wouldn't mind giving a hand :)

Hi everybody,

This is the code I've got:


vector <char> begincharacters;
vector <char> endcharacters;
char beginline[20];
char endline[20];
char *begintok;
char *endtok;
int i = 0;

ifile.getline(beginline,19);
ifile.getline(endline,19);

begintok = strtok(beginline," ");
begincharacters.push_back((const char&)begintok);

while(begintok != NULL)
{
     begincharacters.push_back((const char&)begintok);
     begintok = strtok(NULL," ");
}

endtok = strtok(endline," ");
endcharacters.push_back((const char&)endtok);

while(endtok != NULL)
{
     endcharacters.push_back((const char&)endtok);
     endtok = strtok(NULL," ");
}

i = 0;

while(i <= begincharacters.size())
{
     cout << begincharacters[i];

     i++;
}


The trouble is, is that it just spews out tripe and not the words that are assigned...if they're assigning properly at all! :( Can anybody give a hand please?

Thanks for your time,
NAE
0
nearlyanexpert
Asked:
nearlyanexpert
1 Solution
 
MattAACommented:
I'm not sure what the purpose of your algorithm is here, so this might not work in your situation, but it seems to me you  could make your life a whole lot easier just by using the string class (part of the STL, like vector).  The string class' find and substr functions will do what you want above in much cleaner, more efficient code.

As for the vector method, methinks the problem is that you have a vector declared as <char> (single character) and are assigning it a char* returned froms strtok (possibly multiple characters).  This produces several warnings in gcc about invalid pointer dereferencing, which is probably what causes the garbage output.  Using vector<char*> will get it to work  (although I'm not necessarily sure if that is your intended purpose).

Hope this helps.
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
See
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20525963.html

for my disortation on pointers.

As for fixing your problem:

int i = 0 ;

while ( beginline[i]!= "\0") //\zero, the characters for null
{
 vector.push_back(beginline[i]);
 i++;
}

I'm not sure what the second part is supposed to be doing.

as for your while loop:

it should be only < .size();

if you have <= .size() it will go out of bounds.  reason being:

If you have an empty vector, the size of it is 0.
If you have one element in vector, the size of it is 1;
however, the 1st element of the vector is referenced by vector[0].  (same as arrays.) so there's an offset of one.  If you say give me the 20th element, it's actually vector[19]. size returns the 1 based, you need the 0 based, hence,  < .size() will stop 1 before the size, IE if size is 20, it stops at 19, which is what you want.

Questions?  Hope that helps.
 
0
 
nearlyanexpertAuthor Commented:
Right...sorry I should have explained it better. What its doing is reading a line from a file then seperate it up by ' '. It then adds each word that is seperated into a vector. It then outputs the vector contents but only rubbish comes out. Things like 'ifile' are declared elsewhere...

MattAA, I'm not trying to implement any string function like that, but simply use a tokenizer on a string and add each one to a vector.

ged325, the second part i.e. the i++, increments i so that on the next loop in the while it prints out the next vector item...or if you mean the '    begincharacters.push_back((const char&)begintok);'.
That is assigning whatever was in begintok to the end of the vector.


Hope its a bit more clearer,
NAE
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
nearlyanexpertAuthor Commented:
MattAA, the solution you gave give the following arror:

error C2664: 'push_back' : cannot convert parameter 1 from 'const char' to 'char *const & '
        Reason: cannot convert from 'const char' to 'char *const '
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
The second part as in the end tokens.  I know what i++ does, and when you get it running if you leave it <=size() it will go out of bounds.  If buffer holds everything in the file, then you can do it as I suggested.  

(while buffer[i]!="\0")

If you're trying to add whole words instead of strings, then you should declare vector as vector<char *>;
or use the STL vector<strings>;
0
 
nearlyanexpertAuthor Commented:
I'm sorry but I just can't understand what you don't get about the code :)....but I'll step through it trying to explain:

vector <char> begincharacters;               //Setup a vector called begincharacters of type char
vector <char> endcharacters;               //Setup a vector called endcharacters of type char
char beginline[20];                    //Setup a char array called beginline with 20 characters of type char
char endline[20];                    //Setup a char array called endline with 20 characters of type char
char *begintok;                         //Setup the pointed needed to tokenize beginline
char *endtok;                         //Setup the pointed needed to tokenize endline
int i = 0;                         //Setup the indexer of the vector

//ifile is already declared as an input stream elsewhere

ifile.getline(beginline,19);               //Reads the line and puts it into beginlin
ifile.getline(endline,19);                    //Reads the next line and puts it into beginlin

begintok = strtok(beginline," ");                    //Tokenize beginline with a " "
begincharacters.push_back((const char&)begintok);     //Add the tokenised character form beginline to the end of the the vector begincharacters

while(begintok != NULL)               //Start a while loop to add the character of every tokenised string to the end of a vector
{
begincharacters.push_back((const char&)begintok);     //Add the tokenised character form beginline to the end of the the vector begincharacters
begintok = strtok(NULL," ");          //Tokenize beginline with a " "
}

endtok = strtok(endline," ");                    //Tokenize endline with a " "
endcharacters.push_back((const char&)endtok);          //Add the tokenised character form endline to the end of the the vector endcharacters

while(endtok != NULL)               //Start a while loop to add the character of every tokenised string to the end of a vector
{
endcharacters.push_back((const char&)endtok);          //Add the tokenised character form endline to the end of the the vector endcharacters
endtok = strtok(NULL," ");          //Tokenize endline with a " "
}

while(i < begincharacters.size())          //Enter a while loop to go through all of the characters in begincharacters array and print them out.
{
     cout << begincharacters[i];          //Reference to the begincharacters vector with [i]

     i++;                         //Increase i by 1 to then move onto the next character in begincharacters array
}
0
 
nearlyanexpertAuthor Commented:
Right...


The file is setup like the following:

*2*3*5*
*8*3*0*

I've just put a * in here to indicate where the spaces are more easily. There is only 1 character ever needed to be tokenized. What should happen is:

beginline reads the first line
endline reads the last line                    (as there are only ever 2 lines)

beginline is tokenized into the characters seperated by spaces in this case 2,3 and 5, and as they are, they are put into a vector called begincharacters. The same thing then happens with endcharacters.

Then it enters a while loop which is meant to spit out the characters of currently only the begincharacters, although it gives me a bit of tripe instead...


Hope that's better :)

NAE
0
 
nearlyanexpertAuthor Commented:
Forgot to add....there can be any number of characters on the lines in the file, although the chances of there being more than 7 is about 1 in a million net alone 19...
0
 
nearlyanexpertAuthor Commented:
Ah...I've fixed it...

Instead of:

begintok = strtok(beginline," ");
begincharacters.push_back((const char&)begintok);

while(begintok != NULL)
{
    begincharacters.push_back((const char&)begintok);
    begintok = strtok(NULL," ");
}


I have:


begintok = strtok(beginline," ");
while(begintok != NULL)
{
     cout << "begintok: " << begintok << endl;
     begincharacters.push_back(*begintok);
     begintok = strtok(NULL," ");
}

and the same done for endline...

Seems to fix it ok, may I ask for this to be turned into a PAQ? If anybody objects I'll split the points, although hope you don't mind Ged but I'll have to give MattAA the larger share as it was his comment that I've just read through and made me think a bit more about it, as my compiler gives no errors or warnings at all...

Thanks
Nearlyanexpert question by question...
0
 
nearlyanexpertAuthor Commented:
Actually...On second thoughts, I'll just split the points...
0
 
Computer101Commented:
Points reduced for a split.  You can now accept one experts comment as an answer.  After that, make another question in this topic area for the other expert.  Make the question for the amount of points intended.  Label the question, "Question for (Expert Name) and post this question number in the base of the question, i.e. For your assistance in question # --------


Computer101
E-E Admin
0
 
nearlyanexpertAuthor Commented:
Thanks....your solution didn't really help but it made me look again at that area which let me find a solution! :)

NAE
0
 
SpideyModCommented:
ged325,
Points for you have been placed at:

http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20531215.html

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now