I'm writing a program that is supposed to get the word count of the passage using a text file. I am only able to get my program to grab the character count, rather than the word count. I am not sure on what I can do to fix the program.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string gettysburg_address;
string constitution_preamble;
string file;
string words;
string someMoreWords;
int count = 0;
ifstream fin("gettysburg_address.txt");
ifstream con("constitution_preamble.txt");
gettysburg_address = "gettysburg_address.txt";
constitution_preamble = "constitution_preamble.txt";
words = fin.get();
someMoreWords = con.get();
while (true) {
cout << "Enter your file name or type 'end' if you would like to stop: ";
cin >> file;
if (file == "gettysburg_address.txt") {
while (!fin.eof()) {
words = fin.get();
count++;
}
cout << "The word count of this document is: " << count << endl;
cout << endl;
count = 0;
}
else if (file == "constitution_preamble.txt") {
while (!con.eof()) {
someMoreWords = con.get();
count++;
}
cout << "The word count of this document is: " << count << endl;
cout << endl;
count = 0;
}
else {
cout << "Not a valid input. Please try the name of a text file." << endl;
cout << endl;
}
if (file == "end") {
return 0;
}
}
}