How to read the amount of a certain character in each line from a .txt file in c++?

I'm reading from a .txt file called RainOrShine and the contents of the file look like this:
R C R A R S R R S R S C R C S S S S R S C A R C S S A C R S
C R R A S A S R C S R C R S S S R S C R R S C C S C S C R R
C R C C S C C R R R S A A C R C C C R C R S S S R S C A R A

I successfully loaded the file and put it into the contents of my array, but now I have to count each character in the line and print out how many of them are in line 1, 2, and 3.

I tried using while(getline ( inputFile, line))
but I wasn't really sure how to set that up correctly. Does anyone know how to correctly read the amount of a certain character in a line from a .txt file?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  void initArray(char theArray[][COL], const int R) //R is row size constant integer
{
    ifstream inputFile;
    inputFile.open("RainOrShine.txt");

    for(int i = 0; i < R; i++) //initialize the array.
    {
        for(int j = 0; j < COL; j++)
        {
            inputFile >> theArray[i][j]; //inputfile >> theArray[i][j]; for loading the array from input file.
        }
    }
    inputFile.close();
}


void printArray(char theArray [][COL], const int R)
{
    for(int i = 0; i < R; i++) //print array in columns and rows with these two for loops.
    {
        for(int j = 0; j < COL; j++)
        {
            cout << theArray[i][j] << ' ';
        }
        cout<<endl;
    }
}
To me it looks like the easy way is after line 10, Right after you read each char from the file.

1
2
3
if (theArray[i][j]=='A') {ACount++;)
else if (theArray[i][j]=='C') {CCount++;)
else if (until done...)
Thanks for the solution! I'll try implementing this into my code and see if it works
So it seems to be counting, but not counting correctly and i'm having an issue understanding why..It seems like a variable isn't being passed correctly or it simply stops counting after 1,

for my output I keep getting 1, instead of 10 for the 1st row if it was counting R

void initArray(char theArray[][COL], const int R) //R is row size constant integer
{
ifstream inputFile;
inputFile.open("RainOrShine.txt");

for(int i = 0; i < R; i++) //initialize the array.
{
for(int j = 0; j < COL; j++)
{
inputFile >> theArray[i][j];
countChars(theArray, R, i, j);
}
}
inputFile.close();
}

void countChars(char theArray[][COL], const int R, int i, int j)
{
int ACount = 0;
int CCount = 0;
int SCount = 0;
int RCount = 0;

if (theArray[i][j]=='A')
{
ACount++;
}
else if (theArray[i][j]=='C')
{
CCount++;
}
else if (theArray[i][j]=='S')
{
SCount++;
}
else (theArray[i][j]=='R');
{
RCount++;
}

cout<<"\t Rainy"<<"\t Cloudy"<<"\t Sunny"<<"\t Armegeddon"<<endl;
cout<<"June"<<setw(8)<<RCount<<endl;
cout<<"July"<<setw(8)<<RCount<<endl;
cout<<"August"<<setw(6)<<RCount<<endl;
cout<<"Total"<<setw(7)<<"10"<<endl;

}
Use the count algorithm to keep a running count of each occurrence of A, C, etc as the file is read line by line. Save the results of each line in a vector<tuple<..>> and finally read off the results:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<tuple>
#include<algorithm>

using namespace std;

int main()
{
    ifstream inputFile("F:\\test.txt");
    string line;
    vector<tuple<int, int, int, int>> rain;

    if(inputFile)
    {
        while(getline(inputFile, line))
        {
            int numA = count(line.begin(), line.end(), 'A');
            int numC = count(line.begin(), line.end(), 'C');
            int numR = count(line.begin(), line.end(), 'R');
            int numS = count(line.begin(), line.end(), 'S');
            rain.emplace_back(numA, numC, numR, numS);
        }
    }

    for(auto& itr : rain)
    {//using range-loop (C++11);

        cout<<"A: "<<get<0>(itr)<<" C:"<<get<1>(itr)<<" R:"<<get<2>(itr)<<" S:"<<get<3>(itr)<<"\n";
                //prints the vector element by element;
    }
}


Output
1
2
3
A: 3 C:6 R:10 S:11
A: 2 C:8 R:10 S:10
A: 4 C:11 R:9 S:6
An alternative, and probably more flexible, way to do this is to declare a struct Rain with data members A, C, R and S. You can then overload the addition and insertion operators for this struct and read off the data from the file directly into struct objects and place these objects into a container (here vector) of your choice:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<algorithm>

using namespace std;

struct Rain
{
    int m_A;
    int m_C;
    int m_R;
    int m_S;

    Rain()
    {
        m_A = 0; m_C = 0; m_R = 0; m_S = 0;
    }
    Rain operator+(const Rain& r)const
    {
        Rain tmp;
        tmp.m_A = m_A + r.m_A;
        tmp.m_C = m_C + r.m_C;
        tmp.m_R = m_R + r.m_R;
        tmp.m_S = m_S + r.m_S;

        return tmp;
    }
};
    ostream& operator<<(ostream& os, const Rain& r)
    {
        os<<"A:"<<r.m_A<<" C:"<<r.m_C<<" R:"<<r.m_R<<" S:"<<r.m_S<<"\n";

        return os;
    }
int main()
{
    ifstream inputFile("F:\\test.txt");
    string line;
    vector<Rain> records;

    if(inputFile)
    {
        while(getline(inputFile, line))
        {
            Rain r;
            r.m_A = count(line.begin(), line.end(), 'A');
            r.m_C = count(line.begin(), line.end(), 'C');
            r.m_R = count(line.begin(), line.end(), 'R');
            r.m_S = count(line.begin(), line.end(), 'S');
            records.emplace_back(r);
        }
    }
    Rain sum;
    for(auto& itr : records) //using range-loop (C++11);
    {
        sum = itr + sum;

    }

    cout<<sum;
}
Topic archived. No new replies allowed.