I don't know what I've done wrong.

Hello, I'm trying to write a program that encodes text by using what I've heard called a "Trifig cipher", for fun. Each text character is represented by 3 digits. The idea was to put the text file into an array, then loop through assigning a new, three-digit, array for each character and send the first,second, and third characters of said array into separate text files to do more magic on later. This is what I've done thusfar:


#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;


void main ()
{
string STRING;
ifstream infile;
infile.open ("testinput.txt");
while(infile)
{
getline(infile,STRING);

stringstream oss;
oss << STRING;

string ArrayString = oss.str();

int i;
for (i=0; i < ArrayString.length(); i++)
{
int Trifig [3]= {0,0,0};

if(ArrayString[i] = 'a' || 'A'){
int Trifig [3]={1,1,1};
}else if(ArrayString[i] = 'b' || 'B'){
int Trifig [3]={1,1,2};
}else if(ArrayString[i] = 'c' || 'C'){
int Trifig [3]={1,1,3};
}else if(ArrayString[i] = 'd' || 'D'){
int Trifig [3]={1,2,1};
}else if(ArrayString[i] = 'e' || 'E'){
int Trifig [3]={1,2,2};
}
//This goes on until I get to Z

int a = Trifig [0];
int b = Trifig [1];
int c = Trifig [2];

cout<<a<<b<<c<<endl; //this is here for me to make sure it's working, it'll be deleted later

ofstream fout;
fout.open ("firstdigit.txt");
fout<< a << endl;
fout.close ();

fout.open ("seconddigit.txt");
fout<< b << endl;
fout.close ();

fout.open ("thirddigit.txt");
fout<< c << endl;
fout.close ();
}
}
infile.close();
}

Now, my question is: why does my cout statement just show me a bunch of zeros? I know that's what I initialized Trifig[3] to, but if I don't initialize it to something, I get an error. I thought that my if statement would just overwrite the initialization each time it looped. Also, cout displayed double the number of lines as characters I had in the "testinput" file. I don't totally understand what "stringstream" and "str" did, I grabbed that from forum posts I read here. I've only taken a basic intro course, and I am a total noob, plagued by misconceptions.

Any help and/or insights would be very much appreciated. Thank you in advance.
I think you want to move int Trifig [3]= {0,0,0}; under using namespace std; unless you want it to reset to 0 each time you increase i
Last edited on
Oh jeez. I didn't even see that. The program still just outputs
a list of zeros though, after I changed that. Thanks though.
closed account (3qX21hU5)
Something else that pops outs is you use void main(), main ALWAYS returns a integer so this needs to be int main() and if your compiler is not shooting out a error for that I would get a new compiler ;p

And please use codetags (Hint: they are the <> off to your right when replying) it makes code much easier to read on the forums.
Last edited on
Yours is not looping, not sure why, made a lot of changes and put in some code to see the loops. Seems to be working but too tired to mess with it anymore tonight.

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
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int Trifig [3]= {0,0,0};
int a=0, b=0, c=0;
int main ()
{
string STRING;
ifstream infile;
infile.open ("testinput.txt");
while(infile) 
{
getline(infile,STRING);

stringstream oss;
oss << STRING;

string ArrayString = oss.str();

for (int i=0; i < ArrayString.length(); i++)
{
cout <<"i = " << i << endl;
cout << "Tx " <<Trifig [0]<<Trifig [1]<<Trifig [2]<<endl;
cout << "A1 = " << ArrayString[i] << endl;
if(ArrayString[i] == ('A')){
cout << "AAA" << endl;
//int Trifig [3]={1,1,1};
Trifig[0]=1;
Trifig[1]=1;
Trifig[2]=1;
cout << "TA " <<Trifig [0]<<Trifig [1]<<Trifig [2]<<endl;
cout << "A2 = " << ArrayString[i] << endl;
}
else if(ArrayString[i] == ('B')){
cout << "BBB" << endl;
// int Trifig [3]={1,1,2};
Trifig [0]=1;
Trifig [1]=1;
Trifig [2]=2;
cout << "TB " <<Trifig [0]<<Trifig [1]<<Trifig [2]<<endl;
cout << "A3 = " << ArrayString[i] << endl;
}
else if(ArrayString[i] == ('C')){
//int Trifig [3]={1,1,3};
Trifig [0]=1;
Trifig [1]=1;
Trifig [2]=3;
cout << "A4 = " << ArrayString[i] << endl;
}else if(ArrayString[i] == ('D')){
//int Trifig [3]={1,2,1};
Trifig [0]=1;
Trifig [1]=2;
Trifig [2]=1;
cout << "A5 = " << ArrayString[i] << endl;
}else if(ArrayString[i] == ('E')){
// int Trifig [3]={1,2,2};
Trifig [0]=1;
Trifig [1]=2;
Trifig [2]=2;
cout << "A6 = " << ArrayString[i] << endl;
}
//This goes on until I get to Z	

a = Trifig [0];
b = Trifig [1];
c = Trifig [2];
cout << "T0 " <<Trifig [0]<<Trifig [1]<<Trifig [2]<<endl;
cout<< "T1 " <<a<<b<<c<<endl; //this is here for me to make sure it's working, it'll be deleted later

ofstream fout;
fout.open ("firstdigit.txt");
fout<< a << endl;
fout.close ();

fout.open ("seconddigit.txt");
fout<< b << endl;
fout.close ();

fout.open ("thirddigit.txt");
fout<< c << endl;
fout.close ();
}
}
infile.close();
}
Last edited on
The loop seems like it's working now, but it's still putting out three zeros when the Trifig[] elements are accessed. The loop's definitely running, but it seems like the if statement isn't overwriting the initial values of Trifig[]. It should be doing that, right?
Last edited on
Topic archived. No new replies allowed.