Trouble saving a string to a file

1. Read the provided plain.txt file one line at a time. Because this file has
spaces, use getline (see section 3.8).
2. Change each character of the string by adding 4 to it.
3. Write the encoded string to a second file, such as coded.txt
4. The encoded file should have the same structure as the original file, if the
second line of the original file has 24 characters (including spaces) then
there should be 24 characters on the second line of the encoded file
(spaces will now look like '$').

The plain.txt file contains this:
This is a sample input file for COSC 1436
assignment #10. Your program should encode this
file by adding four to the AsCII value of each
character in it. In your encoded file the first character should
be 'X'. The last character of the first line should be ':'.
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'.

The main problem I'm having with this program is saving the string to the outfile correctly. The text displays correctly in the cmd like plain.txt says it should but when I save string plain; to coded.txt it just comes out with jm(i(/:/6

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

#include<iostream>
#include<string>
#include<fstream>
using namespace std;

int main()
{
	ifstream infile;
	ofstream outfile;
	string plain;

	infile.open("plain.txt", ios::in);
	if (infile)
	{
		while (infile)
		{
			cout << plain << endl;
			getline(infile, plain);
			for (int index = 0; index < plain.size(); index++)
			{
				plain[index] += 4;
			}
			
		}
		infile.close();
	}
	else
	{
		cout << "Input file open failed!" << endl;
	}
	outfile.open("coded.txt");
	if (outfile)
	{
		outfile << plain << endl;
	}
	else
	{
		cout << "Output file open failed!" << endl;
	}
	outfile.close();
	system("pause");
}
Last edited on
closed account (48T7M4Gy)
Your loops were incorrectly nested.

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
#include<iostream>
#include<string>
#include<fstream>

using namespace std;

int main()
{
    ifstream infile;
    ofstream outfile;
    string plain;
    
    outfile.open("coded.txt");
    if (!outfile)
    {
        std::cout << "Output file failed.\n";
        return 0;
    }
    
    infile.open("plain.txt", ios::in);
    if (infile)
    {
        while (infile)
        {
            getline(infile, plain);
            
            cout << plain << endl;
            for (int index = 0; index < plain.size(); index++)
            {
                plain[index] += 4;
            }
            
            outfile << plain << '\n';
        }
    }
    else
    {
        cout << "Input file open failed!" << endl;
        return 0;
    }
    infile.close();
    outfile.close();
    
    return 0;
}


plain.txt

This is a sample input file for COSC 1436
assignment #10. Your program should encode this
file by adding four to the AsCII value of each
character in it. In your encoded file the first character should
be 'X'. The last character of the first line should be ':'.
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'.


coded.txt
Xlmw$mw$e$weqtpi$mrtyx$jmpi$jsv$GSWG$587:
ewwmkrqirx$'542$]syv$tvskveq$wlsyph$irgshi$xlmw
jmpi$f}$ehhmrk$jsyv$xs$xli$EwGMM$zepyi$sj$iegl
glevegxiv$mr$mx2$Mr$}syv$irgshih$jmpi$xli$jmvwx$glevegxiv$wlsyph
fi$+\+2$Xli$pewx$glevegxiv$sj$xli$jmvwx$pmri$wlsyph$fi$+>+2
Xli$irgshih$jmpi$wlsyph$lezi$<$pmriw$sj$xi|x$nywx$pmoi$xlmw$sri0
fyx${mpp$fi$yrviehefpi2$Xli$pewx$glevegxiv$sj$xli$pewx$pmri${mpp
fi$e$+6+2
Last edited on
The order in which things are sequenced is incorrect.
Instead of this:
1
2
3
4
5
6
7
8
9
10
    while (infile)
    {
        cout << plain << endl;
        getline(infile, plain);
        for (int index = 0; index < plain.size(); index++)
        {
            plain[index] += 4;
        }
			
    }

this would be better
1
2
3
4
5
6
7
8
9
    while (getline(infile, plain))
    {
        for (int index = 0; index < plain.size(); index++)
        {
            plain[index] += 4;
        }
        cout    << plain << endl;
        outfile << plain << endl;	
    }
Topic archived. No new replies allowed.