Hi, I am trying to code for a "Book Cipher". I need to write code that encodes and decodes.
For encoding, I need to read a text file from the command-line and convert each character to a pair of numbers representing a line index(0-based) and a character index(0-based) for that line. Those two values together, when applied to the cipher file as a position, must lead to the same character being decoded.
For example there are two files to be read, (1)inputFile and (2)cipherFile:
inputFile contents:
my bad
cipherFile contents:
mary had
a little
lamb
I am trying to find the location of each character read from the inputFile in the cipherFile and report its location is stated above, lineIndex and characterIndex of that line. I have done this on paper but am having such a tough time coding it. I know, or at least I think I do, I need to open the files and read the inputFile character by character store them in a string arraay? and then find the occurences of those characters in the cipherFile. I am not sure how to do this and report its loacation.
For the example above the output could be:
2 2 'm' is found on line 2, character position 2 in the cipherFile
0 3 'y' is found on line 0, character position 3 in the cipherFile
1 1 ' ' a space found between 'a' and 'little' line 1, position 1
2 3 'b' is found on line 2, position 3 in the cipherFile
2 1 'a' is found on line 2, position 1 in the cipherFile
0 7 'd' is found on line 0, position 7 in the cipherFile
Any help would be greatly appreciated! I am still very new to programming and to the C++ language. Please, any guidance I can get would be greatly appreciated.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main()
{
unsigned row[256]{}, col[256]{};
ifstream incipher( "cipherFile" );
string line;
for ( int r = 0; getline( incipher, line ); r++ )
{
for ( int c = 0; c < line.size(); c++ )
{
unsignedchar i = line[c];
row[i] = r + 1;
col[i] = c + 1;
}
}
ifstream in( "inputFile" );
while( getline( in, line ) )
{
for ( unsignedchar ch : line )
{
if ( row[ch] )
{
unsigned r = row[ch] - 1, c = col[ch] - 1;
cout << r << " " << c << " '" << ch << "' is found on line " << r << ", position " << c << " in the cipherFile\n";
}
else
{
cout << ch << " is not found in the cipherFile\n";
}
}
}
}
2 2 'm' is found on line 2, position 2 in the cipherFile
0 3 'y' is found on line 0, position 3 in the cipherFile
1 1 ' ' is found on line 1, position 1 in the cipherFile
2 3 'b' is found on line 2, position 3 in the cipherFile
2 1 'a' is found on line 2, position 1 in the cipherFile
0 7 'd' is found on line 0, position 7 in the cipherFile
2 2 'm' is found on line 2, position 2
0 3 'y' is found on line 0, position 3
1 1 ' ' is found on line 1, position 1
2 3 'b' is found on line 2, position 3
2 1 'a' is found on line 2, position 1
0 7 'd' is found on line 0, position 7
my bad
thank you for your response. This has really cleared this up for me. I am curious though if I were to write the encoded text to a new file and then decode that encoded file to return the string "my bad" how would I do that?
For example using the exact same files as I mentioned above:
1. Open and read the file containing:
mary had
a little
lamb
2. Open and reading the now encoded file:
2 2
0 3
1 1
2 3
2 1
0 7
3. How would I find the characters associated with the lineIndex and characterIndex from the encoded file in the first file opened and then report the message to the console? Which should be "my bad"
That is already done by the line Code code( "cipherFile" );
which calls Class Code's constructor with a file named "cipherFile".
The encoding of your message from a file named "inputFile" is done by
1 2 3 4 5 6 7 8 9
ifstream in( "inputFile" );
for ( string line; getline( in, line ); )
{
for ( unsignedchar ch : line )
{
unsignedint r, c;
if ( code.encode( ch, r, c ) ) ...
}
}
which encodes successive characters ch into integers r, c. You can simply write those r, c (for "row" and "column") values to another file.
1 2 3 4 5 6 7
2. Open and reading the now encoded file:
2 2
0 3
1 1
2 3
2 1
0 7
You can use another ifstream to read successive r, c values in.
3. How would I find the characters associated with the lineIndex and characterIndex from the encoded file in the first file opened and then report the message to the console?
Not sure what you are asking. The code already does precisely that.
At the moment it checks the encoding/decoding by stuffing the pairs {r,c} in a vector and successively decoding those characters back (so recovering your original message "my bad").
Sorry, maybe this will better clarify.
What I'm trying to do is, instead of storing the pairs in a vector and then decoding, read in the encoded message from the two files and find the character corresponding to the 2 2, 0 3, 1 1, etc..
File1:
mary had
a little
lamb
File2:
my bad
Encoded message:
2 2
0 3
1 1
2 3
2 1
0 7
So, if my two input files are:
File1
mary had
a little
lamb
File2
2 2
0 3
1 1
2 3
2 1
0 7
I want to decode File2 using File1, find the character in File1
2 2 'm' is found on line 2, position 2
0 3 'y' is found on line 0, position 3
1 1 ' ' is found on line 1, position 1
2 3 'b' is found on line 2, position 3
2 1 'a' is found on line 2, position 1
0 7 'd' is found on line 0, position 7
Therefore the decoded message is "my bad"
I guess the encoding in reverse. I hope this makes sense.
The following lines take the pairs of integers (representing row and column) from the vector check. All you have to do is take those pairs from file rather than the vector.
1 2 3 4 5
for ( auto pr : check )
{
unsignedchar ch;
if ( code.decode( pr.first, pr.second, ch ) ) cout << ch;
}
We know what you want to do ... we just don't want to do ALL of it for you.
BTW, please don't create yet another thread on exactly the same topic.