lundi 28 juin 2021

Problem reading a formatted text file in C++

High guys, officially my first post. I'm sure the Stack is full of answers, but the problem that I need help with is a little bit specific. So here goes nothing...

The Task:

I'm doing a small school project and in one part of my program I need to read the temperature measurements at different locations, all from a single formatted text file. The data inside the file is written as follows:

23/5/2016
Location 1
-7,12,-16,20,18,13,6
9/11/2014
Location 2
−1,3,6,10,8
9/11/2014
Location 3
−5,−2,0,3,1,2,−1,−4

The first row represents the date, second row the location and the third row represents the all the measurements the were taken on that day (degrees Celsius). The code that I wrote for this part of the program looks something like this:

tok.seekg(0, std::ios::beg);
int i = 0;
double element;
char sign = ',';
while (!tok.eof()) {
vector_measurements.resize(vektor_measurements.size() + 1);
tok >> vektor_measurements.at(i).day >> sign >> vektor_measurements.at(i).month >>
    sign >> vektor_measurements.at(i).year >> std::ws;
std::getline(tok, vektor_measurements.at(i).location);
sign = ',';
while (tok && sign == ',') {
  tok >> element;
  vektor_measurements.at(i).measurements.push_back(element);
  sign = tok.get();
}
if (!tok.eof() && !tok) {
  tok.clear();
  break;
}
vektor_measurements.at(i).SetAverage();
i++;

The code that I'm presenting is linked to a class:

struct Data {
  std::string location;
  std::vector<int> measurements;
  int day, month, year;
  double average = 0;

  void SetAverage();
  int GetMinimalTemperature();
  int GetMaximalTemperature();
};

I've already checked and confirmed that the file exists and the stream is opened in the correct mode without any errors; all class methods working as intended. But here's the problem. Later on, after the data is sorted (the part of data that has been successfully read), it fails to correctly print the data on the screen. I get something like:

Location 2
Date: 9/11/2014
Minimal temperature: 0
Maximal temperature: 0
Average temperature: 0

Location 1
Date: 23/5/2016
Minimal temperature: -16
Maximal temperature: 20
Average temperature: 6.57143

; but I expect:

Location 3
----------
Date: 9/11/2014
Minimal temperature: -5
Maximal temperature: 3
Average temperature: -0.75

Location 2
----------
Date: 9/11/2014
Minimal temperature: -1
Maximal temperature: 10
Average temperature: 5.20

Location 1
----------
Date: 23/5/2016
Minimal temperature: -16
Maximal temperature: 20
Average temperature: 6.57143

The Problem:

The order of the locations is good, since I'm sorting from the lowest to the highest average temperature. But no matter the number of locations, the first location is always correct, the second one only has zero's, and every other location isn't even printed on the screen. What do I need to change in order for my program to read the data properly? Or am I just missing something? Forgive me for any spelling mistakes I made since English isn't my native language. Thank you all in advance, any help is appreciated!

Aucun commentaire:

Enregistrer un commentaire