0

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

273K
  • 29,503
  • 10
  • 41
  • 64
Guileful
  • 1
  • 1
  • I don't see your attempt to use `std::getline` here. It is part of the standard library and has been for decades. I suggest you show what you tried so we can correct you. See https://stackoverflow.com/questions/7868936/read-file-line-by-line-using-ifstream-in-c – paddy Nov 08 '22 at 02:58
  • [Why is `iostream::eof` inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – Evg Nov 08 '22 at 03:01
  • Not sure what you want to do, do you want to find a specific record in the file by day? – Mahdy.n Nov 08 '22 at 04:12
  • I.e. line one number 56 is maxtemp, and number 26 is mintemp. The rest of the first line is not needed. Second line number 40 is maxtemp, and number 12 is mintemp etc And so on for 31 lines(dates). We need to read min and max and output how many days temp was above 75 degrees, how many days between 33-75 degrees, between 32 and 0 degree, and below 0 degree – Guileful Nov 08 '22 at 21:59
  • After reading the first 3 numbers, call `jan91.ignore(std::numeric_limits::max(), '\n')` to skip all remaining characters in the current line. – Igor Tandetnik Nov 08 '22 at 22:38
  • What if I will need to read numbers placed at 8th and 9th position of a line – Guileful Nov 09 '22 at 12:42

0 Answers0