Okay, I'm trying to do my homework, but my code isn't doing what I want it to. What I'm trying to do is have the user enter a file name and then the program will output the number of words in the file. It works fine the first time through the loop, but when prompted again, the file never opens correctly. Here is my code:
#include <iostream>
#include <fstream>
#include <cassert>
using namespace std;
int main()
{
int numberOfWords;
string fileName;
string wordString;
ifstream infile;
cout << "File to process (type quit to exit): ";
cin >> fileName;
while(fileName != "quit")
{
infile.open(fileName.c_str());
assert(infile);
infile >> wordString;
for(numberOfWords = 0; infile; numberOfWords++)
{
infile >> wordString;
}
infile.close();
cout << fileName << " has " << numberOfWords
<< " words." << endl << endl;
cout << "File to process (type quit to exit): ";
cin >> fileName;
}
}