Hello xiaolim,
QUOTE(xiaolim @ 5 Aug, 2008 - 01:25 AM)

hi, i written a programe that will open a file and then read all the characters in it and display them out. so here is my code:
alright, here is the problem, for "upper", "lower", "digits" and "other" all displayed correctly,but the others were wrong, i wanted to count all the characters in the file, End-of-Sentence Punctuation Marks( those punct in the end of a sentence ) and the number of white space in between the sentence. the all characters count have a wrong output as well as the whitespace, End-of-Sentence Punctuation Marks have no output. can anyone try on some text file and see where my code went wrong? any help is greatly appreciated. thanks
Just a couple problems with the code, Total characters is everything put together, isalnum just does the same thing as isdigit & lower upper.
Here's my fix...
cpp
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <cstdlib>
using namespace std;
#ifdef _WIN32
const char CLRSCR[] = {"cls"};
const char PAUSE[] = {"pause"};
#else
const char CLRSCR[] = {"clear"};
const char PAUSE[] = {"sleep(20)"};
#endif
int main(void)
{
ifstream fin;
ofstream fout;
string filename;
cout << "Welcome\n" << endl;
cout << "This program will analyze the file content &" << endl;
cout << "compute the statistics of the file you input.\n\n\n\n\n" << endl;
system(PAUSE);
system(CLRSCR);
do{
cout << "Enter input data file name:\n";
cin >> filename;
cout << "\n"; // Get the file name.
fin.open(filename.c_str()); // Convert to C-string and open it.
if (!fin)
{ // Will fail if didn't exist.
cout << "Unable to open " << filename << endl;
cin.get();
system(CLRSCR);
}
}while(!fin);
char next;
int characters = 0;
int digits = 0;
int upper = 0;
int lower = 0;
int space = 0;
int eospm = 0;
int others = 0;
while((next = fin.get()) != EOF)
{
//calculate total numbers of characters including space
//---> it doesnt count all the characters, what should i use to count every characters including space?
//The function isalnum() returns non-zero if its argument
//is a numeric digit or a letter of the alphabet. Otherwise, zero is returned
if(isalnum(next))
characters++;
//calculate total numbers of digits ---> correct
if(isdigit(next))
++digits;
//calculate total numbers of uppercase ---> correct
if(isupper(next))
++upper;
//calculate total numbers of lowercase ---> correct
if(islower(next))
++lower;
//calculate total numbers of space ---> this 1 doesnt count the correct whitespace either.
if(isspace(next))
++space;
//calculate total numbers of punctuation ---> correct
if(ispunct(next)){
++others;
switch(next){
case '?':
case '.':
case '!':
++eospm;
break;
default:
break;
}
}
}
// Print out what is counted.
cout << "Total Number of Characters: " << space+digits+upper+lower+others << endl;
cout << "" << endl;
cout << "Total Number of Uppercase: " << upper << endl;
cout << "Total Number of Lowercase: " << lower << endl;
cout << "Total Number of Digits: " << digits << endl;
cout << "Total Number of Space: " << space << endl;
cout << "Total Number of End-of-Sentence Punctuation Marks: " << eospm << endl;
cout << "Total Number of Other Characters: " << others << endl;
cout << "" << endl;
system(PAUSE);
return 0;
}
Here's the file I used...
CODE
1 2 3 4 5 6 7 8 9 10
; ' " : ? ! . ,
a b c d e f g h i j k l m n o p q r s t u v w x y z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
sentence.
SENTENCE.
digits = 11 Because you're counting individual characters. If you want to count 10 as one digit then you'll need to track what the previous character was and if the previous character and the current character are digits then don't count it.
total = 160
upper = 34
lower = 34
spaces = 71
eospm = 5
others = 10
JW