T
Theron NightStar
I am trying to teach myself c++. This is the first program I have ever
written that might have an practical use to me. I rather proud of it since
like implied - I have no real knowledge of c++.
Anyways, here is my problem - This prog does what it is supposed to (a very
very crude encryption/decryption of text files) but for some reason it
always duplicates the last character 2 extra times. If I were to run the
word
this
through it, it would come out
thisss
Please spare me the flames of wrath for mine stupidity - again, this was
simply to learn something about file i/o
Here is the prog source
// This is an encyrption/decryption program. Nothing fancy - it is a file
i/o
// learning exercise. it is a VERY VERY crude version. I will make it more
// modular and "proper" as I learn more.
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
string iFilename;
string oFilename;
char in_ch;
char out_ch;
ifstream iFile;
cout << "Please tell me the name of the Input File:\n";
cin >> iFilename;
iFile.open(iFilename.c_str());
if (! iFile.is_open())
{
cout << iFilename << " does not exist. Exiting now.\n\n";
cout << "Press enter to exit...";
cin.get();
return 1;
}
ofstream oFile;
cout << "Give me a name for the output file:\n";
cin >> oFilename;
oFile.open(oFilename.c_str());
if (! oFile.is_open())
{
cout << oFilename << "is not getting opened for some reason.\n";
cout << "Exiting now.\n\n";
cout << "Press enter to exit...";
cin.get();
return 1;
}
while (! iFile.eof())
{
iFile.get(in_ch);
out_ch = ~in_ch;
oFile << out_ch;
}
iFile.close();
oFile.close();
cout << "Press enter to exit...";
cin.get();
return EXIT_SUCCESS;
}
written that might have an practical use to me. I rather proud of it since
like implied - I have no real knowledge of c++.
Anyways, here is my problem - This prog does what it is supposed to (a very
very crude encryption/decryption of text files) but for some reason it
always duplicates the last character 2 extra times. If I were to run the
word
this
through it, it would come out
thisss
Please spare me the flames of wrath for mine stupidity - again, this was
simply to learn something about file i/o
Here is the prog source
// This is an encyrption/decryption program. Nothing fancy - it is a file
i/o
// learning exercise. it is a VERY VERY crude version. I will make it more
// modular and "proper" as I learn more.
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
string iFilename;
string oFilename;
char in_ch;
char out_ch;
ifstream iFile;
cout << "Please tell me the name of the Input File:\n";
cin >> iFilename;
iFile.open(iFilename.c_str());
if (! iFile.is_open())
{
cout << iFilename << " does not exist. Exiting now.\n\n";
cout << "Press enter to exit...";
cin.get();
return 1;
}
ofstream oFile;
cout << "Give me a name for the output file:\n";
cin >> oFilename;
oFile.open(oFilename.c_str());
if (! oFile.is_open())
{
cout << oFilename << "is not getting opened for some reason.\n";
cout << "Exiting now.\n\n";
cout << "Press enter to exit...";
cin.get();
return 1;
}
while (! iFile.eof())
{
iFile.get(in_ch);
out_ch = ~in_ch;
oFile << out_ch;
}
iFile.close();
oFile.close();
cout << "Press enter to exit...";
cin.get();
return EXIT_SUCCESS;
}