This is a somewhat big project for me as I'm not very experienced in c++. The end goal is to read a text file, and read every word in the text file. Each time a new word is read, store it in a vector. If a word is repeated, keep a counter for how many times the word was used.
Right now, I have code that is supposed to read every word, and then give me a final count on the total number of words. For some reason it reads the first 4 and then stops.
Can any one help me see my problem? Thanks in advance
#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
int main()
{
int i = 0;
bool loop = true;
string line;
stringstream ss;
string output;
ifstream myFile ("const.txt");
if (myFile.is_open())
{
while(myFile.good())
{
getline (myFile,line);
ss << line;
while (loop)
{
std::string st;
ss >> st;
i++;
output+=st+" ";
if (st == "")
{
loop = false;
}
}
}
myFile.close();
cout << output << endl;
cout << "number of words " << i;
}
else cout << "unable to open";
system ("pause");
}
This is the output that I get
Provided by USConstitution.net
number of words 4Press any key to continue . . .
And here is the text file I'm reading from
http://www.usconstitution.net/const.txt