first prog first post

N

nooneinparticular

I sat down last night and decided to try and teach myself something
about programming in C++. I have always had an interest in it, so I
went for it. This is what I have to show for my efforts.

What I came up with is simply nothing more than a text
encrypter/decrytper of kindergarden proportions ;)

My problem is this - anytime the prog goes through it's routine, which
it does rather well - the out put of the decryption always stutters
the last character. If the last word is

this

it will always come out with

thisss

It repeats the last char twice. Please pardon any stupidity in my
questions (see above). Here is the 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;
}
 
K

Karl Heinz Buchegger

I sat down last night and decided to try and teach myself something
about programming in C++. I have always had an interest in it, so I
went for it. This is what I have to show for my efforts.

What I came up with is simply nothing more than a text
encrypter/decrytper of kindergarden proportions ;)

My problem is this - anytime the prog goes through it's routine, which
it does rather well - the out put of the decryption always stutters
the last character. If the last word is

this

it will always come out with

thisss [snip]

while (! iFile.eof())
{
iFile.get(in_ch);
out_ch = ~in_ch;
oFile << out_ch;
}

Your usage of eof() is wrong.
eof() becomes true only *after* you tried and failed to read past
the end of file.
eof() is ment to be used in another way: After a read operation has failed,
figure out why it failed.

So the C++ way to code a reading loop is

while( data can be read ) {
process data
}

if( !eof() ) {
// something bad has happend
}

All read operations support this by returning something that can be used
to test if the operation worked or failed:

while( iFile.get( in _ch ) ) {
out_ch = ~in_ch;
oFile << out_ch;
}

if( !iFile.eof() ) {
cout << "Error during read!" << endl;
}
 
N

nooneinparticular

Thank you very very much. That was something that was neglected to be
mentioned in the sources I was reading. It makes perfect sense to me
now. Again - thank you very much. And while it might be obvious to
you, I even figured out why it was doing it the magic number of 2
times ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top