Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 17th, 2005, 10:42 PM   #1
Mortavitch
Newbie
 
Join Date: Nov 2004
Posts: 14
Rep Power: 0 Mortavitch is on a distinguished road
String Tokenization

Ok I have a program that reads in a .dat file. The file consists of fields that contain things like customer name, street addy, city, state, zipcode, and so on. It is my job to print out a report for each customer in the file. I have the entire report being printed, but I realized that I have a problem with my city output. Scine I have to allot 25 bytes to the city string outputing was getting all messed up with my spacing because it printed alot of spaced until the 25 bytes were all filled up. It would look this this:

Name
Street Addy
City , State Zip

Well I tokenized the city string so it would end at the first white space it encountered.

[ CODE ]
char *cCity;
cCity = strtok(sCity," ");
[ /CODE ]

The problem with this is that some city name have 2 words in them (Las Vegas, New York, St. Paul, ext) and when you go to output them they only have the first word of the two. So I did this:

[ CODE ]
char *cCity1;
char *cCity2;
cCity1 = strtok(sCity," ");
cCity2 = strtok(NULL," ");
strcat(cCity1, cCity2);
cout << cCity1;
[ /CODE ]

The problem with this is that not all of the cities have 2 words to them. So when the program encounters a 1 word city name it just crashes. What could I do that I can get the output to be in the format of:

Name
Street Addy
City, State Zip


Thanks for your help.
__________________
It's too bad that stupidity isn't painful
Mortavitch is offline   Reply With Quote
Old Feb 18th, 2005, 3:44 AM   #2
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
my advise would be to use some other form of type seperator other then white space, use a comma or somehting similar then you can get the strtok to add until it finds the instance of that token,

although i cant find anything on that, i would guess something like

 

char *city;
city = strtok(sCity,",");
Berto is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:57 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC