String comparision problem

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

#include <iostream>
#include <string>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <iomanip>

using namespace std;

int main ()
{

	//put your code after this line
    string s = "Tom Smith,1 Main Street,Rochester,New York,14623";

    // printing original string
    cout <<  "Original string <" << s << ">" << endl;

    string szInput[5];
    int locOfCma = s.find( ',' );

    string name =  s.substr(0,locOfCma);
    string address = s.substr(locOfCma + 1,13);
    string town = s.substr(locOfCma + 15, 30);
    string zip = s.substr(locOfCma + 34, 5);

    // use getline, ignore and replace.

    do
    {



    //replaces comma after New York.
    town.replace(18,1, " ");
    // inserts space after Rochester,
    town.insert(10, " ");

    int atoi ( const char * zip);

    cout << name << endl;
    cout << address << endl;
    cout << town << endl;
    cout << zip << endl;

    int strlen( const string * zip );


    // Error checking for Zip

    } while (strcmp (zip,szInput) != 0);





	//put your code before this line

    //keep output window open
    return 0;
} //end main




1
2
3
4
    string name    = s.substr(0,locOfCma);
    string address = s.substr(locOfCma + 1,13);
    string town    = s.substr(locOfCma + 15, 30);
    string zip     = s.substr(locOfCma + 34, 5);

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.
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.
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:
1
2
3
4
5
6
7
8
9
10
    int comma1 = s.find(',' );
    int comma2 = s.find(',',comma1+1);
    int comma3 = s.find(',',comma2+1);
    int comma4 = s.find(',',comma3+1);

    string name    = s.substr(0,comma1);
    string address = s.substr(comma1 + 1, comma2 - comma1 - 1);
    string town    = s.substr(comma2 + 1, comma3 - comma2 - 1);
    string town2   = s.substr(comma3 + 1, comma4 - comma3 - 1);
    string zip     = s.substr(comma4 + 1);


Having said that, there are other ways to tackle this problem. Here's one using stringstream. http://www.cplusplus.com/reference/sstream/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    stringstream ssin(s);
    const char comma = ',';

    string name;
    string address;
    string town;
    string town2;
    string zip;

    getline(ssin, name, comma);
    getline(ssin, address, comma);
    getline(ssin, town, comma);
    getline(ssin, town2, comma);
    getline(ssin, zip, comma);

Last edited on
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.
Thanks. You can call me 'sir' - in the very informal sense of course.
Topic archived. No new replies allowed.