Hello Community,
I really need help with the following problem, that, try as i might, can't get solved ... My current program is supposed to find and display the number of words found in a text file, based on user input. So, if the word is 'the', it should display the exact number.
The other part is to extract lines containing this word. If the word is 'and', with the following condition:
1 2 3 4
|
if (found = tmp.find(" " + sWord + " ") != string::npos)
{
cout << text + "\n";
}
|
It would display exactly only words that are separated by white-space at the beginning and end of the word, and only sentences are displayed matching the word.
If, however, the word is - say - "Players", and the word is at the beginning of a sentence, obviously, with the above condition, this sentence is never displayed. If my condition is like this:
1 2 3 4
|
if (found = tmp.find(sWord + " ") != string::npos)
{
cout << text + "\n";
}
|
Then, if I search for 'and', it would also display a sentence containing 'understand', which is not what I want ... And this is what I need help with. Also, if there is any better way to go about doing it, meaning without opening the file twice (the upper - to lowercase part will be one function for both tasks), to get a correct result for word count AND the extraction of sentences containing the word, I'm all ears ... :))
Lastly here is my code so far [please scroll down for the test-text]:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
#include <algorithm>
#include <string>
#include <fstream>
#include <iostream>
#include <iterator>
using std::string;
using std::fstream;
using std::cin;
using std::flush;
using std::cout;
using std::endl;
using std::ios;
int main()
{
string sWord = " ";
int cnt = 0;
string tmp = " ";
fstream txt("short.txt", ios::in);
string delimList = ":,!.?)(\"";
string text = "";
string wList = "";
cout << "enter sW: ";
cin >> sWord;
if (!txt.fail())
{
while (txt >> tmp)
{
wList = tmp + "\n";
for (unsigned int i = 0; i < delimList.size(); i++)
{
wList.erase(remove(wList.begin(), wList.end(), delimList.at(i)), wList.end());
tmp.erase(remove(tmp.begin(), tmp.end(), delimList.at(i)), tmp.end());
}
for (unsigned int i = 0; i < tmp.size(); i++)
{
tmp[i] = tolower(tmp[i]);
}
size_t found = tmp.find(sWord);
if (found !=string::npos)
{
if (tmp == sWord)
{
cnt++;
}
}
}
}
txt.close();
txt.open("short.txt", ios::in);
if (!txt.fail())
{
while (getline(txt, tmp))
{
text = tmp;
for (unsigned int i = 0; i < tmp.size(); i++)
{
tmp[i] = tolower(tmp[i]);
}
size_t found = tmp.find(sWord+"");
if (found = tmp.find(sWord + " ") != string::npos)
{
cout << text + "\n";
}
}
}
txt.close();
cout << cnt;
cin.get();
cin.ignore();
return 0;
}
|
test text:
Players of a video game do not look at the underlying code but at dynamically
generated audiovisual and tactile results based on it. They look at the mediated
plane and see the performance of the code. The code itself stays hidden behind
elaborate virtual worlds and interfaces, and the only time one might encounter it is when an error crashes the program and a debug message points to certain lines of broken code. Players do not have to understand the logic of the code but of the mediated game world. "Beyond the fantasy, there are always the rules,"argues Turkle (1984, 83), but from the vantage point of a player's experience, it is the fictional plane where the player makes sense of the game, the space of personal interpretation and assessment. Overwhelmingly, most game players stay on the level of the fantasy world when playing a video game. For example, they would not realize the fundamental logical difference between a version of Pac-Man (Iwatani 1980) running on the original Z80 microprocessor of the arcade board or a Java or C++ version of the game emulated on a Pentium processor under Windows.
(Michael Nitsche - Video Game Spaces Image, Play, and Structure in 3D Game Worlds)
(Copyright: 2008 Massachusetts Institute of Technology)