Having trouble outputting from a file

I'm using a playfair program, and i'm having trouble with outputting to my file. Everything outputs correctly, minus the encryption of my text file.

the "message.in" is just a generic text file with a random sentence nothing specific.

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;

ifstream inf("message.in");
ofstream out("reply.out");

struct node
{
	int i, j;
};

struct node search(char a[5][5],char p)
{
	struct node l;
	for(int u=0;u<5;u++)
	{
		for(int v=0;v<5;v++)
		{
			if(a[u][v] == p)
			{
				l.i = u;
				l.j = v;
				return l;
			}
		}
	}
}

string encryption(string pt,string key)
{
	//Creates the 5x5 required matrix
	char a[5][5];
	bool visited[26]={0};

	int p=0,q=0;

	//Creates the key input
	for(int i=0;i<key.length();i++)
	{
		if(visited[key[i]-65]==0)
		{
			a[p][q] = key[i];
			visited[key[i]-65] = 1;
			if(q+1 < 5)
				q++;
			else{
				p++;
				q=0;
			}
		}
	}

	for(char t='A';t<='Z';t++)
	{
		if(t!='J')
		{
			if(visited[t-65]==0)
			{
				a[p][q] = t;
				visited[t-65] = 1;
				if(q+1 < 5)
					q++;
				else
				{
					q=0;
					p++;
				}
			}
		}
	}

	for(int i=0;i<5;i++)
	{
		for(int j=0;j<5;j++)
			out<<a[i][j]<<" ";
		out<<endl;
	}

	//Encrypts Plaintext
	string ct = "";
	for(int r=0;r<pt.length();r=r+2)
	{
		struct node x = search(a,pt[r]);
		struct node y = search(a,pt[r+1]);
		// if same row
		if (x.i == y.i)
		{
			ct = ct + a[x.i][((x.j)+1)%5];
			ct = ct + a[y.i][((y.j)+1)%5];
		}
		// if same col
		else if (x.j == y.j)
		{
			ct = ct + a[((x.i)+1)%5][x.j];
			ct = ct + a[((y.i)+1)%5][y.j];
		}
		// diagonal
		else
		{
			ct = ct + a[x.i][y.j];
			ct = ct + a[y.i][x.j];
		}
	}
	return ct;
}

string keypreprocess(string k)
{
	string key = "";
	for(int i=0;i<k.length();i++)
	{
		if(k[i]!=' ')
		{
			if(toupper(k[i])=='J')
				key += 'I';
			else
				key += toupper(k[i]);
		}
	}
	return key;
}

string ptpreprocess(string p)
{
	string pt = "", pp = "";

	for(int i=0;i<p.length();i++)
	{
		if(toupper(p[i])=='J')
			pp += 'I';
		else if(p[i]!=' ')
			pp += p[i];
	}
	for(int i=0;i<pp.length();i+=2)
	{
		if(pp[i]==pp[i+1]){
			pt += toupper(pp[i]);
			pt += 'X';
			i--;
		}
		else
		{
			if(i==pp.length()-1)
			{
				pt += toupper(pp[i]);
				pt += 'X';
			}
			else
			{
				pt += toupper(pp[i]);
				pt += toupper(pp[i+1]);
			}
		}
	}
	return pt;
}

int main()
{
	string pt,key = "Tennessee";
	out<<"Plain Text: ";
	getline(inf,pt);
	pt = ptpreprocess(pt);
	key = keypreprocess(key);
	out<<pt<<" \nKey: "<<key<<endl;
	out<<encryption(pt,key);
}
I would say that the first thing you need to do is pay attention to your compiler warnings, you should be getting several. Never ignore warnings, they are a good sign of problems with your code.

1
2
3
4
5
6
7
8
9
10
11
 In function 'std::string encryption(std::string, std::string)':
40:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
83:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 In function 'std::string keypreprocess(std::string)':
112:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 In function 'std::string ptpreprocess(std::string)':
129:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
136:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
145:8: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 In function 'node search(char (*)[5], char)':
29:1: warning: control reaches end of non-void function [-Wreturn-type]
In function search(), if p isn't found there's nothing returned - but the function requires a type node returned. Note there's no need to use struct node for the return type. Just node is OK with C++. This is the main issue reported by the warnings (L29)
Last edited on
Topic archived. No new replies allowed.