C-Strings

#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 numCharsInFile( ifstream &in, int &numLines ); // a function to count the
// number of characters
// and lines in a text file

void main ()
{
int nLines, // number of lines in the text file
nChars, // number of characters in the text file
avgCharsPerLine; // average number of characters 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);
}

nChars = numCharsInFile( inFile, nLines ); // determine the number of lines
// and characters in the file

avgCharsPerLine = nChars / nLines;

//
// print the number of characters
//

cout << "The number of characters in the file: " << fileName
<< " is = " << nChars << 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 characters per line
//

cout << "The average number of characters per line in the text file: "
<< fileName << " is: " << avgCharsPerLine << 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: numCharsInFile
//
// Purpose: counts the number of characters 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 characters in the text file
//
//*******************************************************************


int numCharsInFile( ifstream &in, int &numLines )
{
int numChars = 0; //number of characters initialized to zero

char ch; // character holder;

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

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

return numChars; // return the number of characters in the file
}


How do I modify this program in order to count the numbers of words instead of the number of characters of the file inputted. As of now the program is counting the numbers of characters including the white spaces (blanks, tabs and end of line characters). I want to modify it just so it counts the number of words which would be the characters separated by white space. Please help.
The main loop would change from char ch; while ( in.get(ch) ), which reads character by character to string str; while(in >> str), which reads word by word (whitespace-separated)
Incidentally, why is the title of the thread "C-Strings"?
Last edited on
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
}

I modify the last part of the program to read above, but it keeps giving me an error that no operator "!=" matches these operands at if ( str != '\n' )
got it needs to be if (str != "\n")
Topic archived. No new replies allowed.