3 Replies - 356 Views - Last Post: 27 January 2010 - 04:08 AM Rate Topic: -----

#1 Chaosnub  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 19-October 09

New Line of Fail

Posted 27 January 2010 - 01:06 AM

So, I've been working on this program which takes a file with at least 2 lines, and then ciphers the text. I have it all working, except that I can't seem to create a new line.

Basically, the program takes a file which has something like this:
abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ

...and creates another file, that *should* look like this:
ijklmnopqrstuvwxyzabcdefgh
IJKLMNOPQRSTUVWXYZABCDEFGH

However, since I can't seem to properly get the new line, it's all just one large line of fail =/

Here's what I have, without whatever it is I need to create a new line (Oh, and this is in a UNIX environment):
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cassert>

using namespace std;

ios_base& skipws(ios_base&);

int main(int argc, char * argv[])
	{
		if(argc != 4)
			cerr << "Input Error. Usage: ./encipher plaintext.txt ciphertext.txt 8\n";

		else
		{
			ifstream plain(argv[1]);
			assert(!plain.fail());
			ofstream cipher(argv[2]);
			assert(!cipher.fail());

			int shift = atoi(argv[3]);
			char ch;
			while(plain >> ch)
			{
				if(ch >= 'a' && ch <= 'z')
				{
					ch = (((ch - 'a') + shift) % 26) + 'a';
					cipher << ch;
				}
				else
				{
					ch = (((ch - 'A') + shift) % 26) + 'A';
					cipher << ch;
				}
			}

			cout << "Operation Completed\n";

			plain.close();
			cipher.close();
		}
		return 0;
	}



I'm feeling I might need to rewrite the whole while loop, but wanted some more experience views before I messed with it further.

This post has been edited by Chaosnub: 27 January 2010 - 01:10 AM


Is This A Good Question/Topic? 0
  • +

Replies To: New Line of Fail

#2 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: New Line of Fail

Posted 27 January 2010 - 01:35 AM

I would suggest you read the input file a line at a time, so as to ensure the library routines do not skip over the end of line sequence.
Was This Post Helpful? 0
  • +
  • -

#3 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: New Line of Fail

Posted 27 January 2010 - 03:51 AM

This is not a great way to do this but if you want to keep it simple and maintain what you have already written how about just adding
if( ch='\n' ) {
     cipher << ch;
}

as your first test inside your while loop and alter the if() test as I imagine is obvious.

EDIT
Whoops
Didn't look carefully enough at how you were getting your input.
Just assumed you were getting your input via getc
http://www.cplusplus...ry/cstdio/getc/
or similar instead of reading your actual code.

Best to ignore the above and do things the right way using getline()
http://www.cplusplus...string/getline/
as set out by Martyn.Rae and Bench

Sorry for any confusion.

This post has been edited by janotte: 27 January 2010 - 04:34 AM

Was This Post Helpful? 0
  • +
  • -

#4 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 854
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: New Line of Fail

Posted 27 January 2010 - 04:08 AM

>> skips whitespace, so ch will never be equal to '\n'. the simplest way is to use getline with a string
std::string line;
while( std::getline(plain, line) )
    //TODO: step through 'line'.  or perhaps use std::transform 



As an aside - using assert to check user input is a bad idea. assert is intended to be used to catch programmer mistakes; in theory, the argument to assert should never yield false - adding it to your program is a way of checking that something which should always be true is always true, regardless of that program's flow or any user behaviour. - If the assertion ever fails then you know that your program is not working as intended.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1