Problem with searching array code

closed account (z1354iN6)
(1)I have two txt files. one with 6-bit binary codes and a corresponding letter, and the other with a coded message in aforementioned 6-bit code.
(2)I want to write a program to read the first txt file and store the 6-bit binary codes into one array and the corresponding letter into another array and finally the coded message into a third array.
(3)Then search each 6-bit string in the message array until it finds a match from the binary codes array
(4 and writes that letter from the letter array into a third external txt file.

So far I have (1) and (2). My code for (3) is either not searching or not writing the decoded message to the output file. I commented where the problem is

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void display(string arr[], int size){
		int i;
		for(i=0;i<size;i++)
			cout << arr[i] << endl;
	}
	

int main(int argc, char** argv) {
	ifstream infile1, infile2;
	ofstream outfile1;
	string bin[40], syb[40], msg[16];
	int i, j;
	infile1.open("code.txt",ios::in);
	infile2.open("message.txt",ios::in);
	outfile1.open("decoded.txt",ios::out);
	if (!infile1.is_open())
		{
	    	cout << "Error opening file.";
	    }
	if (!infile2.is_open())
		{
	    	cout << "Error opening file.";
	    }
	for(i=0;i<40;i++)
		infile1 >> bin[i] >> syb[i];
	for(i=0;i<63;i++)
		getline(infile2,msg[i]);
	
	outfile1 << "Decoder: " << endl;
	
	for(int i=0; i<16; ) // This code is not working
    	{
        	if(msg[i] == bin[j])
	            {
				outfile1 << syb[i];
	            i++;
	        	}
	        else
	        	j++;
	    }
return 0;
}
Last edited on
Should line 31 be,
for(i=0;i<16;i++)
as your assignment at line 16 is,
msg[16];
not 63
?
And what about the j in line 38 did you assign it to zero?

Maybe something more like this?
1
2
3
4
5
6
7
8
9
	cout << "Decoder: " << endl;
	
	for (int i = 0;i<16;i++){ 'for each message
            for ( int j = 0; j<40; j++){
                if (msg(i) == bin(j) ){
			cout << i << "  " << msg(i) << "  " << syb(j);
                }
            }
        } 
Last edited on
Topic archived. No new replies allowed.