I have been trying to write a program which counts contents of a file in c++, I have used Ascii table and if statements to define characters, sentences but I not sure how I define words. there is also another problem with the code, it counts the last characters of the file twice and I have been trying to fix this issues by setting the "count = -1" but didn't turn out to be a good idea. any help will really appreciate it.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream infile;
infile.open("cpp.txt");
int count = 0; // counting characters
int count_t = 0; // text objects
int count_s = 0; // sentence count
int count_i_s = 0; // e-sentence
int count_in_s = 0; // question count
char C;
//char word[30];
while (!infile.eof())
{
infile.read(&C, 1);
{
// counting characters
if (C >= ' '&& C <= '~'|| C == '\t' || C == '\n')
count++;
}
//{
// infile << word;
// count_w++;
//}
{
//counting sentances
if (C == '.'|| C == '!'|| C == '?')
count_s++;
}
{
//counting exclamatory sentance
if (C == '!')
count_i_s++;
}
{
//counting question sentances
if (C == '?')
count_in_s++;
}
{
//counting text objector
if (C == ' '|| C == '\t'|| C == '\n')
count_t++;
}
}
cout << "the number of charaters :" << count << endl;
cout << "the number of sentances :" << count_s << endl;
cout << "the number of exclamtory sentances :" << count_i_s << endl;
cout << "the number of interrogative sentances :" << count_in_s << endl;
cout << "the number of text objects :" << count_t << endl;
return 0;
}