I have been asked to save my values to an external file in c++ I am having difficulty having my code access the .txt file and was wondering if anyone could identify what I am missing. In this case the code is taking a string from my .txt file and encrypting it in a very basic manner. For some reason I am getting a bunch of random characters as my original input when I would Like it to simple encrypt "John Doe"
# include<fstream>
#include<iostream>
#include <string>
using std::ifstream;
using std::ofstream;
using std::endl;
using namespace std;
int const LENGTH_OF_STRING = 10;
int main() {
ifstream instream;
ofstream outstream;
instream.open("infile.txt");
outstream.open("outfile.txt");
char myMsg[LENGTH_OF_STRING];
char encryption[LENGTH_OF_STRING];
cout << myMsg; // this is returning a crazy string of characters therefore I must be missing the command to let the program know where to get the information from
for (int i = 0; i < strlen(myMsg); i++) {
int asciiNum = (int)myMsg[i];
int newAsciiNum = asciiNum + 100;
char newEncryption = (char)newAsciiNum;
encryption[i] = newEncryption;
outstream << myMsg[i] << " = " << asciiNum << "\t"
<< " new ascii number = " << newAsciiNum << "\t"
<< "New encrypted character = " << newEncryption << endl << endl;
}
outstream << endl << "encrypted message = " << encryption << endl << endl;
instream.close();
outstream.close();
system("PAUSE");
return 0;
}
Any help would be greatly appreciated
P.S. my infile.txt looks like:
myMsg = "John Doe";
encryption = " ";