I found the properly way to add a while so I can complete this exercise. However, there are 2 things that require a touch. The file output is displaying twice. The first time properly, and the second time in a single line ( I don't need this line to show up). The second issue is the account ++ function. It has to display the counting of 7 words but it's counting 8 instead. Why? could you help me with this. the issue is in the last while.
#include<iostream>
#include<fstream>//step#1
#include<string>
using namespace std;
int main()
{
string word, fileName;
int charcounter = 0, wordcounter = 0;
char character;
ifstream inData;// incoming file stream variable
cout << " Enter filename or type quit to exit: ";
cin >> fileName;
//loop to allow for multiple files data reads
while (fileName != "quit")
{
inData.open(fileName.c_str());//open file and bind file to ifstream variable
//loop for file not found validation
while (!inData)//filestream is in fail state due to no file
{
inData.clear();//clear the fail state
cout << "File not found. Enter the correct filename: ";
cin >> fileName;
inData.open(fileName.c_str());
}
inData >> character;//extract a single character from the file
cout << "\n*****************************\n";
while (inData)
{
cout << character;
inData.get(character);//extract the next character and the next character
charcounter++;
}
//Here is the loop that is missing something
//I was told to close the file
inData.close();
//open up the file again and add the while loop
inData.open(fileName.c_str());
while (inData)
{
cout << word;
inData >> word;//extract the next word and the next word
wordcounter++;
}
cout << "\n******************************\n";
cout << fileName << " has " << wordcounter << " words" << endl;
inData.close();//close the ifstream conection to the data file
charcounter = 0; //reset char and word counts
wordcounter = 0;
//port for next file or exit
cout << "Enter a filename or type quit to exit: ";
cin >> fileName;
}
return 0;
}