There are two separate problems.
The first, there is a logic error in the code. That is made clearer if we add some indentation for legibility. Unchanged except formatting:
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
|
#include <iomanip>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>
using namespace std;
int main ()
{
ifstream SecretSociety;
SecretSociety.open("encoded_quotes.txt");
//remove all text except “encoded_quotes.txt”
if ( SecretSociety.fail() )
{
cout <<"encoded_quotes.txt" << endl;
exit (-1);
char ch;
SecretSociety >> ch;
while ( SecretSociety )
{
cout << ch << endl;
SecretSociety >> ch;
}
SecretSociety.close ();
return 0;
}
}
|
If the file
was not opened successfully, then at line 17 the exit function is called, ending the program.
On the other hand, if the file
was opened successfully, none of the statements in lines 15 to 28 will be executed at all. The program simply ends.
Either way, the useful part of the program, lines 19 to 25 can never be executed.
Action
1. Remove the closing brace at line 28. Instead, insert a closing brace at line 18.
2. Check the file path is correct. The file will fail to open usually because the program is looking in a different directory to where the file is located. This depends on how the program is run, usually, place the file in the same folder as the
.exe file. This may not be the same as the source code.
As for checking the fail bit, that should be ok as it stands. Checking
is_open()
would also work.
I'd also recommend changing the text at line 16 to something like "Error, could not open file: " and then the file name, so it is more meaningful.