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
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main(void)
{
// Variables
string dataline = "";
string station_s = "", date_s = "", tmax_s = "", tmin_s = "", prcp_s = "";
unsigned int pos_tmax = 0;
unsigned int pos_tmin = 0;
unsigned int pos_prcp = 0;
unsigned int pos_station = 0;
unsigned int pos_date = 0;
float tmax = 0, tmin = 0, prcp = 0; // Real types for temps
int bad_records = 0; // Count the records with bad data for tmax and tmin
float max = 0;
float min = 100;
ifstream infile; // Input file pointer
ofstream outfile; // Output file pointer
cout << "WEATHER STATION DATA" << endl;
cout << "Open the data file." << endl;
infile.open("C:/Users/jerie/Downloads/weather_station_five_column.txt");
if (!infile)
{
cout << "Unable to open the input file. " << endl;
system("pause");
return 1;
}
cout << "Weather data file opened." << endl;
cout << "Use the first line of the file to find the column positions. " << endl;
getline(infile, dataline);
string heading = "STATION_NAME DATE PRCP TMAX TMIN";
// Use firest line of file (header) to find max and min temp columns
pos_tmax = dataline.find("TMAX");
if (pos_tmax <= dataline.length())
{
cout << "TMAX begins at column: " << pos_tmax << endl;
}
else
{
cout << "TMAX was not found - terminating." << endl;
system("pause");
return 3;
}
pos_tmin = dataline.find("TMIN");
if (pos_tmin <= dataline.length())
{
cout << "TMIN begins at column: " << pos_tmin << endl;
}
else
{
cout << "TMIN was not found - terminating." << endl;
system("pause");
return 4;
}
pos_prcp = dataline.find("PRCP");
if (pos_prcp <= dataline.length())
{
cout << "TMIN begins at column: " << pos_prcp << endl;
}
else
{
cout << "prcp was not found - terminating." << endl;
system("pause");
return 5;
}
pos_station = dataline.find("STATION_NAME");
pos_date = dataline.find("DATE");
cout << "Read the second line from the file - dashes. " << endl;
getline(infile, dataline);
while (!infile.eof())
{
getline(infile, dataline);
// Add this to check for empty lines
if (dataline.length() < 50)
{
cout << "Short line detected of length " << dataline.length() << endl;
continue; // Skip to the end of the loop.
}
// Read tmax and tmin as strings
tmax_s = dataline.substr(pos_tmax, 5);
tmin_s = dataline.substr(pos_tmin, 5);
prcp_s = dataline.substr(pos_prcp, 5);
station_s = dataline.substr(pos_station, 50);
date_s = dataline.substr(pos_date, 8);
tmax = stof(tmax_s);
tmin = stof(tmin_s);
prcp = stof(prcp_s);
if (tmax > max)
{
max = tmax;
}
if (tmin < min)
{
min = tmin;
}
}
cout << "Max: " << max << endl;
cout << "Min: " << min << endl;
//string max_s = to_string(max);
//string min_s = to_string(min);
//cout << max_s << endl;
//cout << min_s << endl;
infile.clear(); // clear fail and eof bits
infile.seekg(0, std::ios::beg); // back to the start!
getline(infile, dataline);
getline(infile, dataline);
while (!infile.eof())
{
getline(infile, dataline);
if (dataline.length() < 50)
{
cout << "Short line detected of length " << dataline.length() << endl;
continue; // Skip to the end of the loop.
}
tmax_s = dataline.substr(pos_tmax, 5);
tmin_s = dataline.substr(pos_tmin, 5);
prcp_s = dataline.substr(pos_prcp, 5);
station_s = dataline.substr(pos_station, 50);
date_s = dataline.substr(pos_date, 8);
tmax = stof(tmax_s);
tmin = stof(tmin_s);
prcp = stof(prcp_s);
/*find the position of when temp is max. grab that max. so when max(string) is found print out station*/
}
infile.close();
return 0;
}
|