Having Issue with output to text file.

OK this is one of my assignment for my computer science class. I have to decrypt a text file using it's position factor. I pretty much figure out how to decode it and output the file. but my issue is that whenever I have it change from ASCII to character and then put it in the text file. It doesn't go to the next line. So pretty much the sentence goes forever on the first line. Pretty much like this.

Example

This is the first line.This is the second line.This is the third line. and etc.

It should be like this

This is the first line.
This is the second line.
This is the third line.
and etc.

Heres my code so far.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	char ch;
	string input;
	fstream FileInput;

	fstream OutputFile ("Decrypt.txt");

	FileInput.open("nString.txt", ios::in);
	
	if (FileInput)
	{
		while (FileInput)
		{
		getline(FileInput, input, '\n');
		int position = 0;
		int i = 0;

			while (i < input.size())
			{
				//Get Character
				// Change character to ASCII #
				int ASCII = input[i];
				// Find position factor
				position = position + 1;
				int DASCII = position % 5;
				// Subtract Position Factor
				int Total = ASCII - DASCII;
				ch = Total;
				// Output to file
				OutputFile.put(ch);
				i = i + 1;
			}

		}
		FileInput.close();
		OutputFile.close();

	}
	else
	{
		cout << " File Error! \n";
	}

	return 0;
}

I figure using the getline(FileInput, input, '\n') would of fix it. But it didnt work. :(

Any hint at the right direction would be greatly appreciated.
Last edited on
After while (i < input.size()) loop add OutputFile.put('\n'); Getline descards delimeter character.
BTW even if you got newline into your string, your decryption would horribly mangle it.
Wow... I couldn't believe I didn't think of that. Thanks! it works!
I'm not sure what you mean by my decryption is going to mangle it.

Here's pretty much the text file before it's encrypted.

This is a sample input file for COSC 1436
assignment #10. Your program should encode this
file by adding the position factor to the AsCII value of each
character in it. In your encoded file the first character should
be 'U'. The last character of the first line should be '7'.
The encoded file should have 8 lines of text just like this one,
but will be unreadable. The last character of the last line will
be a '2'.


This is how it looks encrypted


Ujlw ju#e tcptlf"lrpvv#jimg#jos"FSSD"4837
buvmgoohrt!%44.![ryr!rusgscp$siqxpd!gqgoeg#xhju
gkoi c{#edekqk ujh$ppulxipp#jadvrv uq#xhf"DwCJK#zamwh$og"heci"
djdvadvhv jp#mt/"Lr zqxv fpfsdff#jimg#xhf"imrtv#ghbtdgtft#whpwoh
cg#+U(0#Xhf"oesu"flascfxes"rj ujh$fjtvx mkqi tjryle"ei (9*2
Ujh$eoerhee"imlf"vlovng$hbxh$8!nlret"rj ug{x kwvx mkni ujlw pph0
cww$wjno$bf"xrrfcgebmg1$Tig#patv#ghbtdgtft#sf!vki mcvx mkqi xkop
cg#e (4*2


I ran the program again and it came back just the way it was before I encrypted. :)
I really appreciate the help. I blame me being sick and the med's i'm taking for making me miss something so easy. haha.
Topic archived. No new replies allowed.