D
dan
this is a program to count average letters per word. i am able to
count the total number of letters, but not words. How do you count the
total number of words in a text file, so i am able to divide the total
letters divided by words.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>
using namespace std;
int main ()
{
int wordcount = 0;
int letters = 0;
double average = 0;
char ch;
ifstream infile;
infile.open("a:wordy.txt");
if (infile.fail( )) {
cout << "Input file opening failed.\n";
system("pause");
exit(1);
}
while(infile >> ch) {
if (isalnum(ch))
letters++;
}
//average = (letters/wordcount);
cout << "There are " << letters << " letters\n";
cout << "There are " << wordcount << " words\n";
//cout << "There average is " << average << " letters per word\n";
infile.close( );
cout << "End of program.\n";
system("pause");
return EXIT_SUCCESS;
}
thanks, Dan
count the total number of letters, but not words. How do you count the
total number of words in a text file, so i am able to divide the total
letters divided by words.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>
using namespace std;
int main ()
{
int wordcount = 0;
int letters = 0;
double average = 0;
char ch;
ifstream infile;
infile.open("a:wordy.txt");
if (infile.fail( )) {
cout << "Input file opening failed.\n";
system("pause");
exit(1);
}
while(infile >> ch) {
if (isalnum(ch))
letters++;
}
//average = (letters/wordcount);
cout << "There are " << letters << " letters\n";
cout << "There are " << wordcount << " words\n";
//cout << "There average is " << average << " letters per word\n";
infile.close( );
cout << "End of program.\n";
system("pause");
return EXIT_SUCCESS;
}
thanks, Dan