I'll just give some advice about main function() and return to other later as I do not have time right now to check algorithms in other functions.
1) Open files in filestream constructors:
std::ifstream fin("C:\\wordIn.txt");
2) You do not need to close files manually, file stream destructor does it.
3) you do not need to return manually anything from main
4)
exit() is tricky. It can lead to loss of data and unexpected results if used carelesly. If you are in
main(), you should just return
5) Two conditional operators are mostly the same, you can merge them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
int main()
{
std::cout << " Results for Project 4:\n\n";
std::ifstream fin("C:\\wordIn.txt");
std::ofstream fout("wordOut.txt");
if (not (fin && fout)) {
if(!fin) std::cout << "\n Input file opening failed.\n";
if(!fout) std::cout << "\n Output file opening failed.\n";
system("pause");
return EXIT_FAILURE;
}
decoder(fin);
sentence_fix(fin, fout);
system("pause");
}
|
In decoder you do not really need two variables, one will be enough:
1 2 3 4 5 6 7 8 9 10 11
|
void decoder(std::ifstream& in_stream)
{
char letter;
std::cout << "\n The decoded word is: ";
for (int y = 0; y < 6; y++) {
in_stream.get(letter);
letter += 10; //increase letter by 10
std::cout << letter;
}
std::cout << '\n';
}
|
Additionally you can drop middleman altogether:
1 2 3 4 5 6 7
|
void decoder(std::ifstream& in_stream)
{
std::cout << "\n The decoded word is: ";
for (int y = 0; y < 6; ++y)
std::cout << char(in_stream.get() + 10);
std::cout << '\n';
}
|
Ok, now to the last one.
1) As usual,
letter is not needed, operate on next directly, or just drop the middlemn again and outut uppercase letters directly:
out_stream.put(toupper(next));
2) Conditions as
1 2 3
|
if (isupper(next)) {
letter = tolower(next);
out_stream.put(letter);
|
are meaningless:
tolower has no effect on characters for which
isupper returns false, so you could just do:
1 2
|
letter = tolower(next);
out_stream.put(letter);
|
without fears. Combine it with (1) for further improvement.
3)
1 2 3 4 5
|
in_stream.get(next);
while (!in_stream.eof()) {
//...
in_stream.get(next);
}
|
First of all mandatory notification about dangers of looping on eof. In this case however it will not lead to any error, but it is way better to loop on read operation:
1 2 3
|
while(in_stream.get(next)) {
//...
}
|
4) Avoid conditional operators with empty
then block. If needed, just negate codition to move work from
else to
then. I do not know if you need to write own routine to skip whitespaces or can use standard one, so I leave it at that. Additionally, do not rely on numeric values for specific characters as they are subject to change. Just use character literals.
5) Too many braces on line 96. It is harder to read that way.
In the end:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
void sentence_fix(std::ifstream& in_stream, std::ofstream& out_stream)
{
int word_count = 0;
char next;
in_stream.get(next);
out_stream.put(toupper(next));
while (in_stream.get(next)) {
if (not (isspace(next) && in_stream.peek() == ' '))
out_stream.put(tolower(next));
if (!isspace(next) && (in_stream.peek() == ' ' || in_stream.eof()))
word_count++;
}
std::cout << "\n Total words in file = " << word_count << '\n';
}
|