Can you also look at my counting the character code? I'm not sure if I did it correctly.
Thanks Again!!!
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <map>
using namespace std;
string getInputFileName();
int numChars(ifstream &in, int &numLines);
int numWords(ifstream &in, int &numLines);
int numPunct(ifstream &in, int &numLines);
void main()
{
//lineBuffer = stream;
int nLines, //number of lines
nWords, //number of words
nChars, //number of character
nPunct; //number of punction marks
char c;
ifstream InFile;
InFile.open("C:/Users/Documents/est.txt", ios::in);
string LineBuffer;
string filename;
//ifstream ("c:/Users/Documents/text.txt", ios::in);
filename = getInputFileName();
InFile.open(filename.c_str());//open the file
if (!InFile.is_open())
{
cerr << "File Error: Failed Could Not open file" << endl; //cerr is a standard :: cout error message
exit(0);
}
nChars = numChars (InFile, nLines); //this will calculate the total number of lines
nWords = numWords (InFile, nWords); //this will calculate the total number of words
nPunct = numPunct (InFile, nPunct); //this will calculate the total number of punction marks
cout << "The number of lines in the document are: " << nLines << endl;
cout << "The number of words in the document are: " << nWords << endl;
cout << "The number of punction marks in the document are: " << nPunct << endl;
cin >> c;
InFile.close(); //close and flush the file
}//end main
string getInputFileName()
{
string fName;
cout << "Enter the name of the document you would like to retrieve(including the path): ";
cin >> fName;
return fName;
}
int numChars (ifstream &in, int &numLines)
{
int numChars = 0;
char ch; //character holder
numLines = 0;
while(in.get(ch))
{
if(ch != ' ')
{
if (ch != '\n')
numChars++;
else
numLines++; //increase the count of lines
}
}
numChars += 1;
return numChars;
}
int numWords(ifstream &in, int &inWords)
{
in.clear();
in.seekg(0,ios_base::beg);
int numWords = 0;
char ch;
while (in.get(ch)) //get the character
{
if(ch == ' ' || ch == '\n' || ch == '\t')
numWords++;
}
numWords +=1;
return numWords;
}
int numPunct(ifstream &in, int &inWords)
{
in.clear();
in.seekg(0,ios_base::beg);
char ch;
int numPunct = 0;
//getline (InFile, lineBuffer);
while (in.get(ch))
{
if(ch == '.' || ch == '?' || ch == '!' || ch == ';')
numPunct++;
}
numPunct += 1;
return numPunct;
}
** Edit **

New Topic/Question
Reply




MultiQuote





|