Strings and Operands

I need help figuring out how to make this program work. I created this program in order to read the number of words of any text file inputted. The last function is giving me trouble though because every time I run the program I get the error:

'!=' : no operator found which takes a left-hand operand of type 'std::string' (or there is no acceptable conversion)

It if referring to line 142:

if ( str != '\n' ) // test for end of line word

Please help...thanks

#include <fstream> // for ifstream
#include <iostream> // for cin, cout and cerr
#include <string> // for the string datatype
#include <cstdlib> // needed for the exit function

using namespace std; //needed to use the string datatype, notice that
// the include files do not have the .h
// extension and <iostream> is required even if
// <fstream> is used

//
// function prototypes
//
string getInputFileName(); // a function to prompt for the complete file name

int numWordsInFile( ifstream &in, int &numLines ); // a function to count the
// number of words and
// lines in a text file

void main ()
{
int nLines, // number of lines in the text file
nWords, // number of words in the text file
avgWordsPerLine; // average number of words per line

ifstream inFile; // handle for the input text file

string fileName; // complete file name including the path

fileName = getInputFileName(); // prompt and obtain the full file name

inFile.open(fileName.c_str()); // try to open the file

if( !inFile.is_open() ) // test for unsuccessfull file opening
{
cerr << "Cannot open file: " << fileName << endl << endl;
exit (0);
}

nWords = numWordsInFile( inFile, nLines ); // determine the number of lines
// and words in the file

avgWordsPerLine = nWords / nLines;

//
// print the number of words
//
cout << "The number of words in the file: " << fileName
<< " is = " << nWords << endl << endl;
//
// print the number of lines
//
cout << "The number of lines in the file: " << fileName
<< " is = " << nLines << endl << endl;


//
// print the average number of words per line
//
cout << "The average number of words per line in the text file: "
<< fileName << " is: " << avgWordsPerLine << endl << endl;

inFile.close(); // close the input file

}

//************************************************************
//
// Function name: getInputFileName
//
// Purpose: to prompt for the fully qualified name of a file
// i.e. including the path of the file
//
// Input parameters: none
//
// Output parameters: none
//
// Return Value: a string containing the fully qualified name
// of a file
//
//************************************************************

string getInputFileName()
{
string fName; // fully qualified name of the file

cout << "Please enter the fully qualified name of the " << endl
<< "input text file (i.e. including the path): ";
cin >> fName; // cannot handle blanks in a file name or path
cout << endl; // skip a line

return fName;
}


//******************************************************************
//
// Function name: numWordsInFile
//
// Purpose: counts the number of words and lines in a text file
// i.e. including the path of the file
//
// Input parameters: in - a file handle pointing (pass by reference)
// to the input file
//
// Output parameters: numLines - will contain the number of lines
// in the text file
//
// Return Value: the number of words in the text file
//
//*******************************************************************


int numWordsInFile( ifstream &in, int &numLines )
{
int numWords = 0; //number of words initialized to zero

string str; // word holder;

numLines = 0; // initialize the number of lines to zero

while ( in >> str ) // get the next word from the file
// the function get will also get whitespace
// i.e. blanks, tabs and end of line words
{
if ( str != '\n' ) // test for end of line word
numWords++; // increase the count of words by one
else
numLines++; // have an end of line word,
// increase the count by one
}

return numWords; // return the number of words in the file
}
The != operator isn't overloaded for the string class.

It does have a compare function you can make use of, though.
http://www.cplusplus.com/reference/string/string/
Last edited on
Actually, std::string does overload the != operator
(see http://www.cplusplus.com/reference/string/operators/ )
but I think the problem is OP is trying to compare to a single character. Try writing "\n" and see if that fixes the comparison issue.

However, you (@OP) should note that by using in >> str will guarantee str will never contain any whitespace or newline characters; that's just how >> works. If you are at a new line, in >> str will simply read to the next line.
My gosh you're right. And of course it does. :-P

Having a 3am moment. ;-)
Topic archived. No new replies allowed.