![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2004
Posts: 14
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
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,","); |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|