Hello! I am currently working on a problem where I read a file into a 2D array, and display data. If you saw my code earlier, this is the same problem, but I am now further along.
The file (90 characters) looks like this:
S
R
R
S
R
R
R
R
S
S
S
S
C
S
C
C
R
C
R
R
S
C
R
C
C
S
C
S
S
R
S
C
C
C
S
C
R
R
R
S
R
C
S
R
S
C
C
R
C
C
C
C
C
R
R
R
C
R
R
C
C
R
S
S
C
R
R
C
R
R
S
C
R
R
S
S
R
R
R
R
R
C
S
C
C
R
S
R
R
R
The "R" resembles rainy days, the "S" resembles sunny days, and the "C" resembles cloudy days. There are 90 characters in the file. The first 30 are for the month of June, the next 30 are for the month of July, and the last 30 are for the month of August. The 31st day of the months are NOT included.
I read the data into a 3 x 30 array, where the row indicates the month (0 = June, 1 = July, 2 = August) and the column indicates the number of days in each month.
Now I need to create a function that creates a report and displays, for each month AND for the whole 3 month period:
1) The number of rainy days
2) The number of cloudy days
3) The number of sunny days
4) Which of the 3 months had the largest number of rainy days
Here is my code so far:
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 64
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int MONTHS = 3; //Number of months
const int DAYS = 30; //Number of days in each month
const int SIZE = 90; //Total number of days
void readFunction(char[], string[][DAYS]); //readFunction prototype
void reportFunction(string[][DAYS]); //reportFunction prototype
int main()
{
char weatherArray[SIZE]; //Array for the weatherFile data
string summerArray[MONTHS][DAYS]; //Array for the 3 months and 30 days in each month
readFunction(weatherArray, summerArray);
reportFunction(summerArray);
return 0;
}
void readFunction(char weatherArray[], string summerArray[][DAYS]) //Reads weatherFile into summerArray
{
int row = 0, //Loop counter for rows
col = 0; //Loop counter for columns
ifstream weatherFile; //Weather file
for (row = 0; row < MONTHS; row++)
{
for (col = 0; col < DAYS; col++)
{
weatherFile.open("RainOrShine.dat");
weatherFile >> summerArray[row][col];
}
}
weatherFile.close();
}
void reportFunction(string summerArray[][DAYS]) //Creates report for data
{
int row, //Loop counter
col, //Loop counter
rCount = 0, //Keeps track of rainy days
cCount = 0, //Keeps track of cloudy days
sCount = 0; //Keeps track of sunny days
for (row = 0; row < MONTHS; row++)
{
for (col = 0; col < DAYS; col++)
{
if (summerArray[row][col] == "R")
rCount++;
else if (summerArray[row][col] == "C")
cCount++;
else
sCount++;
}
}
cout << "There were " << rCount << " rainy days.\n";
cout << "There were " << cCount << " cloudy days.\n";
cout << "There were " << sCount << " sunny days.\n";
}
|
There are no compiling errors, but I am not getting the data I want. My rCount and cCount variables end up being 0, and my sCount variable is 90.
Can someone please tell me what I'm doing wrong?