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.
#include <iostream> // cin, cout, ...
#include <fstream> // ifstream, ofstream, ...
#include <string> // string
#include <cassert> // assert()
#include <conio.h>
usingnamespace 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
#include <iostream> // cin, cout, ...
#include <fstream> // ifstream, ofstream, ...
#include <string> // string
#include <cassert> // assert()
#include <conio.h>
usingnamespace 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):
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.