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?
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++;
}
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:
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: