2011-07-08 19:15:27 UTC
#include
#include
#include
#include
using namespace std;
int main()
{
char control = 'y';//this variable is used by user to stay in or exit out of do-while loop
do
{
ifstream in_stream;
in_stream.open("test.txt");
cout << "The previous piece of advice is given below:\n\n";
char next;
while (!in_stream.eof())
{
in_stream.get(next);
cout << next;
}
in_stream.close();
cout << "Would you like to run the program again? (Y/N): ";
cin >> control;
while ((control != 'y') && (control != 'n'))
{
cout << "Incorrect input. Would you like to run the program again? (Y/N): ";
cin >> control;
control = tolower(control);
}
}while(control == 'y');
cout << "Press key to exit: ";
char exit;
cin >> exit;
return 0;
}
It works fairly well as long as the content of the file ends in a newline ("\n") but when the file ends in any other kind of character, let's say "3" for example, then the last character gets repeated twice. So for example if test.txt contains:
"1
2
3"
Then the output that's printed is:
"1
2
33"
When I actually mean to have it print:
"1
2
3"
Any suggestions? Thanks.