-1

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;
}
Alex
  • 11
  • 2
  • Aside from your problem, surely you can come up with better names for the variables than `a`, `x`, `y`, `z`. – Michael Burr Feb 28 '16 at 03:11
  • Please use `std::string` instead of character arrays. You will have less issues. For example, arrays lose their attributes when passed to functions. So, you will have to pass the capacity and size of an array in addition to the array to functions. – Thomas Matthews Feb 28 '16 at 03:33
  • See also `std::isupper`, `std::islower`, `std::isalpha`, and `std::isdigit`. If you look through the libraries you can save yourself a lot of coding and debugging time. – Thomas Matthews Feb 28 '16 at 03:35
  • You have a memory leak. Where do you delete the memory you allocated for the character arrays? BTW, you can pass strings to functions (you don't need to convert to `char` array). Also, you can access a string like an array, such as `s[5]` or `s[i]`. – Thomas Matthews Feb 28 '16 at 03:37

1 Answers1

1

You must initialize variables.

int uppercount=0, lowercount=0, digitcount=0;

You would have probably gotten a compiler warning, except you passed them by address and the compiler didn't know what you were going to do with them.

Rob L
  • 2,351
  • 13
  • 23
  • And in addition to this, you'll probably get a bad grade from the instructor, for the way you're testing if a character is uppercase, lowercase, or digit. Don't you think it's more efficient that, instead of individually testing whether each individual character is 'A', then 'B', then 'C', and so on until 'Z', it's much simpler to simply test if the character is between 'A' and 'Z', inclusively? – Sam Varshavchik Feb 28 '16 at 03:00