Counting Character

Hello, could somebody help me with counting uppercase, lowercase, and digits in a text file? Here's my program

// Character Analysis
#include <iostream>
#include <fstream>
#include <cctype>
#include <string>
using namespace std;

int main()
{
char ch;
int charCount = 0;
int upperCount = 0;
int lowerCount = 0;
int digitCount = 0;

string text;
ifstream text;
text.open("text.txt");

cout << "Here is the analysis of characters in the text file" << endl;

text.get(ch);

while (text)
{
charCount++;
cout << ch;
if (isupper(text))
cout << upperCount++;
if (islower(text))
cout << lowerCount++;
if (isdigit(text))
cout << digitCount++;
}

}
First of all, please post your code within the tags: You'll find them under Format

You have some problems with your variable names: Your string and ifstream have the same name -- that's not good!

But otherwise, you have a good start -- you just need to keep reading characters from the file until you reach EOF (end-of-file).

1
2
3
4
5
6
7
8
9
10
11
12

ifstream TheStream;

TheStream.open( "text.txt" );

while( TheStream.get( ch ) )

// the rest of your program ...



Thank you!
Topic archived. No new replies allowed.