count number of sentence in text file

i'm beginner in C++.
I try to count the number of sentence in my application program with the function countsentence but it cannot open the file.
The function countword and numberchar work very good.
I want your help, please.

#include <iostream>
#include <fstream>
#include <string>

//#include <iomanip>
using namespace std;
fstream textfile; //File stream object;
string filename; //To hold the file name
char ch; //To hold a character
double nbchar = 0; //To hold the number of character
double nbword = 0; //To hold the number of word
double nbsentence = 0; //To hold the number of sentence
double avgnbword = 0; //To hold the average number of word in each sentence

//function prototypes
double numberchar(fstream &filechar, string textchar); //calculate the number of character
double countword(fstream &fileword, string textword); //calculate the number of word.
void countsentence(fstream &filesent, string textsentence); //calculate the number of word.


int main()
{
cout <<"Enter the file name: ";
//cin.get(filename, 256);
cin >> filename;
cout << numberchar(textfile, filename) << "\n";

cout << countword(textfile, filename) << "\n";

//cout << countsentence(textfile, filename) << "\n";
countsentence(textfile, filename);
return 0;
}

//Define of function numchar
//calculate the number of character in a file.
double numberchar(fstream &file, string textch)
{
//open the file
cout << "Opening file...\n";
//textfile.open(textch.c_str() , ios::in);
file.open(textch.c_str() , ios::in);
// if the file was not successfully opened, error message.
if (file.fail())
{
cout << textch << " could not opened. \n";
return 1;
}
else
// if the file was successfully opened, continue.
while (!file.eof())
{
file.get(ch);
nbchar++;
}

//close the file
file.close();
cout <<"The number of char is: ";
return nbchar;
}


//Define of function countword
//calculate the number of character in a file.
double countword(fstream &file, string textword)
{
//open the file
cout << "Opening file...\n";
file.open(textword.c_str(), ios::in);
// if the file was not successfully opened, error message.
if (file.fail())
{
cout << textword << " could not opened. \n";
return 1;
}
else
// if the file was successfully opened, continue.
while (!file.eof())
{
file.get(ch);
if (ch==' ')
{
nbword++;
}

}
cout <<"The number of word is: ";
return nbword;
//close the file
file.close();
}

//Define of function countsentence
//calculate the number of character in a file.
void countsentence(fstream &filesente, string textsentence)
{
//open the file
cout << "Opening file...\n";
filesente.open(textsentence.c_str(), ios::in);
// if the file was not successfully opened, error message.
if (filesente.fail())
{
cout << textsentence << " could not opened. \n";
cout << 1;
}
else
{
// if the file was successfully opened, continue.
while (!filesente.eof())
{
filesente.get(ch);
if (ch == '.') // condition for number of sentence
{
nbsentence++;
}
}

avgnbword = nbword/nbsentence;

cout <<"The number of sentence is: " <<nbsentence <<"\n";
//return nbsentence ;

cout <<"\nThe average number of words by sentence is: " << avgnbword << "\n";
//return avgnbword;
}
//close the file
filesente.close();
}
Topic archived. No new replies allowed.