C++ Code Weather

I need help with function countDays(last part) portion it runs but not correctly

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;



const int NUM_OF_MONTHS = 3;
DAYS_IN_MONTH = 30;

const string name[NUM_OF_MONTHS] = { "June,", "July","August" };

void readFileData(char[][DAYS_IN_MONTH]);
int countDays(char[][DAYS_IN_MONTH], int, char);


int main()
{
// this 2D array stores the 90 values from the RainOrShine.txt text file
char dayType[NUM__MONTHS][NUM_IN_MONTH];

int rainy,
cloudy,
sunny,
rainyTotal = 0;
sunnyTotal = 0 ;
wettestMonth,
wettestMonthsRain = -1 ;

// Read Data and print report
readFileData(day Type);

cout << " SUmmer Weather Report\n\n"
<< "Month Rainy Cloudy Sunny\n"
<< "______________________________";

}
for(int month = 0; month < NUM_MONTHS;month++)
{
for (int day = 0; day < DAYS_IN_MONTH; day++)
{
rainy = countDays(dayType, month, 'R');
cloudy = countDays(dayType, month, 'C');
sunny = countDays(dayType, month, 'S');

}
rainyTotal += rainy;
cloudyTotal += cloudy;
sunnyTotal += sunny;

if (rainy > wettestMonthsRain)

{
wettestMonthsRain = rainy;
wettestMonth = month;
}
cout << left << setw(6) << name[month]
<< right << setw(6) << rainy << setw(8) << cloudy
<< setw(7) << sunny << endl;
}

cout << "________________________________________\n";
cout << "Totals" << setw(6) << rainyTotal << setw(8)
<< cloudyTotal << setw(7) << sunnyTotal << endl << endl;
cout << " The month with the most rainy days was "
<< name[wettestMonth] << ". \n";

