Hello, part of my project I'm working on needs to have me count the amount of letters in a file. This is what I have so far. Any one help?

#include <iostream>
#include <fstream>
using namespace std;

int main ()
{
char c ; // start with a blank
int i;
int count[111] = {0};
int sum=0;
do
{start:
          c = cin.get() ;
           
         c = tolower(c);
		 {    

         {for(i = 97; i < 100; i++)
          { if (c==(char)i)
            {count[c]=sum++;
		 goto start;}
          }
		 }
         }
		 for(int x= 97; x < 100; x++)
		 {
			 cout<<(char)x<<" = "<<count[x]<<endl;
		 }
        
     }
    
     while (c != EOF) ;

return 0;
}

Dani AI

Generated

This thread shows the typical pitfalls when counting individual letters in a stream. made good progress and later posted a working variant, while and offered alternate ideas. The main issues to avoid: testing a char against EOF, printing counts inside the read loop (which yields repeated output), and calling std::tolower/std::isalpha on a plain char without the unsigned-char cast. The last point can produce undefined behavior for negative char values; see std::tolower and std::isalpha for details.

A few practical rules:

  • Read until the stream reports failure (use while (in.get(ch))) instead of comparing char to EOF.
  • Convert to unsigned char before calling std::tolower/std::isalpha.
  • Only update counts for alphabetic characters, then index by tolower(ch) - 'a'.
  • Print results after the whole file has been processed.

A concise, robust example (reads stdin or a file given as argv[1]):

#include <iostream>
#include <fstream>
#include <array>
#include <cctype>

int main(int argc, char* argv[]) {
    std::istream* in = &std::cin;
    std::ifstream file;
    if (argc > 1) { file.open(argv[1], std::ios::binary); if (!file) return 1; in = &file; }

    std::array<unsigned long long,26> counts = {};
    char ch;
    while (in->get(ch)) {
        unsigned char uc = static_cast<unsigned char>(ch);
        if (std::isalpha(uc)) {
            uc = static_cast<unsigned char>(std::tolower(uc));
            if (uc >= 'a' && uc <= 'z') counts[uc - 'a']++;
        }
    }

    for (int i = 0; i < 26; ++i) std::cout << char('a' + i) << ' ' << counts[i] << '\n';
}

Notes: this is ASCII/locale-aware only. For UTF-8 or other multibyte encodings, decode into code points first (use a Unicode-aware library) or you'll miscount non-ASCII letters. Complexity is O(n) time and O(1) extra space.

Recommended Answers

All 5 Replies

it can be done much easier:

it can be done much easier:

That is a good way to count a total of letters. Sorry I wasn't more specific. I need individual totals for the letters. I am only using a b and c for test purposes. Maybe I can extract elements of that program if I can't figure out how to make mine work.

An even easier way :

An even easier way :

yeah, but we dont want to count like \r\n ., etc..

Thank you for helping. Sorry I am so stubborn. My teacher may ask me to explain my code and I prefer to use code that I can grasp. I think I have got it to work. Take a look and see if there are any holes I don't see.

#include <iostream>
#include <fstream>
using namespace std;

int main ()
{
char c ; 
int i;
int sum[26] = {0};
do
{start:
          c = cin.get() ;
           
         c = tolower(c);
		 {    

         {for(i = 97; i < 123; i++)
          { if (c==(char)i)
            {sum[c-97]++;
		 goto start;}
          }
		 }
         }
		 for(int x= 97; x < 123; x++)
		 {
			 cout<<(char)x<<" "<<sum[x-97]<<endl;
		 }
        
     }
    
     while (c != EOF) ;

return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.