0

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);
    }
}
user149379
  • 11
  • 5
  • Hints: There is no such thing as `void main` Only `Zuul`. Sorry. Only `int main`. Remove from your code stuff to which we are not privy, such as library.h and the make and draw functions. – user4581301 Nov 09 '15 at 19:57
  • Question: what if your file does not contain 364 lines? – user4581301 Nov 09 '15 at 19:59
  • i actually have to use that library, and void main() is defined in it. perhaps it has to do with not using eof()? – user149379 Nov 09 '15 at 20:01
  • [Don't use eof. That trick never works.](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – user4581301 Nov 09 '15 at 20:02
  • This appears to read and draw the first 364 entries in the source file. If you want from March on, you will have to find March and then start printing. That or edit the file so the file starts with March. – user4581301 Nov 09 '15 at 20:04
  • it is printing from march onwards. the issue is that it's not printing the first two months worth of entries. only march 8-dec 31. – user149379 Nov 09 '15 at 20:07
  • Clarifying. You have to use library.h. We can't use it because we don't have it. Therefore anyone who tries to help you out can't compile your code to see what's wrong. When you post the code that is giving you grief, you have to ensure that it is a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). The posted code fails complete and thus is not verifiable. This means you will have to leave out stuff or add more stuff in. In this case it is easier to leave it out. – user4581301 Nov 09 '15 at 20:08
  • Suggestion: Rather than rolling your own month to day converter, use the stuff built into C++. [`struct tm`](http://en.cppreference.com/w/cpp/chrono/c/tm) and it's handler functions does most of what you want for you. – user4581301 Nov 09 '15 at 20:20
  • A wild guess: On March 8th the temperature is much higher than in January or February. Perhaps that makes the point appear inside the visible window? – Bo Persson Nov 09 '15 at 20:48
  • You could use an array of offsets instead of using the `if-then-else` ladder. Much faster. – Thomas Matthews Nov 09 '15 at 20:50
  • Suggestion: For now replace the graphing with `cout << daySinceJan(mo[i], day[i], yr[i]) << "," << avgTemp[i] * 2 << endl;` and see what prints out. If the printing still starts in March I have no idea what's going on. Otherwise follow @BoPersson 's suggestion and start debugging looking into what's going to happen when the temperature is negative. – user4581301 Nov 09 '15 at 20:58
  • great! thank you so much! – user149379 Nov 09 '15 at 21:36

0 Answers0