473,405 Members | 2,415 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

first prog first post

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;
}

Jul 23 '05 #1
2 1261
no***************@nowhere.net wrote:

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;
}
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #2
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 ;)
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Theron NightStar | last post by:
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++. ...
2
by: facicad | last post by:
I would like to set topmost another prog. from my program. Ex: I use AutoCAD, run my prog. from autocad. My prog. is topmost but went I would like to pick some object in autocad, I set TopMost to...
1
by: Rachel McConnell | last post by:
Hi, I am trying to import data using COPY, from a file containing thirty or so COPY commands each with 0 or more rows of data. Reason, I have a small data set I want to include into a database...
8
by: tvnaidu | last post by:
I am running prog with GDB - throws SIG32, why? @localhost config]# gdb prog GNU gdb Red Hat Linux (5.3.90-0.20030710.40rh) Copyright 2003 Free Software Foundation, Inc. GDB is free software,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.