?
Solved

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

Posted on 2003-02-25
13
Medium Priority
?
315 Views
Last Modified: 2008-03-04
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
Comment
Question by:nearlyanexpert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
13 Comments
 
LVL 1

Accepted Solution

by:
MattAA earned 105 total points
ID: 8021549
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
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 8022675
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
 

Author Comment

by:nearlyanexpert
ID: 8023602
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
Technology Partners: 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!

 

Author Comment

by:nearlyanexpert
ID: 8023656
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
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 8025387
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
 

Author Comment

by:nearlyanexpert
ID: 8026686
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
 

Author Comment

by:nearlyanexpert
ID: 8026774
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
 

Author Comment

by:nearlyanexpert
ID: 8026790
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
 

Author Comment

by:nearlyanexpert
ID: 8027042
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
 

Author Comment

by:nearlyanexpert
ID: 8027055
Actually...On second thoughts, I'll just split the points...
0
 
LVL 1

Expert Comment

by:Computer101
ID: 8029401
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
 

Author Comment

by:nearlyanexpert
ID: 8029459
Thanks....your solution didn't really help but it made me look again at that area which let me find a solution! :)

NAE
0
 

Expert Comment

by:SpideyMod
ID: 8138873
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

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question