I need the user to enter a file and for as long as the user enters files that exist the file will loop. The program will break when the user enters a file that does not exist.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string currentfile;
int i = 0;
do {
cout << "Please enter a file name \n";
cin >> currentfile;
cout << currentfile << "\n";
ifstream myfile(currentfile);
if (myfile.good())
{
// display thenumber of characters, words, and lines in that file
myfile.close();
}
else {
cout << "break";
break;
}
i++;
} while(true);
// repeat while user enters valid file name
}
when i enter a file that exists, myfile.good()
returns good then if i try a file that does not exist the like myfile.good()
returns true again. If i start the program and i try first a file that does not exist then myfile.good()
returns false.
I do not know why after i enter a valid file myfile.good()
will continue to return true.