PPP2 Chapter 10 Reading example code operator<<

Pages: 123
What are the conditions for a valid day ? A little comment at the top of the function might help.
At the moment you return valid after you read the first valid temperature.
Why don't you write a little test program where you manually create a few day objects and test this function separately?

A day is valid if it has even one temperature reading that isn't a not_a_reading (default sentry value). And right now it works. If I put
1
2
3
4
if (temp == not_a_reading)
{
	return false;
}


inside the loop and then return true outside the loop, none of the days are marked valid and the months come out empty in the output. The way I have it now, which is like this:
1
2
3
4
5
6
7
8
9
10
11
bool is_valid_day(const Day &d)
{
	for (const auto &temp : d.hour)
	{
		if (temp != not_a_reading)
		{
			return true;
		}
	}
	return false;
}


correctly marks the valid days as valid and the months are filled correctly as needed. I guess if there's a problem, it'd be elsewhere.

This is the output file right now:

1990

1991
June

1992
January
January 1
hour: 0 temperature: 61.5 F
February
February 1
hour: 1 temperature: 64 F
February 2
hour: 2 temperature: 65.2 F

2000
February
February 1
hour: 0 temperature: 67.2 F
hour: 1 temperature: 68 F
February 2
hour: 3 temperature: 66.66 F
December
December 14
hour: 0 temperature: -2 F
December 15
hour: 14 temperature: -8.8 F
hour: 15 temperature: -9.2 F
Last edited on
I suggest you take the advice given by Thomas1965 and put a comment block above that function, and all of your print functions for that matter and state the pre and post conditions of all of those functions.

By the way the code you posted above differs from the code in your last post, and that last post seems to now be different from the original code that you posted. Please don't alter the content of you previous posts as it makes it extremely difficult for others to follow the topic.

Here is the original of that function:
1
2
3
4
5
6
7
8
9
10
11
bool is_valid_day(const Day &d)
{
	for (size_t i = 1; i < d.hour.size(); ++i)
	{
		if (d.hour[i] != not_a_reading)
		{
			return true;
		}
	}
	return false;
}


If you have now modified that function, and nothing else then the output should be:

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
read 4 years of readings
1990

1991
        June

1992
        January
   January 1
      61.5 F
        February
  February 1
        64 F
  February 2
      65.2 F

2000
        February
  February 1
      67.2 F
        68 F
  February 2
     66.66 F
        December
  December 14
        -2 F
  December 15
      -8.8 F
      -9.2 F


But since this differs from your last post, it looks like you also modified something else as well so there would now be no way of us knowing where the problem would now lie, except that if you look closely at your latest output you should see that the problem is no longer there.


In the original code you posted yesterday the problem was in that validation function, the one that I posted. Can you see the difference between your "new" function and the original?

By the way are you getting help on this program from other sources as well?


The problem in there was that that the loop was starting from 1 instead of 0. It has to start from 0 so that the 0th hour is also printed.

I'll try commenting it later, but for now I'll just print the code as it is now:
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// temp_readings.cpp : Defines the entry point for the console application.
// Osman Zakir
// 2 / 7 / 2017
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 10 Sections 10.1.1 to 10.1.3
// This program takes temperature readings from an input file and prints it to
// output file.  The readings are represented in the form of readings for the
// year, month and hour.  Structs are used to represent Readings, Days, and Months.

#include "../../std_lib_facilities.h"

// not_a_reading == less than absolute zero
const int not_a_reading = -7777;

const int not_a_month = -1;

struct Day
{
	vector<double> hour{ vector<double>(24, not_a_reading) };
};

// a month of temperature readings
struct Month
{
	// [0:11] January is 0
	int month{ not_a_month };

	// [1:31] one vector of readings per day
	vector<Day> day{ 32 };
};

// a year of temperature readings, organized by month
struct Year
{
	// positive == A.D.
	int year;

	// [0:11] January is 0
	vector<Month> month{ 12 };
};

struct Reading
{
	int day;
	int hour;
	double temperature;
};

istream& operator>>(istream &is, Reading &r);
istream& operator>>(istream &is, Month &m);
istream& operator>>(istream &is, Year &y);
string int_to_month(int i);
int month_to_int(const string &s);
bool is_valid(const Reading &r);
bool is_valid_day(const Day &d);
void end_of_loop(istream &is, char term, const string &message);
void print_year(ostream &os, const Year &y);
void print_month(ostream &os, const Month &m);
void print_day(ostream &os, const Day &d);

int main()
{
	try
	{
		// open an input file:
		cout << "Please enter input file name\n";
		string iname;
		cin >> iname;
		ifstream ist{ iname };
		if (!ist)
		{
			error("can't open input file ", iname);
		}

		ist.exceptions(ist.exceptions() | ios_base::badbit);

		// open an output file:
		cout << "Please enter output file name\n";
		string oname;
		cin >> oname;
		ofstream ost{ oname };
		if (!ost)
		{
			error("can't open output file ", oname);
		}

		// read an arbitrary number of years:
		vector<Year> ys;
		while (true)
		{
			// get a freshly initialized Year each time around
			Year y;
			if (!(ist >> y))
			{
				break;
			}
			ys.push_back(y);
		}
		cout << "read " << ys.size() << " years of readings\n";

		for (const Year &y : ys)
		{
			print_year(ost, y);
		}
	}
	catch (const runtime_error &e)
	{
		cerr << "error: " << e.what() << '\n';
		keep_window_open();
		return 1;
	}
	catch (const exception &e)
	{
		cerr << "error: " << e.what() << '\n';
		keep_window_open();
		return 2;
	}
	keep_window_open();
	return 0;
}

