Class assignment - Decode

Hello I can normally do my class assignments no problem but today I am having an issue with one of them. I have a text document that has been encoded and I am to decode it and write the actual message to another file. I am currently reading the file and getting each individual char as its integer number. To decode the message I just need to add 3 to the characters integer. The issue is I dont know how to turn that integer back into a character.

Here is the text file:

"Brx duh vdg," wkh Nqljkw vdlg lq dq dqalrxv wrqh: "ohw
ph vlqj brx d vrqj wr frpiruw brx."
"Lv lw yhub orqj?" Dolfh dvnhg, iru vkh kdg khdug d jrrg
ghdo ri srhwub wkdw gdb.
"Lw'v orqj," vdlg wkh Nqljkw, exw lw'v yhub, yhub
ehdxwlixo. Hyhubergb wkdw khduv ph vlqj lw--hlwkhu lw eulqjv
wkh whduv lqwr wkhlu hbhv, ru hovh---"
"Ru hovh zkdw?" vdlg Dolfh, iru wkh Nqljkw kdg pdgh d
vxgghq sdxvh.
"Ru hovh lw grhvq'w, brx nqrz. Wkh qdph ri wkh vrqj lv
fdoohg 'Kdggrfnv' Hbhv'."
Rk, wkdw'v wkh qdph ri wkh vrqj, lv lw?" Dolfh vdlg,
wublqj wr ihho lqwhuhvwhg.
"Qr, brx grq'w xqghuvwdqg," wkh Nqljkw vdlg, orrnlqj d
olwwoh yhahg. "Wkdw'v zkdw wkh qdph lv fdoohg. Wkh qdph
uhdoob lv 'Wkh Djhg Djhg Pdq'."
"Wkhq L rxjkw wr kdyh vdlg 'Wkdw'v zkdw wkh vrqj lv
fdoohg'?" Dolfh fruuhfwhg khuvhoi.
"Qr, brx rxjkwq'w: wkdw'v txlwh dqrwkhu wklqj! Wkh vrqj
lv fdoohg 'Zdbv dqg Phdqv': exw wkdw'v rqob zkdw lw'v
fdoohg, brx nqrz!"
"Zhoo, zkdw lv wkh vrqj, wkhq?" vdlg Dolfh, zkr zdv eb
wklv wlph frpsohwhob ehzloghuhg.
"L zdv frplqj wr wkdw," wkh Nqljkw vdlg. "Wkh vrqj
uhdoob lv 'D-vlwwlqj Rq D Jdwh': dqg wkh wxqh'v pb rzq
lqyhqwlrq."


and here is the code I currently have:

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
#include <iostream> // cin, cout, ...

#include <fstream> // ifstream, ofstream, ...

#include <string> // string

#include <cassert> // assert()

#include <conio.h>

using namespace std; // use the standard namespace

int main() // start the main function

{

//create the filename var
string inFileName = "labnine.txt";
//open the in file
ifstream fin(inFileName.data());
//create a var for the char integer
int c;
//check to see if the in file opened
assert(fin.is_open());

while (!fin.eof()) {

// get character
c = fin.get();
//how to turn this into a char again
c = c+3;

}
getch();

}

// end of main function 
Ok so ive made some advancement on my issue, now my code looks like this

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
#include <iostream> // cin, cout, ...

#include <fstream> // ifstream, ofstream, ...

#include <string> // string

#include <cassert> // assert()

#include <conio.h>

using namespace std; // use the standard namespace

int main() // start the main function

{
string letter =
	"a b c d ef g h i j k l"
	"m n o p q r s t u v w x y z"
	"A B C D E F G H I J K L"
	"M N O P Q R S T U V W X Y Z";


//create the filename var
string inFileName = "labnine.txt";
string outFileName = "decoded.txt";
//open the in file
ifstream fin(inFileName.data());
//create a var for the char integer
int c;
//create a char to assing the decoded char to
char ch;
//assign a string to hold the decodoed message
string sent = "";
//check to see if the in file opened
assert(fin.is_open());

while (!fin.eof()) {

// get character
c = fin.get();

//check to see if the char is a letter
if (letter.find(c) != letter.npos){
//use the decode method char int value - 3
c = c-3;
}
//asign the decoded int value to a char
ch = c;
//add the char to the string
sent.push_back(ch);
}
//close the in file
fin.close();
//open up the out file
ofstream out;
out .open(outFileName);// make an assert to assure its open
// assign the decoded sentence to the out file
out << sent ;

getch();

} // end of main function  


However the passage is still not quite decoding properly it looks like this decoded (reference first post for message being decoded):

"?ouaresad,"theKnightsaidinanan^ioustone:"let
mesing_ouasongtocomfort_ou."
"Isitver_long?"Aliceasked,forshehadheardagood
dealofpoetr_thatda_.
"It'slong,"saidtheKnight,butit'sver_,ver_
beautiful.Ever_bod_thathearsmesingit--eitheritbrings
thetearsintotheire_es,orelse---"
"Orelsewhat?"saidAlice,fortheKnighthadmadea
suddenpause.
"Orelseitdoesn't,_ouknow.Thenameofthesongis
called'Haddocks'E_es'."
Oh,that'sthenameofthesong,isit?"Alicesaid,
tr_ingtofeelinterested.
"No,_oudon'tunderstand,"theKnightsaid,lookinga
littleve^ed."That'swhatthenameiscalled.Thename
reall_is'TheAgedAgedMan'."
"ThenIoughttohavesaid'That'swhatthesongis
called'?"Alicecorrectedherself.
"No,_ououghtn't:that'squiteanotherthing!Thesong
iscalled'Wa_sandMeans':butthat'sonl_whatit's
called,_ouknow!"
"Well,whatisthesong,then?"saidAlice,whowasb_
thistimecompletel_bewildered.
"Iwascomingtothat,"theKnightsaid."Thesong
reall_is'A-sittingOnAGate':andthetune'sm_own
invention."ΓΏ


Does anyone know why I have these _'s and other symbols as well as why I do not have spaces being put it?
Last edited on
@mike12255

The issue is I dont know how to turn that integer back into a character.


A char is just a small int - it doesn't matter whether you use the int value or the char literal in single quotes.

Just wondering what happens with your first code if you just write the character (plus 3) to the output file, taking account of the punctuation and spaces.

I am guessing there is no need for an array holding all the chars. Although the way you have done that as a string isn't right.

Another observation: Does the minus 3 on the char wrap around, rather than straight subtraction? Capital B should be a capital Y on output.

HTH



closed account (D80DSL3A)
unimportant content removed.
Last edited on
Topic archived. No new replies allowed.