Comparing arrays of strings doesn't work.


Hey Guys! I have a task where it's neccassary to compare two array of strings to eachother. However when I try to test the program, it prints out a higher number than 1 for some strings. Haven't seen something like this ever, because the results could only be true or false. Any suggestions what could be the problem?

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
#include <iostream>
#include <string>

using namespace std;

int lancok;
int i;
int n;
int j;

int main()
{
    //input the number of string pairs, and the strings
    cin >> lancok;

    string evo[30];
    string evett[30];
    bool help[60];
    int duplalancok = lancok*lancok;

    for(i=1;i<=lancok;i++){

        cin >> evo[i];
        cin >> evett[i];

    }
    //comparing the strings to eachother
    for(i=1;i<=lancok;i++){
     for(n=1;n<=lancok;n++){

        if(evett[i]!=evo[n]){

            help[n]=true;
        }
     }
    }
    //printing out the result of the comparison
    for(n=1;n<=duplalancok;n++){

        cout << help[n] << endl;
    }

    return 0;
}


example input:
4
fox rabbit
rabbit grass
bird seeds
fox bird

output:

1
1
1
1
52
23
106
255
255
255
255
36
0
0
0
68
Last edited on
Hello doncsoo,

The first thing I would try is to initialize all the variables when you define them.

It looks like the "help" array has whatever was i that block of memory when "help" was created and the 0s and 1s are likely the only part of the array that was sctually used.

Hope that helps,

Andy
Thanks Andy, that was the issue. :D
Topic archived. No new replies allowed.