Problem with string in Class

If i run this code with just the object zip3 it runs fine but if i use any Zipcode objects before the string variable no longer works. It wont output even before being passed to the constructor Zipcode(string barcode). Very Confused appreciate any help. I believe the problem is cause because the string variable first is no longer working properly but not sure what is causing this.

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <iostream>
#include <string>

using namespace std;

class Zipcode
{
public:
    Zipcode(int x);
    Zipcode(string barcode);
    void output();

private:
    void decode(string x);
    int deco_helper(char x[]);
    void read_check(char x[]);
    string encode();
    int zip[];
};


int main()
{
    string first = "110100101000101011000010011";
    int second = 63016;

    //Zipcode zip2(second);
    //zip2.output();

    Zipcode zip1("110100101000101011000010011");
    zip1.output();

cout << first << endl;
    Zipcode zip3(first);
    zip3.output();



    return 0;
}

//Function Definitions
Zipcode::Zipcode(int x)
{
    int temp;
    zip[0] = x/10000;
    temp = zip[0];
    x = x - (temp*10000);
    zip[1] = x/1000;
    temp = zip[1];
    x = x - (temp*1000);
    zip[2] = x/100;
    temp = zip[2];
    x = x - (temp*100);
    zip[3] = x/10;
    temp = zip[3];
    x = x - (temp*10);
    zip[4] = x;

}

Zipcode::Zipcode(string barcode)
{

    decode(barcode);

}

void Zipcode::output()
{
    for(int i=0;i<5;i++)
    {
        cout << zip[i];
    }
    cout << endl;
}

void Zipcode::decode(string barcode)
{

    char num[5][5];
    int y=0;

    for(int i=1;i<26;i++)
    {
        for(int z=0;z<5;z++)
        {
            num[y][z]=barcode[i];
            i++;
        }
        i--;
        y++;
    }
    for(int i=0;i<5;i++)
    {
        zip[i] = deco_helper(num[i]);
    }
}

int Zipcode::deco_helper(char x[])
{
    int temp=0;
    read_check(x);
    if(x[0]=='1')
        temp = temp + 7;
    if(x[1]=='1')
        temp = temp + 4;
    if(x[2]=='1')
        temp = temp + 2;
    if(x[3]=='1')
        temp = temp + 1;
    if(temp==11)
        temp=0;
    return temp;

}

void Zipcode::read_check(char x[])
{
    int count=0;
    for(int i=0;i<5;i++)
    {
        if(x[i]=='1')
            count++;
    }
    if(count!=2)
    {
        cout << "Read Error" << endl;
        exit(1);
    }
}
Last edited on
However if i add an unused string under string first (string junk) it works fine and seems to destroy that junk string which makes no sense to me.
Last edited on
Hello jimmy10,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



This is what I see when I tried to compile your code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Zipcode
{
    public:
        Zipcode(int x);
        Zipcode(string barcode);
        void output();

    private:
        void decode(string x);
        int deco_helper(char x[]);
        void read_check(char x[]);
        string encode();  // <--- Declared, but not defined.

        int zip[];  // <--- Needs a size.
};

Line 14 did produce a warning for me, but did compile. Without a size there is no elements in the array to work with.

Andy
Thanks for the tips on posting. Placing the size for int zip[5]; fixed my problem
how frustrating but cant blame anyone but myself lol
Hello jimmy10,

No problem.

I am not sure exactly how you are decoding the "barCode" string or what you are expecting for the results.

Andy
Hello Andy,

I was just working on a practice project from my book based on the old US Mail POSTNET Format which displayed zipcodes in a barcode of 27 tall or short lines. So string barcode would represent 1 for tall lines and 0 for short in a 27 digit string. I then used decode() and deco_helper() to convert the barcode to a 5 digit zipcode. I hadnt gotten that far yet but i used encode() to convert the zipcode back to a barcode.

Thanks again for your help.
Topic archived. No new replies allowed.