Results 1 to 2 of 2
hello!
I have a program for my c++ program in which I have to open a text file and count the number of lines, characters, sentences and words. so far ...
- 02-13-2009 #1Just Joined!
- Join Date
- Feb 2009
- Posts
- 1
counting words with c++
hello!
I have a program for my c++ program in which I have to open a text file and count the number of lines, characters, sentences and words. so far I have been able to do must of the work but I dont know how to make my counter count the words. I know it has something to do with spaces, or counting spaces, but how do I do that? please consider that I'm doing basic c++ programming and I dont know any advance stuff neither is available to do any advance things in this project.
thank you in advance
Sabrina
so this is my code:
#include <iostream>
#include <iomanip> // standard IO routines
#include <cctype>
#include <string>
#include <fstream>
using namespace std;
/* ================================================== ======================= */
int main ()
{
// variable declaration
char ch;
const char BLANK = ' ';
int num_character = 0;
int num_line = 0;
int num_sentence = 0;
int num_word = 0;
int last_ch = '_';
ifstream inFile;
string filename; // the name of the file
/* Output heading */
cout << "**=============================================== =========**" << endl
<< endl;
cout << " Welcome to Star Text Analyzer!!! " << endl
<< endl << endl;
// little explanation about what the program does
cout << "The contents of the specified input file will be printed" << endl;
cout << "exactly as they occur in the file." << endl;
cout << "Two rows of asterisks will be used to delineate the file" << endl;
cout << "contents. Finally the number of characters, words, lines" << endl;
cout << "and sentences will be printed." << endl << endl;
cout << "**=============================================== ============**" << endl
<< endl << endl;
// asking for the name of the file
cout << "Please enter the name of the file " << endl;
cin >> filename; //priming read
inFile.open(filename.c_str());
inFile.get (ch);
// if the name of the file is incorrect it will ask again
while(!inFile)
{
cout << "The file name you gave is invalid. please try again." << endl;
cout << endl << "Please enter again the name of the file " << endl;
cin >> filename;
inFile.clear();
inFile.open(filename.c_str());
inFile.get (ch);
}
while (inFile){
//do something
cout << ch;
num_character++
if (ch == '\n'){
num_line++;
}
if (ch =='.') {
num_sentence++;
}
if (ch = " ")
num_word++
//read again
inFile.get (ch);
}
cout << endl;
cout << " ---------- Summary Count of the elements: ----------- " << endl << endl;
cout << setw(14) << num_character << " Character(s) " << endl;
cout << setw(14) << num_sentence << " sentence(s) " << endl;
cout << setw(14) << num_line << " line(s) " << endl;
cout << setw(14) << num_word << " word(s) " << endl;
inFile.clear();
inFile.close ();
cout << endl << "Execution Terminated" << endl << endl;
return (0);
}
- 02-14-2009 #2
I would just check the text file for spaces ' ' tabs '\t' and new lines '\n'...this should give you the start and end of each word...Hope this helps Gerard4143


Reply With Quote