Problem with numbers

We have 5 differern files that include text in txt format. We have to find and counting the last word or number from text file, how often it occurs in the text.


Example text 1 = This is what Mississippi is.
The output is: is 5 (the last word and the count at last)

Example text 2 = 0000000
The output is: 0 7

Example text 3 = This is a short test with only one line.
The output is: line 1

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
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<sstream>
using namespace std;

int main()
{
ifstream in_file;
string fileName, text, words, test;
stringstream buffer;

int counter = 0;

    cin >> fileName;

    in_file.open(fileName.c_str());
    if(in_file.fail())
    {
        cout << "Can't open file" << endl;
        exit(1);
    }

    while(!in_file.eof())
    {
        in_file >> text;
    }
    in_file.close();

    text.erase(std::remove(text.begin(), text.end(), '.'), text.end());
    text.erase(std::remove(text.begin(), text.end(), ','), text.end());
    text.erase(std::remove(text.begin(), text.end(), '!'), text.end());
    text.erase(std::remove(text.begin(), text.end(), '?'), text.end());
    text.find_last_of(" ");

    in_file.open(fileName.c_str());
    while(!in_file.eof())
    {
        buffer << in_file.rdbuf();
        test = buffer.str();

        getline(in_file, words);
    }
    in_file.close();

    size_t pos = 0;
    while(true)
    {
        pos = test.find(text, pos++);
        if (pos != std::string::npos)
        {
            counter++;
        }
        else break;
    }
    cout << text << " " << counter;
    return 0;
}
Last edited on
CodeBlocks gives me 0 error, 0 warnings , but what may be wrong?

And if there is a really helpful genius here, he may rewrite the code after line 31 .. :)
Last edited on
So what was the actual problem? Is the program not working?
I found out the reason for this not to work, the pos++ have to be ++pos and it seems to work with words but now the problem is with numbers..

.. if I try seven zeros:
0
0
0
0
0
0
0
The output is: 0 6 (but Mooshak only accepts: 0 7) :( :(
Last edited on
Topic archived. No new replies allowed.