I'm learning programming and one of my assignments is to create a program in order to count the number of uppercase, lowercase, and digits in any sort of input text file. For some reason, in my program, the number of lowercase letters that it has confirmed being counted is a ridiculously large number. I don't know how to fix this. The count for uppercase and digits seem to work fine. Also I don't have much knowledge on C++ so please don't put anything too advanced, sorry in advance.
#include <iostream> #include <fstream> #include <cstring> using namespace std; void countfile(char *a, int &x, int &y, int &z) { int len = strlen(a); for (int i =0; i < len; i++) { for (char b = 'A'; b <= 'Z'; b++) { if (a[i] == b) x++; } } for (int i = 0; i < len; i++) { for (char b = 'a'; b <= 'z'; b++) { if (a[i] == b) y++; } } for (int i =0; i < len; i++) { for (char b = '0'; b <= '9'; b++) { if (a[i] == b) z++; } } } int main() { string fileName; ifstream fin; cout << "Enter a text file: "; getline(cin, fileName); fin.open(fileName.c_str()); if (!fin.good()) throw "I/O error"; int uppercount, lowercount, digitcount; while (true) { string s; getline(fin, s); char *a = new char[s.size()+1]; a = (char*)s.c_str(); if (!fin.good()) break; countfile(a, uppercount, lowercount, digitcount); } cout << "The file contains: " << endl; cout << uppercount << " uppercase letters" << endl; cout << lowercount << " lowercase letters" << endl; cout << digitcount << " digits" << endl; return 0; }