| johnnydiamond08 (21) | |||
|
Hello, I currently am having a problem figuring out how to compare the zip string to another variable checking to make sure it has only 5 or 10 characters. Please all input would be greatly appreciated. I'm not entirely sure I'm doing it correctly in the first place so all help will be greatly appreciated.
| |||
|
|
|||
| Chervil (812) | |||
Line 1 makes sense. But line 2, has the mysterious value 13 in there. Instead, you could find the next comma, and use the portion between the pairs of commas as the address. Similarly with the others, line 4 should use the substring from the last comma to the end. If you want to be sure that the zip code contains just 5 or 10 characters, you might then want to check whether there are leading or trailing spaces. After that, check the length of what remains. | |||
|
|
|||
| johnnydiamond08 (21) | |
| The value 13 would just take me to the comma. I was assuming if I tried to find the comma again it would just constantly bring me to the first one. Could you give me any kind of example I would really like to understand this concept better. | |
|
|
|
| Chervil (812) | |||||
|
My point was, what would happen if the program needed to work with different data? You need a general-purpose solution, where the computer does the work, not one which is hand-crafted for each different set of data. So what's the answer? Well, the find command accepts different parameters, including the position from which you wish to start the search. http://www.cplusplus.com/reference/string/string/find/ Example:
Having said that, there are other ways to tackle this problem. Here's one using stringstream. http://www.cplusplus.com/reference/sstream/
| |||||
|
Last edited on
|
|||||
| johnnydiamond08 (21) | |
| Thank you so much! My professor doesn't do as good of a job in explaining as I'd like. I sincerely hope that you sir/madam have a wonderful day. | |
|
|
|
| Chervil (812) | |
| Thanks. You can call me 'sir' - in the very informal sense of course. | |
|
|
|