I wrote a program for counting the number of alphanumeric characters in a text file. However, the number it returns is always larger than the number that online character counters return.
For example, the program will calculate the number of alphanumeric characters in this text:
if these people had strange fads and expected obedience on the most extraordinary matters they were at least ready to pay for their eccentricity
to be 162. Running the program again, it'll say there are 164 characters in the text. Running it again, it'll say there are 156 characters. Using this online character counter, it seems that the character count ought to be lower than 144 (the online character counter includes spaces as well).
Here is the code:
#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;
int main() {
char line[100];
int charcount = 0;
ifstream file("pg1661sample.txt");
while (!file.eof()) {
file.getline(line, 99);
for (int i = 0; i < 100; i++) {
if (isalnum(line[i])) {
charcount++;
}
}
}
cout << endl << "Alphanumeric character count: " << charcount;
cin.get();
return 0;
}
What am I doing wrong?