return 0;
{

void readFileData(char dayType[][DAYS_IN_MONTH])
{
ifstream weatherData;

weatherData.open("RainOrShine.dat");
if (!weatherData)
{
cout << "Error opening data file.\n";
exit(EXIT_FAILURE);
}


for (int month = 0; month < NUM - MONTHS; month++)
{
for (int day = 0; day < DAYS_IN_MONTH; day++)
weatherData >> datType[month][day];
}

weatherData.close();

}
int countDays(char array[][DAYS_IN_MONTH], int mo, char symbol)
{
int count = 0;

for (int day = 0; day < DAYS_IN_MONTH; day++)
{
if (== symbol)
count++;
}
return count;
}

needs to read

Summer weather Report
Month Rainy Cloudy Sunny
______________________
June 9 9 12
July 2 10 18
August 2 8 20
_____________________
Totals 13 27 50

The month with the monst rainy days was June.
Not bad overall. The biggest problem I'm seeing with your code is that you need to be more mindful while typing, multiple times your variables are half-spelled, mistyped or skipped altogether. Once those were fixed the program runs quite well.
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

const int NUM_OF_MONTHS = 3;
const int DAYS_IN_MONTH = 30;
const string name[NUM_OF_MONTHS] = { "June", "July","August" };
void readFileData(char[][DAYS_IN_MONTH]);
int countDays(char[][DAYS_IN_MONTH], int, char);

int main()
{
	// this 2D array stores the 90 values from the RainOrShine.txt text file
	char dayType[NUM_OF_MONTHS][DAYS_IN_MONTH];

	int rainy = 0,
	cloudy = 0,
	sunny = 0,
	rainyTotal = 0, 
	cloudyTotal = 0;
	int sunnyTotal = 0 ;
	int wettestMonth = 0,
	wettestMonthsRain = -1 ;

	// Read Data and print report
	readFileData(dayType);

	cout << "Summer Weather Report\n\n"
	<< "Month Rainy Cloudy Sunny\n"
	<< "______________________________\n";

	for(int month = 0; month < NUM_OF_MONTHS; month++)
	{
		for (int day = 0; day < DAYS_IN_MONTH; day++)
		{
			rainy = countDays(dayType, month, 'R');
			cloudy = countDays(dayType, month, 'C');
			sunny = countDays(dayType, month, 'S');
		}
		rainyTotal += rainy;
		cloudyTotal += cloudy;
		sunnyTotal += sunny;
	
		if (rainy > wettestMonthsRain)
	
		{
			wettestMonthsRain = rainy;
			wettestMonth = month;
		}
		cout << left << setw(6) << name[month]
		<< right << setw(6) << rainy << setw(8) << cloudy
		<< setw(7) << sunny << endl;
	}

	cout << "________________________________________\n";
	cout << "Totals" << setw(6) << rainyTotal << setw(8)
	<< cloudyTotal << setw(7) << sunnyTotal << endl << endl;
	cout << " The month with the most rainy days was "
	<< name[wettestMonth] << ". \n";

	return 0;
}

void readFileData(char dayType[][DAYS_IN_MONTH])
{
	ifstream weatherData;

	weatherData.open("RainOrShine.dat");
	if (!weatherData)
	{
		cout << "Error opening data file.\n";
		exit(EXIT_FAILURE);
	}


	for (int month = 0; month < NUM_OF_MONTHS; month++)
	{
		for (int day = 0; day < DAYS_IN_MONTH; day++)
		weatherData >> dayType[month][day];
	}

	weatherData.close();

}
int countDays(char array[][DAYS_IN_MONTH], int mo, char symbol)
{
	int count = 0;

	for (int day = 0; day < DAYS_IN_MONTH; day++)
	{
		if (array[mo][day] == symbol)
		count++;
	}
	return count;
}

Last edited on
The L37 for loop is not needed - as this is part of countDays(). Consider:

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
65
66
67
68
69
70
71
72
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

const int NUM_OF_MONTHS { 3 };
const int DAYS_IN_MONTH { 30 };
const std::string name[NUM_OF_MONTHS] { "June", "July","August" };

bool readFileData(char[][DAYS_IN_MONTH]);
int countDays(const char[][DAYS_IN_MONTH], int, char);

int main() {
	char dayType[NUM_OF_MONTHS][DAYS_IN_MONTH];

	int rainyTotal {}, cloudyTotal {}, sunnyTotal {};
	int wettestMonth {}, wettestMonthsRain { -1 };

	if (!readFileData(dayType))
		return (std::cout << "Error opening data file.\n"), 1;

	std::cout << "Summer Weather Report\n\n"
		<< "Month Rainy Cloudy Sunny\n"
		<< "______________________________\n";

	for (int month {}; month < NUM_OF_MONTHS; ++month) {
		const int rainy { countDays(dayType, month, 'R') };
		const int cloudy { countDays(dayType, month, 'C') };
		const int sunny { countDays(dayType, month, 'S') };

		rainyTotal += rainy;
		cloudyTotal += cloudy;
		sunnyTotal += sunny;

		if (rainy > wettestMonthsRain) {
			wettestMonthsRain = rainy;
			wettestMonth = month;
		}

		std::cout << std::left << std::setw(6) << name[month]
			<< std::right << std::setw(6) << rainy << std::setw(8) << cloudy
			<< std::setw(7) << sunny << '\n';
	}

	std::cout << "________________________________________\n";
	std::cout << "Totals" << std::setw(6) << rainyTotal << std::setw(8)
		<< cloudyTotal << std::setw(7) << sunnyTotal << "\n\n";
	std::cout << " The month with the most rainy days was "
		<< name[wettestMonth] << ". \n";
}

bool readFileData(char dayType[][DAYS_IN_MONTH]) {
	if (std::ifstream weatherData { "RainOrShine.dat" }) {
		for (int month {}; month < NUM_OF_MONTHS; ++month)
			for (int day {}; day < DAYS_IN_MONTH; ++day)
				weatherData >> dayType[month][day];

		return true;
	}

	return false;
}

int countDays(const char array[][DAYS_IN_MONTH], int mo, char symbol) {
	int count {};

	for (int day {}; day < DAYS_IN_MONTH; ++day)
		if (array[mo][day] == symbol)
			++count;

	return count;
}


Last edited on
@bsauceda03, gonna remind ya....

Please learn to use code tags, they make reading and commenting on source code MUCH easier.

How to use code tags: http://www.cplusplus.com/articles/jEywvCM9/

There are other tags available.

How to use tags: http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either
thank you


i have tried those link but i believe they are not working
The links DO work, they point to articles here at CPlusPlus. Try again!
Confirm that the given links are OK.
Topic archived. No new replies allowed.