So I am having a loop issue each time I type in a valid file name after telling my program I want to run it again. It says cannot open file and I do not know why.
I have created .txt files named test,test2,test3,test4. So when I type test.txt it reads the file and it will ask for another file name and I will type test2.txt but it fails to read it. Not sure why. Here is my code.
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
int count;// Number of word operators
char again;
ifstream inFile;// Data file
string filename,word;
do
{
// Get the filename from the user.
cout << "Enter the filename or type 'quit' to quit: ";
cin >> filename;
inFile.open(filename.c_str());// Attempt to open file
if(!inFile)
{
// If file wouldn't open, print message
cout << "Can't open input file" << endl;
}
if(inFile)
{
count = 0;// Initialize counter
while (inFile>>word)// While input succeeds . . .
{
count++;
}
cout << count << " words operators were found." << endl;
}
}while(filename != "quit");
return 0;
}