473,623 Members | 2,447 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

first prog - first post

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(iFil ename.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(oFil ename.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
4 1444
* Theron NightStar:
while (! iFile.eof())
{
iFile.get(in_ch );
out_ch = ~in_ch;
oFile << out_ch;
}


Consider an in-file that contains a single character 'o'. First
time around there's no end-of-file, the loop body executes, and
the character is thereby copied to the out-file. End-of-file has
still not been detected because no attempt has been made to read
beyond the last character, so the loop goes one more time. This
time the 'get' operation does not change 'in_ch' because there's
no more data in the file. Instead the internal end-of-file flag
is set. But the loop body proceeds to copy the character to the
out-file. Now end-of-file is set and the loop terminates.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2
sorry bout the double post. Apparently one of my news readers had a
copy of this lying around waiting to go out and I didn't catch it - my
apologies
Jul 23 '05 #3
This is the offending section of code:
while (! iFile.eof())
{
iFile.get(in_ch );
out_ch = ~in_ch;
oFile << out_ch;
}

Instead of using iFile.eof(), you should rewrite the loop like so:
while (iFile >> in_ch)
{
out_ch = ~in_ch;
oFile << out_ch;
}

Alf P. Steinbach has provided a very good explanation of why this is
the case.

HTH,
Philip

Jul 23 '05 #4
In article <11************ ********@g47g20 00cwa.googlegro ups.com>,
<ph********@mac osx.com> wrote:

Instead of using iFile.eof(), you should rewrite the loop like so:
while (iFile >> in_ch)
{
out_ch = ~in_ch;
oFile << out_ch;
}


Or if he doesn't want to skip whitespace (as >> does), he can use

while (iFile.get(in_c h))
{
// ... etc.
}

--
Jon Bell <jt****@presby. edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 23 '05 #5

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

Similar topics

0
4919
by: Ben Eisenberg | last post by:
I'm trying to run a php script setuid. I've tried POSIX_setuid but you have to be root to run this. The files are located on a public access unix system and have me as the owner and nobody as the group. Rather than make them group writable where in anybody could make a script and write to my files i would like to make them setuid. I tried making a c prog with the setuid function. I used chmod and made it setuid. I called it with the system...
2
1276
by: nooneinparticular | last post by:
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...
2
1853
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 false and I would like to autocad bring to front, but for autocad object, it not have TopMost properties
1
3531
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 with an identical schema, with existing data. I figured a good way to do this would be to use pg_dump on the small dataset, trim out the table definitions and constraints, and run the resulting file. (I also reordered the COPY statements to...
1
1277
by: ניר | last post by:
Hello, I've already sent such a messege, hopping to be answered shortly this time. I am practicing c/c++ programming using microsoft visual c++ express 2005 express edition. Anyway, unlike other compilers there is no option to actually run the prog. (Ctrl+f5,f7 in vs). What I meen is that I can't find a way to execute a prog. thanks, Hezy.
2
1992
smartway
by: smartway | last post by:
Can we use c++ code in c prog or can we include .cpp in c prog. ? If yes ........ How can we do this?
4
1390
by: Signeg | last post by:
Hey guys, I have some work for university and i never used C++ in my life so i'm kinda lost.. But i've still managed to write something woth it. But too bad for me the prog does compile and lunch propely, but once i enter "-1" for the second while, nothing more is happening.. This is the code http://rafb.net/paste/results/rHKKIF94.html Could you guys tell me whats wrong because it looks okay for me :(
3
1498
by: McCarthy | last post by:
Im writing a command-line program in unix. Whenever I run the prog in unix it gives me 49 output files instead of the user entered amount. Im not sure what the problem is with my main prog the loop looks fine int main(int argc, char * argv) { // Command Arguements // Usage: <program> <policy file> <gen file prefix> <num generations> // argv is the name of the policy file // argv is the string that the generation output file...
8
9505
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, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for...
0
8231
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8168
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8672
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8614
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6107
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5561
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4075
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2603
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1474
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.