Garbage chars in string.

Hi. I've written this code for a program I'm working in. Basically, the objective is, given an input of a N number of variable-length DNA sequences, find the size of the biggest subsequence present in K sequences simultaneously. For that, I've started by preprocessing the DNA strings to numbers. (A=1, C=2, G=3, T=4). However, I get garbage chars and numbers, that I have no idea where they come from. The code is as follows. (I need to use dynamic arrays as I don't know a priori how many strings I will be going to work with, and their lenght.)

The code is as follows:

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
cin >> n >> k;
    
    const int cols = 100;

    // declaration
    int ** z;

    // allocation
    z = new int*[n];
    for(int i = 0; i < n; i++)
            z[i] = new int[cols];

    // initialization
    for(int j = 0; j < n; j++)
            for(int i = 0; i < n; i++)
                    z[i][j] = 0;
    
    x = new string[n];
    
    for(i=0;i<n;i++) {
        x[i]="";
    }
    
    for(int i=0; i<n; i++) {  //debug
        cout << x[i] << "z" << endl;
    }
    
    for(int i=0; i<n; i++) {  //obter input
        cin >> x[i];
    }
    
    for(int i=0; i<n; i++) {  //debug
        cout << x[i] << ",";
    }
    
    cout << endl;
    
    for(int i=0; i<n; i++) {  //debug
        for(j=0;j<30;j++) {
            cout << x[i][j] << ",";
        }
        cout << endl;
    }


I get the output:

1
2
3
4
5
6
7
8
9
10
11
AAAT
CCCC
AAAG
AAAT,CCCC,AAAG,
A,A,A,T,,,,,¡,S,,,,,,,,,,,,,,,,,,,,,
C,C,C,C,,,,,,,,,,,,,,,,,,,,A,A,A,G,,,
A,A,A,G,,,,,>,,,,|,4,$,a,|,4,$,a,ÿ,ÿ,ÿ,ÿ,,,,,<,,
1114000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
2222000000000000000000001113000000000000000000000000000000001010103000000000000000000000000000000000
1113000000000000000000000000000000001010103000000000000000000000000000000000000000000000000000000000
101


Sorry if this is confusing, I'm pretty lost myself xP. I think I will give up on preprocessing. I can't seem to find the error here.

Thanks in advance.
In line 39 you write j<30... no wonder why you get garbage
I think you want to write j < x[i].size()

And dou you know about STL containers? Here std::vector for example would be much more simple to handle...
http://www.cplusplus.com/reference/stl/
Thanks for the reply. I'm learning my ropes around C++, so I don't really know anything about vectors. My objective with the blocks tagged with //debug was to know what was contained in the strings. I wanted to initialize them to zero, them change to 1/2/3/4 depending on the char. Later, 0 would act as the EOL marker.

My doubt is this: if I tell it to print out the strings, it acts normally. If I tell him to print all the individual chars it spews out seemingly arbitrary garbage. I don't really understand this. Could you care to explain?
My doubt is this: if I tell it to print out the strings, it acts normally. If I tell him to print all the individual chars it spews out seemingly arbitrary garbage. I don't really understand this. Could you care to explain?


You aren't telling it to print "only" the individual chars. You're telling it to print 30 characters per string, no matter what is actually stored in those strings, and obviously there are not 30 characters stored in any of the strings.

Try:
1
2
3
4
5
6
   for(int i=0; i<n; i++) {  //debug
        for(j=0;j<30;j++) {
            cout << x[i].at(j) << ",";
        }
        cout << endl;
    }


If you're using operator[] to access a string, you're telling the string that you know what you're doing (and obviously, that isn't the case.)

http://www.cplusplus.com/reference/string/string/at/

Ok, thanks. I'll check into that.
Topic archived. No new replies allowed.