Hello juliabrushett,
I dug into the program this morning. Before I started fixing it the program worked fine, but did not match the instructions.
I find it helps to break up the instructions so that you know what you have to work with. Some times I put line numbers to help.
Write a C++ program that does the following:
1. Creates a class called Paragraph. It will have one private member variable of type string.
2. Opens the text file and reads in the text and stores it in an instance of “Paragraph”.
3. Decode the message by subtracting 3 from the ASCII value for each character (For example, ‘M’ becomes ‘J’,
and ‘#’ becomes a space character).
4. Store the decoded message in another instance of “Paragraph”.
5. Output the decoded message to another text file.
If this is done successfully, you will see an English, decoded message in the new text file.
|
I set up the class this way:
1 2 3 4 5 6 7 8 9 10 11
|
class Paragraph
{
public:
Paragraph(); // <--- Default ctor.
~Paragraph(); // <--- Default dtor.
void SetText(std::string origTextUncoded);
std::string GetText();
private:
std::string origText; // <--- Changed to match directions.
};
| |
The default ctor and dtor are not needed, but I thing it is good form to include them in the class.
Putting the member functions after the class is OK, but I like to put them after main. Eventually you will learn to put the class in its own header file and the member functions in its own ".cpp" file. This makes things less confusing when they are separate.
Your while loop to read the file is not really needed unless you have more than one line to read. Should you find that the input file has more than one line the private variable of the class will need to be changed to something that will hold more than one string. I would use a vector of strings for this. Of course this is just future thought.
This is what I did with the first while loop:
1 2 3 4 5
|
while (getline(myfile, line))
{
decode = line; // <--- Changed. Nor really needed.
coded.SetText(decode);
}
| |
Actually the while loop can be shortened to just:
1 2
|
while (getline(myfile, decode))
coded.SetText(decode);
| |
After this where you have used variables that are defined in main will have to be changed to use the member functions of the classes and the private variable of the classes.
At the end of the program you use "system".
It is best not to use "system" anything in a program as this could leave the program vulnerable to attack by hackers who can use this. Also it tends to be specific to Windows and not everyone can use it. If it is your own program and only you will be using it that is OK, but do not let it out to everyone. An alternative you can use is:
1 2 3 4 5
|
// This next line may not be needed. If you have to press enter twice put a comment on the line.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
// Sometimes it is needed.
std::cout << "\n\n Press Enter to continue";
std::cin.get();
| |
While I am here I off these pieces of knowledge for you enjoyment:
Try to avoid using
using namespace std;
in your programs it may seem easy now, but
WILL get you in trouble some day.
It is better to learn to qualify what is in the standard name space with "std::" and then to learn what is in the standard name space now while it is easy. And not all at once to try to keep a job.
What you are most likely to use for now is "std::cout", "std::cin" and "std::endl". About a week or so of typing this and you will not even notice that you are doing it.
It is
ALWAYS a good practice and programming to initialize your variables. If your compiler is using the C++11 standards or after the easiest way to initialize variables is with empty {}s, e.g.,
int num{};
. This will initialize the variable to 0 (zero) or 0.0 for a double. A "char" will be initialized to "\0". "std::string"s are empty to start with and do not need to be initialized. Should you need to you can put a number between the {}s.You can also Initialize an array, e.g.,
int aNumbers[10]{};
. This will initialize all elements of the array to 0 (zero). A use of
int aNumbers[10]{ 1 };
will initial the first element to "1" and the rest of the array to "0".. Following the "1" with ", 2 ..." will initialize the array with the numbers that you have used up to the entire array.
Any questions let me know.
Hope this helps,
Andy