Read Dates file and count days elapsed in the year

1

I'm trying to make a program that opens a file and calculates the days elapsed from the date.

The dates are in the file as follows: 30-10-2018 . But I'm having trouble with the output that is printing garbage.

Follow my code so far.

include iostream
include fstream
include sstream
include string
include vector

using namespace std;

int main() {

        std::vector<std::string> my_arr;
        std::ifstream dict_file("bissextos.txt");
        std::string line;

        while(std::getline(dict_file, line))
      {
          std::string new_line;
          new_line = line + "\n";
          std::cout<<new_line;
          my_arr.push_back(new_line);

        std::istringstream stm(new_line) ;
        int day, month, year ;
        char delim ;
        stm >> day >> delim ;
        stm >> month >> delim ;
        stm >> year ;

      int meses[] = {31,28,31,30,31,30,31,31,30,31,30,31}; 
      int meses_bis[] = {31,29,31,30,31,30,31,31,30,31,30,31};
      int soma_meses, contagem_dias, ano_bis;

      if (year % 4 == 0 && (year % 400 == 0 || year % 100 != 0)) {
           ano_bis = 'S';
        }
         else {
           ano_bis = 'N';
          }

          switch(ano_bis) {
            case 'S' :
             for (int i=0; i<(month -1); i++){ 
               soma_meses += meses_bis[i];
                contagem_dias = soma_meses + day;
             }
                 cout << "int: " << contagem_dias;
            break;
            default :
             for (int i=0; i<(month -1); i++){ 
                soma_meses += meses[i];
                 contagem_dias = soma_meses + day;
            }
                cout << "int: " << contagem_dias;

          }

}


    return 0;
}
    
asked by anonymous 30.10.2018 / 20:33

1 answer

0

You did not initialize soma_meses , then any memory value (garbage) was

C ++ initializes global and static variables with zero, local variables get any value from memory

To solve the problem, just initialize the variable with 0

I've improved your code:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

int main() {
    const int meses[] = {31,28,31,30,31,30,31,31,30,31,30,31}; 
    const int meses_bis[] = {31,29,31,30,31,30,31,31,30,31,30,31};

    vector<std::string> my_arr;
    ifstream dict_file("bissextos.txt");

    if (!dict_file.is_open())
    {
        cerr << "Houve um erro ao abrir o arquivo.";
        return 1;
    }

    std::string line;

    while(getline(dict_file, line))
    {
        cout << line << endl;

        my_arr.push_back(line);

        istringstream stm(line) ;
        int day, month, year ;
        char delim;

        stm >> day >> delim;
        stm >> month >> delim;
        stm >> year;

        int soma_meses = 0;
        int contagem_dias = 0;

        bool is_ano_bis = false;

        if (year % 4 == 0 && (year % 400 == 0 || year % 100 != 0)) {
            is_ano_bis = true;
        }

        if(is_ano_bis) {
            for (int i = 0; i < (month - 1); i++) { 
                soma_meses += meses_bis[i];
                contagem_dias = soma_meses + day;
            }

            cout << "int: " << contagem_dias << endl;
        } else {
            for (int i = 0; i < (month - 1); i++) { 
                soma_meses += meses[i];
                contagem_dias = soma_meses + day;
            }

            cout << "int: " << contagem_dias << endl;
        }

    }


    return 0;
}
    
31.10.2018 / 00:04