Using SetText and GetText()... what am I doing wrong??

I am attempting to write some code that will take an input secret message and decode it by taking the character - 3, displaying the message in the output. I believe the rest of my code is okay, but what is wrong with my SetText and GetText functions/calling/I don't know? When I run my program, the output is as follows:

The original text in decodeme.txt is:

Using the ASCII value conversions, the decoded message is:

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class Paragraph {
public:
	Paragraph();
	string GetText();
	void SetText(string origTextUncoded);
private:
	string origText;
};

Paragraph::Paragraph() {
}

string Paragraph::GetText() {
	return origText;
}

void Paragraph::SetText(string origTextUncoded) {
	origText = origTextUncoded;
}

int main() {
	string text;
	string decoded;
	string coded;
	string plainText;
	Paragraph code;
	Paragraph decode;
	ifstream inFS("decodeme.txt");

	inFS.open("decodeme.txt");
	if (!inFS.is_open()) {
		cout << "Could not open file decodeme.txt" << endl;
		return 1;
	}

	if (inFS.is_open()) {

		while (getline(inFS, text)) {
			decoded = text;
			code.SetText(decoded);
		}
		inFS.close();
		
		size_t i = 0;
		char ch;

		while (i < decoded.length()) {
			ch = decoded[i] - 3;

			plainText = plainText + ch;
			i++;
		}

		decode.SetText(plainText);

		cout << "The original text in decodeme.txt is:" << endl << decode.GetText() << endl;
		ofstream pfile("plainText.txt");
		pfile << plainText;

		cout << "Using the ASCII value conversions, the decoded message is:" << endl << decoded << endl;

		pfile.close();
	}

	else cout << "Unable to open file.";

	system("pause");
	return 0;
}
The GetText and SetText methods have nothing to do with it. In fact, the Paragraph class is totally useless. It's just some clutter around a string.

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
    ifstream fin("decodeme.txt");
    if (!fin) {
        cerr << "Could not open file decodeme.txt\n";
        return 1;
    }

    string cipherText;
    while (getline(fin, cipherText)) {
	
        string plainText;
        for (auto ch: cipherText)
            plainText += char(ch - 3); // but what if it's 'A', 'B' or 'C'?

        cout << "The original text in decodeme.txt is:\n";
        cout << cipherText << '\n';
        cout << "Decoded text:\n";
        cout << plainText << '\n';
    }

    return 0;
}

I believe the rest of my code is okay

I’m afraid the rest of your code is betraying you ;-)

There’re other issues, but the first one is your code doesn’t read your file, since it’s not being properly opened.
You open it here: ifstream inFS("decodeme.txt");
and then you try to open it again in the following line: inFS.open("decodeme.txt");

When you try to open() an already opened file:
http://en.cppreference.com/w/cpp/io/basic_filebuf/open
Return value
this on success, a null pointer on failure.

You don’t catch the error because you look for only a few of them, not all of them. To control every stream error condition you can check directly their state, in this case:
if ( ! inFS )
instead of if (!inFS.is_open())

I don’t want to give you further advice because I don’t want to spoil your fun, but feel free to ask again if you’re unsatisfied with your code.
Topic archived. No new replies allowed.