Praca z plikiem tekstowym, zliczanie poszczególnych liter

0

Witam, napisałem programik mający wypisać wszystkie liczby w tekście oraz zliczyć ile poszczególnych liter alfabetu się w nim znajduje. Niby się uruchamia, wypisuje liczby i ilość liter jednak okazało się, że zlicza je nieprawidłowo. Np. w tekście jest 3 b a on wypisuje 1... Oto kod:

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

bool openFile(string fileName, ifstream & file)
{
    file.open(fileName.c_str());

    if(!file.good()) return false;
    else return true;
}

void writeText(ifstream & file, string fileName)
{
    string line;

    cout <<"Tekst z pliku "<< fileName <<":"<< endl;
    cout << endl;
    cout <<"-------------------------"<< endl;

    do
    {
     getline(file, line);
     cout << line << endl;
    }while(!file.eof());

    cout <<"-------------------------"<< endl;
    cout << endl;
}


void writeNumbers(ifstream & file)
{
    int temp;
    int i=1;

    file.clear();
    file.seekg(0, ios::beg);
    cout <<"Wszystkie liczby w tym tekscie: "<< endl;
    cout << endl;

    do
    {
        if(file >> temp)
        {
            cout << i <<" liczba to: "<< temp << endl;
            ++i;
        }else
        {
            file.clear();
            if(file.get()==EOF)    return;
        }
    }while(true);
}

void countLetters(ifstream & file, char alphabet[])
{
    int i=0;
    int temp=0;
    int sum=0;

    file.clear();
    file.seekg(0, ios::beg);
    cout << endl;
    cout <<"Ilosc liter alfabetu: "<< endl;
    cout << endl;

    do
    {
        if(file.get()==alphabet[i])   ++temp;
        else if(file.get()==EOF)
        {
          cout << alphabet[i] <<": "<< temp << endl;
          sum+=temp;

          if(file.get()==EOF && i==25)    break;

          file.clear();
          file.seekg(0, ios::beg);
          temp=0;
          ++i;
        }
    }while(true);

    cout << endl;
    cout <<"Lacznie liter: "<< sum << endl;
}

int main()
{
    ifstream file;
    char alphabet[26]={'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'};

    if(openFile("test.txt", file)==false)
    {
        cout <<"Nie udalo sie otworzyc pliku..."<< endl;
        return 0;
    }

    writeText(file, "test.txt");
    writeNumbers(file);
    countLetters(file, alphabet);
    file.close();
    return 0;
}

Macie pomysł co może być tego przyczyną?
Tutaj jeszcze screen z odpalonego programu:
user image

0

To jest straszne! Wystarczy tyle:

#include <cctype>
#include <iostream>
#include <fstream>
using namespace std;
 
int main()
  {
   ifstream fin("test.txt");
   unsigned counts[26]={0},numcount=0;
   while(true)
     {
      int value;
      if(fin>>value) cout<<(++numcount)<<" liczba to: "<<value<<endl;
      else
        {
         fin.clear();
         int ch=fin.get();
         if(!fin) break;
         if(islower(ch=tolower(ch))) ++counts[ch-'a'];
        }
     }
   for(unsigned i=0;i<26;++i) if(counts[i]) cout<<(char)('a'+i)<<": "<<counts[i]<<endl;
   return 0;
  }

1 użytkowników online, w tym zalogowanych: 0, gości: 1