istream& operator>>(istream &is, Reading &r)
// read a temperature reading from is into r
// format: ( 3 4 9.7 )
// check format, but don’t bother with data validity
{
	char ch1;
	if (is >> ch1 && ch1 != '(') 
	// could it be a Reading?
	{ 
		is.unget();
		is.clear(ios_base::failbit);
		return is;
	}
	char ch2;
	int d;
	int h;
	double t;
	is >> d >> h >> t >> ch2;
	if (!is || ch2 != ')')
	{
		// messed-up reading
		error("bad reading"); 
	}
	r.day = d;
	r.hour = h;
	r.temperature = t;
	return is;
}

istream& operator>>(istream &is, Month &m)
// read a month from is into m
// format: { month feb . . . }
{
	char ch = 0;
	if (is >> ch && ch != '{')
	{
		is.unget();

		// we failed to read a Month
		is.clear(ios_base::failbit);
		return is;
	}

	string month_marker;
	string mm;
	is >> month_marker >> mm;
	if (!is || month_marker != "month")
	{
		error("bad start of month");
	}
	m.month = month_to_int(mm);

	int duplicates = 0;
	int invalids = 0;
	for (Reading r; is >> r;)
	{
		if (is_valid(r))
		{
			if (m.day[r.day].hour[r.hour] != not_a_reading)
			{
				++duplicates;
			}
			m.day[r.day].hour[r.hour] = r.temperature;
		}
		else
		{
			++invalids;
		}
	}
	if (invalids)
	{
		error("invalid readings in month", invalids);
	}
	if (duplicates)
	{
		error("duplicate readings in month", duplicates);
	}
	end_of_loop(is, '}', "bad end of month");
	return is;
}

istream& operator>>(istream &is, Year &y)
// read a year from is into y
// format: { year 1972 . . . }
{
	char ch;
	is >> ch;
	if (ch != '{')
	{
		is.unget();
		is.clear(ios_base::failbit);
		return is;
	}

	string year_marker;
	int yy;
	is >> year_marker >> yy;
	if (!is || year_marker != "year")
	{
		error("bad start of year");
	}
	y.year = yy;

	while (true)
	{
		// get a clean m each time around
		Month m;
		if (!(is >> m))
		{
			break;
		}
		y.month[m.month] = m;
	}

	end_of_loop(is, '}', "bad end of year");
	return is;
}

int month_to_int(const string &s)
// is s the name of a month? If so return its index [0:11] otherwise – 1
{
	vector<string> month_input_tbl =
	{"jan", "feb", "mar", "apr", "may", "jun", 
		"jul", "aug", "sep", "oct", "nov", "dec"};

	for (int i = 0; i < 12; ++i)
	{
		if (month_input_tbl[i] == s)
		{
			return i;
		}
	}
	return -1;
}

string int_to_month(int i)
// months [0:11]
{
	vector<string> month_print_tbl =
	{ "January", "February", "March", "April", "May", "June",
	"July", "August", "September", "October", "November", "December" };

	if (i < 0 || i >= 12)
	{
		cout << i << ":\n";
		error("bad month index");
	}
	return month_print_tbl[i];
}

bool is_valid(const Reading &r)
// a rough test
{
	constexpr int implausible_min = -200;
	constexpr int implausible_max = 200;

	if (r.day < 1 || r.day > 31)
	{
		return false;
	}
	if (r.hour < 0 || r.hour > 23)
	{
		return false;
	}
	if (r.temperature < implausible_min || r.temperature > implausible_max)
	{
		return false;
	}
	return true;
}

bool is_valid_day(const Day &d)
{
	for (const auto &temp : d.hour)
	{
		if (temp != not_a_reading)
		{
			return true;
		}
	}
	return false;
}

void end_of_loop(istream &ist, char term, const string &message)
{
	// use term as terminator and/or separator
	if (ist.fail())
	{
		ist.clear();
		char ch;
		if (ist >> ch && ch == term)
		{
			// all is fine
			return;
		}
		error(message);
	}
}

void print_year(ostream &os, const Year &y)
{
	os << y.year << '\n';
	for (const auto &m : y.month)
	{
		if (m.month != not_a_month)
		{
			print_month(os, m);
		}
	}
	os << '\n';
}

void print_month(ostream &os, const Month &m)
{
	os << '\t' << int_to_month(m.month) << '\n';
	for (size_t i = 1; i < m.day.size(); ++i)
	{
		if (is_valid_day(m.day[i]))
		{
			os << setw(10) << int_to_month(m.month) << ' '
				<< i << '\n';
			print_day(os, m.day[i]);
		}
	}
}

void print_day(ostream &os, const Day &d)
{
	for (size_t i = 0; i < d.hour.size(); ++i)
	{
		if (d.hour[i] > not_a_reading)
		{
			os << setw(10) << "hour: " << i << ' ' 
				<< "temperature: " << d.hour[i] << " F\n";
		}
	}
}


Here's the output file:

1990

1991
June

1992
January
January 1
hour: 0 temperature: 61.5 F
February
February 1
hour: 1 temperature: 64 F
February 2
hour: 2 temperature: 65.2 F

2000
February
February 1
hour: 0 temperature: 67.2 F
hour: 1 temperature: 68 F
February 2
hour: 3 temperature: 66.66 F
December
December 14
hour: 0 temperature: -2 F
December 15
hour: 14 temperature: -8.8 F
hour: 15 temperature: -9.2 F
Last edited on
Topic archived. No new replies allowed.
Pages: 123