Read Dates file and count days elapsed in the year

Asked

Viewed 73 times

1

I am 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;
}

1 answer

0

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

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

To solve the problem just initialize the variable with 0

I enhanced 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;
}
  • Thank you so much for the help, I still make many basic mistakes. Practice will improve this.

  • @Nicollasstefansoaresdacost accepts the answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.