I am new to programming, and I am having an issue with a loop. I am reading in a file which displays numerical values relating to the weather. Here is a snippet:
2003 1 1 18 0 -1 36 50 46
2003 1 2 16 3 -1 43 56 52
2003 1 3 19 7 -1 42 56 49
2003 1 4 14 3 -1 42 58 50
The second column represents the month and the third column represents the day of the month. The file displays data for an entire year. I successfully read each column into an array, and am trying to print out only the month and day columns. However, when I do this, my program starts printing from March 8th, and I need it to begin printing from Jan 1-December 31. This is my code so far.
#include "library.h"
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
ifstream in;
int yr[364], mo[364], day[364], windSpeed[364], precip[364], snowDepth[364], minTemp[364], maxTemp[364], avgTemp[364];
int x_pos;
int y_pos;
int daySinceJan(int month, int dayOfMonth, int year) {
int offset;
if (month == 1 || month == 4 || month == 5) {
offset = 0;
}
if (month == 2 || month == 6 || month == 7) {
offset = 1;
}
if (month == 3) {
offset = -1;
}
if (month == 8) {
offset = 2;
}
if (month == 9 || month == 10) {
offset = 3;
}
if (month == 11 || month == 12) {
offset = 4;
}
return (month - 1) * 30 + offset + dayOfMonth;
}
void main() {
make_window(800, 800);
set_pen_color(color::red);
set_pen_width(8);
// open file, read in data
in.open("PORTLAND-OR.TXT");
if (in.is_open()) {
// read each column into an array
for (int i = 0; i < 364; i++) {
in >> yr[i] >> mo[i] >> day[i] >> windSpeed[i] >> precip[i] >> snowDepth[i] >> minTemp[i] >> maxTemp[i] >> avgTemp[i];
int x_pos = daySinceJan(mo[i], day[i], yr[i]);
int y_pos = avgTemp[i] * 2;
draw_point(x_pos, y_pos);
}
in.close();
}
else {
cout << "error reading file" << endl;
exit(1);
}
}