Hi, all
i have one assignment about using some of the STL to read from .txt file and count the words and find the topnwords etc.
So i implement my class like that:

class A
{
public:
    A( const string& filename )
    {
            ifstream file( filename.c_str() ) ;
      string word;
     while( file >> word)
      _word_list.push_back(word);
    }
    size_t wordCount(const string& word) const;
private:
    vector<string> _word_list;
};

So the wordCount is to find how many word occurrence in the file
so when i implement the wordcount function, there should have one feature to judge the puctuations, so i use ispuct() function to judge it.
But i got compile error:

for(size_t i=0; i<_word_list.size(); i++)
  {
    if(!ispunct(_word_list[i])) 
    {
     //do something
    }
   }

error information:

Error	1	error C2664: 'ispunct' : cannot convert parameter 1 from 'const std::basic_string<_Elem,_Traits,_Ax>' to 'int'

That's because the ispunct() -function is expecting an int as argument, and your vector is of the type string ...

What happens if you use static_cast<int>(_word_list[i]) ??

Hope this helps !

Just ignore my previous post and take a look at the following example:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
	vector<string> test(1); /* This vector will contain 1 string */
        /* We assign a value to the string */	
        test[0] = "Ab.d";
        /* We print the ASCII code of the first character in the string */	
        cout << static_cast<int>(test[0][1]) << endl; 
        /* We check whether the 3rd character of the string is a punct */
	if(ispunct(static_cast<int>(test[0][2])))
	{
		cout << "yes it was a punct" << endl;
	}
	return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.