This a problem from introduction to C++ course. We need to read a Data from the file jan91.dat. Data looks like that.
1 59 26 43 .. .. .. .. ..
2 40 12 24 .. .. .. .. ..
3 21 14 18 .. .. .. .. ..
4 .. .. .. .. .. .. .. ..
First number is a date, we want to read only second and third number from each line. Second number is Max Temperature, and third number is Min Temperature. Later we may need to read other numbers in a line like humidity etc that (but it is not required yet).
// This program determines the number of days in each of six
// temperature categories for the days of January 1991.
#include <iostream> // required for cin and cout
#include <iomanip> // required for set precision, setw
#include <fstream> // required for ifstream and ofstream
#include <string> // required for string
using namespace std; // standard library
int main()
{
// Name of Variables
int i, below0 = 0, from0to32 = 0, from33to75 = 0, above75 = 0;
double min_temp, max_temp, date = 0;
const string FILENAME = "jan91.dat";
// Open input file
ifstream jan91;
jan91.open(FILENAME.c_str());
if (jan91.fail()) {
cerr << "Error opening file" << endl;
exit(1);
}
// Read and check temperature per day
while (!jan91.eof()) {
(jan91 >> date >> max_temp >> min_temp);
if (min_temp < 0);
below0++;
if (max_temp > 75) {
from0to32++;
from33to75++;
above75++;
}
else if (max_temp > 33 && max_temp < 75) {
from0to32++;
from33to75++;
}
else if (max_temp > 0 && max_temp < 33) {
from0to32++;
}
}
// Print results
cout << "January of 1991" << endl;
cout << "Temperature Ranges \t Number of Days " << endl;
cout << "Below 0 \t \t\t" << below0 << endl;
cout << "0 to 32 \t \t\t" << from0to32 << endl;
cout << "33 to 75\t \t\t" << from33to75 << endl;
cout << "Above 75 \t \t\t" << above75 << endl;
// Close file
jan91.close();
// Exit program.
return 0;
}
some people suggested to use "getline" but for some reason I could not use it in my Visual Studio 17.3.3
Please advise