encoder problem

Dec 8, 2010 at 3:13pm
hey, im trying to make a simple encoder program that encodes text. here's what i have done already

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

int main(int argc, char *argv[])
{
    
    ifstream input("text.txt");
    char a[1000];
    input.read(a, 1000);
    ofstream output("encoded.txt");
    ofstream output2("debug.txt");
    output2<<a;
    for(int x = 0; x <= sizeof a;x++){
            switch(a[x]){
                         //lowercase
                         case 'a' : output<<"0"<<endl;break;
                         case 'b' : output<<"00"<<endl;break;
                         case 'c' : output<<"vb"<<endl;break;
                         case 'd' : output<<"z"<<endl;break;
                         case 'e' : output<<"zz"<<endl;break;
                         case 'f' : output<<"zzz"<<endl;break;
                         case 'g' : output<<"v"<<endl;break;
                         case 'h' : output<<"vv"<<endl;break;
                         case 'i' : output<<"4"<<endl;break;
                         case 'j' : output<<"jkll"<<endl;break;
                         case 'k' : output<<"j9"<<endl;break;
                         case 'l' : output<<"zdc"<<endl;break;
                         case 'm' : output<<"na"<<endl;break;
                         case 'n' : output<<"po"<<endl;break;
                         case 'o' : output<<"12"<<endl;break;
                         case 'p' : output<<"34"<<endl;break;
                         case 'q' : output<<"85"<<endl;break;
                         case 'r' : output<<"12"<<endl;break;
                         case 's' : output<<"64"<<endl;break;
                         case 't' : output<<"1"<<endl;break;
                         case 'u' : output<<"3"<<endl;break;
                         case 'v' : output<<"a"<<endl;break;
                         case 'w' : output<<"dsgf"<<endl;break;
                         case 'x' : output<<"haf"<<endl;break;
                         case 'y' : output<<"xbvcmnfwsr"<<endl;break;
                         case 'z' : output<<"yrets"<<endl;break;
                         //uppercase
                         case 'A' : output<<"783"<<endl;break;
                         case 'B' : output<<"563"<<endl;break;
                         case 'C' : output<<"4563"<<endl;break;
                         case 'D' : output<<"785632"<<endl;break;
                         case 'E' : output<<"456387"<<endl;break;
                         case 'F' : output<<"dr47d3rfhg4"<<endl;break;
                         case 'G' : output<<"3df56g4"<<endl;break;
                         case 'H' : output<<"64df4"<<endl;break;
                         case 'I' : output<<"dfg8h63"<<endl;break;
                         case 'J' : output<<"s6dgh4"<<endl;break;
                         case 'K' : output<<"gh654j1"<<endl;break;
                         case 'L' : output<<"as678f9"<<endl;break;
                         case 'M' : output<<"gf6hj21"<<endl;break;
                         case 'N' : output<<"vc78nb"<<endl;break;
                         case 'O' : output<<"rt3yu21"<<endl;break;
                         case 'P' : output<<"h78gfd9"<<endl;break;
                         case 'Q' : output<<"9b75hg4j6df5h4"<<endl;break;
                         case 'R' : output<<"x7vbxcv"<<endl;break;
                         case 'S' : output<<"ghj78k"<<endl;break;
                         case 'T' : output<<"sd978gf"<<endl;break;
                         case 'U' : output<<"g3h45"<<endl;break;
                         case 'V' : output<<"as987df"<<endl;break;
                         case 'W' : output<<"dfg98j7g4k"<<endl;break;
                         case 'X' : output<<"sd78gf"<<endl;break;
                         case 'Y' : output<<"g65jh"<<endl;break;
                         case 'Z' : output<<"s8d9f7g"<<endl;break;
                         //symbols
                         case ' ' : output<<"5498567ff"<<endl;break;
                         case '!' : output<<"soooo30302"<<endl;break;
                         case '?' : output<<"fasddddhggtryt"<<endl;break;
                         case '.' : output<<"2345dgfh5647"<<endl;break;
                         case ',' : output<<"873562345"<<endl;break;
                         case '-' : output<<"bddfgdfga"<<endl;break;
                         case '\0' : break;break;break;
                          
                   }
                }
            
            
    
    
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


anyways, text.txt is what you input for the program to encode, encoded.txt is the text encoded, and debug.txt just shows if the text is read correctly.
now it works fine, but after it is done with the text in text.txt it just adds a lot of random symbols and i cant understand why. any ideas?
Dec 8, 2010 at 5:27pm
When the loop reaches the '\0' character, it doesn't stop. Only the first break actually does anything, and that just brings you out of the switch statement. What you can do instead is write something like this:

case '\0' : x = 1001; break;

This means that the for loop will stop executing as well
Dec 8, 2010 at 5:30pm
also i should point out that sizeof(a) will always return 1000, regardless of how long the string inside it is. Actually i think you could use strlen(a) to give you the correct length of the string.
Dec 8, 2010 at 5:30pm
Yep i just tried strlen(a) and it works right, you can also just change the code in my previous post to:

case '\0' : break;

since the for loop will stop at the correct point anyway
Dec 8, 2010 at 7:11pm
thanks! i was searching for such a function! thanks very much!
Topic archived. No new replies allowed.