1

I am new to programming, so I have what is probably a basic question. I currently have a text file with 365 lines...one line per day of the year. These are the first four lines of the file:

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

I eventually have to graph these using a special library given to us, but first I want to put the data for each column into an array. This is the part of my code where I attempt to do just that.

#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];

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];
            cout << mo[i] << "    " << day[i] << endl;

        }
        in.close();
    }
    else {
        cout << "error reading file" << endl;
        exit(1);
    }
}

When I attempt to print out all of the values in the second and third columns (month and day), it begins printing from march 8 (3 8) through december 31 (12 31). I need it to print all the way from January 1 to December 31. Is there a reason why the first two months worth of values isn't printing?

user149379
  • 11
  • 5
  • It's difficult to see what's wrong without seeing how the arrays are declared and how they are printed. – R Sahu Nov 11 '15 at 03:44
  • post more useful code such as array declaration,,, – RajSharma Nov 11 '15 at 03:46
  • need the input file because i have tested with few inputs and it's working – Md Shibbir Hossen Nov 11 '15 at 04:03
  • i pasted the first four lines of the input file. the file contains 365 lines. the first column is the year, the second is the month, and the third is the day. the rest of the columns are data relating to the weather. – user149379 Nov 11 '15 at 04:03

1 Answers1

1

Below is a really stupid main-only program that reads your file in with your existing reading code and prints it back out again. I've embedded a running commentary in the code with things you should consider doing.

Basically, this is whaty I was talking about yesterday in loop not displaying all data entries from file

#include <iostream>
#include <fstream>

using namespace std; // bad! Avoid doing this in real life.

int yr[364], 
    mo[364], 
    day[364], 
    windSpeed[364], 
    precip[364], 
    snowDepth[364], 
    minTemp[364], 
    maxTemp[364], 
    avgTemp[364]; // bad! define a structure instead


/* Example:
struct stats
{
    int yr; 
    int mo; 
    int day; 
    int windSpeed; 
    int precip; 
    int snowDepth; 
    int minTemp; 
    int maxTemp; 
    int avgTemp;
};

struct stats statistics[364]; // bad! use a std::vector instead

std::vector<stats> statistics; 
*/

int main()
{
    // removed all the windowing stuff.
    ifstream in;
    in.open("PORTLAND-OR.TXT");
    if (in.is_open())
    {
        // read each column into an array
        for (int i = 0; i < 364; i++)
        { // what do you do if the file ends early or contains bad information?
            in >> yr[i] >>
                  mo[i] >>
                  day[i] >>
                  windSpeed[i] >>
                  precip[i] >>
                  snowDepth[i] >>
                  minTemp[i] >>
                  maxTemp[i] >>
                  avgTemp[i];
        }
        in.close();
    }
    else
    {
        cout << "error reading file" << endl;
        return 1;
    }
    // printing out all the stuff that was read 
    for (int i = 0; i < 364; i++)
    {
        cout << yr[i] << "," <<
                mo[i] << "," <<
                day[i] << "," <<
                windSpeed[i] <<  "," <<
                precip[i] <<  "," <<
                snowDepth[i] <<  "," <<
                minTemp[i] <<  "," <<
                maxTemp[i] <<  "," <<
                avgTemp[i] << endl;
    }
}
Community
  • 1
  • 1
user4581301
  • 33,082
  • 7
  • 33
  • 54