Jump to content

Hey guys, I'm doing a project for my C++ in a Unix environment class and I'm having a bit of trouble tokenizing the input give to me.

The input is given in a .txt file which i will attach.    config.txt

 

The Tokenizer class was given to us by the professor. Tokenizer.cpp.txt    Tokenizer.h.txt

 

Here is the code I am using in my main to read from the file (just the Tables part atm, and by this point i have already skipped the first line that lets you know you are reading the table info)

 

 //Build new Tokenizer
Tokenizer tblTokenizer(line, " "); //line is being pulled from the file and " " is the delimiter 
string tblToken;
//Set token to next string
tblToken = tblTokenizer.next();
                
 cout << tblToken << endl;
 
While I expect the output to look like:
1 //Tablenum
2 //Num of people at table
2 //Tablenum
4 //Num of people at table
3 //Tablenum
2 //Num of people at table
4 //Tablenum
2 //Num of people at table
5 //Tablenum
2 //Num of people at table
6 //Tablenum
4 //Num of people at table
7 //Tablenum
6 //Num of people at table
8 //Tablenum
10//Num of people at table
 
and so on what i end up with is:
1
2
3
4
5
6
7
8
 
and so on, mean it is missing the last value in the line.
 
Any idea how to retain that value without typing a space at the end of each line in the config file?
 
I appreciate the help in advance. Thank you =]

 

Link to comment
https://linustechtips.com/topic/72439-c-tokenizer-issues/
Share on other sites

Link to post
Share on other sites

Ok, so you are reading line by line, and using the tokenizer (which btw, don't know if you did this for any particular reason but this line of code Tokenizer tblTokenizer(line, " ") might be better if written like Tokenizer tblTokenizer(line)...the default setting contains more delimiters).

 

The issue that I see is you are only calling .next once.  After tblTokenizer, you do

tblToken = tblTokenizer.next();
This will mean tblToken will only contain the first token.  Here is a quick demo of printing it.
 
Tokenizer tblTokenizer(line); //line is being pulled from the file and " " is the delimiter string tblToken;//Set token to next stringtblToken = tblTokenizer.next();while(tblToken != "") {    cout << tblToken << endl;    tblToken = tblTokenizer.next();}

0b10111010 10101101 11110000 00001101

Link to comment
https://linustechtips.com/topic/72439-c-tokenizer-issues/#findComment-992114
Share on other sites

Link to post
Share on other sites

Ah, okay.  If you professor told you to do it that way, then don't do the default way :P

 

(as an fyi, the default accepted things like \t...so maybe the processor is going to use a tab and doesn't want it tokenized)

0b10111010 10101101 11110000 00001101

Link to comment
https://linustechtips.com/topic/72439-c-tokenizer-issues/#findComment-995763
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×