find last word in a file and count how often it appears.

Im having trouble finding a way to make code to find the last word in file, the code should work for every type of file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{
    string thefilename;
    ifstream file;
    cin >> thefilename;
    file.open(thefilename.c_str());
    if (file.fail())
    {
        cout << "Can't open file: " << thefilename << endl;
        exit(1);
    }
    string str;
    while (!file.eof())
    {
    string word;
    for (string word; file >> word;)
    {
        cout << word << endl;
    }
        str.find_last_of(word);
    }
    file.close(); 
    return 0;
}
Firstly, it would be better on line 5 if you used "getline" rather than "cin" because "cin" only gets characters up to a space rather than the entire thing and can leave characters in the input buffer. Like this:

getline(cin, thefilename);

There is also another error at line 9 which is the fact that the console outputs text but then it closes down simply because you have not told it to pause. A simple "cin.ignore();" can solve that issue.

As for the loop between lines 13 and 22, there is no need to use a for loop inside a while loop. The code should look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
    vector<string> str;
    while (!file.eof())
    {
    string temp;
    getline(file, temp);
    str.push_back(temp);
    }
    file.close(); 
    int lastwordpos = str[str.size() - 1].find_last_of(" ");
    string lastword = str[str.size() - 1].substr(lastwordpos, (str.size() - lastwordpos));
    cout << "The last word is " << lastword << endl;
    cin.get();
    return 0;


You'll need to include this preprocessor directive at the top of your source otherwise you won' be able to use a vector.

#include <vector>

Your code should now work if you replace your part of the code with this part.
Last edited on
Thank you, this was very helpful!
#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
#include <vector>
using namespace std;

//Main fall
int main()
{
string thefilename, lastword, text;
ifstream file;
getline(cin, thefilename);
file.open(thefilename.c_str());

if (file.fail())
{
cout << "Can't open file: " << thefilename << endl;
exit(1);
}

vector<string> str;
while (!file.eof())
{
string temp;
getline(file, temp);
str.push_back(temp);
}

file.close();
int lastwordpos = str[str.size() - 1].find_last_of(" ");
string lastwordis = str[str.size() - 1].substr(lastwordpos, (str.size() - lastwordpos));
cout << "The last word is " << lastwordis << endl;


// for (int i = 0; i < lastwordis.length(); i++)
// {
// lastwordis = lastwordis.substr(0,lastwordis.length()-1);
// }

cout << lastwordis << endl;
cin.get();

return 0;
}

when i run this code with text file i get this output: libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: basic_string
(lldb)


can someone tell me why


Sorry! That was my fault why the code didn't work. I didn't test it beforehand so there was bound to be an error in it. I've no solved the problem and this is the new code:

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
#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
#include <vector>
using namespace std;

//Main fall
int main()
{
	string thefilename;
	ifstream file;
	getline(cin, thefilename);
	file.open(thefilename.c_str());

	if (file.bad())
	{
		cout << "Can't open file: " << thefilename << endl;
		exit(0);
	}

	vector<string> str;
	while (!file.eof())
	{
		string temp;
		getline(file, temp);
		str.push_back(temp);
	}

	file.close();
	int lastwordpos = str[str.size() - 1].find_last_of(" ");
	string lastwordis;
	if (lastwordpos != string::npos) { // if a space exists
		lastwordis = str[str.size() - 1].substr(lastwordpos + 1, (str[str.size() - 1].length() - lastwordpos));
	} else {
		lastwordis = str[str.size() - 1];
	}
	cout << "The last word is " << lastwordis << endl;

	cin.get();
	return 0;
}
thank you so much!
You're welcome.
So... complicated. My advice would be the opposite of Tom56785: Don't use getline.

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
#include <unordered_map>
#include <string>
#include <fstream>
#include <iostream>

int main()
{
    std::string fname;
    getline(std::cin, fname);

    std::ifstream in(fname); // or fname.c_str() if no C++11 support.

    if (!in.is_open())
    {
        std::cerr << "Unable to open file \"" << fname << "\" for input.\n";
        return 0;
    }

    std::unordered_map<std::string, std::size_t> word_count;

    std::string word;
    while (in >> word)
        ++word_count[word];

    std::cout << "The last word in the file is \"" << word << "\"\n";
    std::cout << "It occurs " << word_count[word];
    std::cout << (word_count[word] == 1 ? " time.\n" : " times.\n");
}


Of course, care must be taken if "words" and "WoRdS" should be considered equivalent.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{    
   string s, s2;
   int count=0;
   cout << "file name= ";
   getline(cin,s);	 
   ifstream inf(s);
   if(!inf)
       { cerr << "could not be opened." << endl;   exit(1);  }
   while(inf)
       inf >> s;    
   inf.clear();
   inf.seekg(0);
   while(inf)
      {  inf >> s2;  if (s==s2)  count++;  }
   count--;
   cout << "count= " << count;   
   inf.close();  
   return 0; 
}

my example is case sensitive, a!=A .
and punctuation counted "hello" != "hello."
Topic archived. No new replies allowed.