jeudi 28 mars 2019

The variable 'pos' is being used without being initialized

I have a bug in my code. How can I deal with it? It can't compile successfully, but I can't find anything wrong.

  • I am a newbie, the first time I use this website, I don't know what else to say but it seems to have a word limit so I have to add some meaningless text, I am very sorry.
  • I am a newbie, the first time I use this website, I don't know what else to say but it seems to have a word limit so I have to add some meaningless text, I am very sorry.
  • I am a newbie, the first time I use this website, I don't know what else to say but it seems to have a word limit so I have to add some meaningless text, I am very sorry.
  • I am a newbie, the first time I use this website, I don't know what else to say but it seems to have a word limit so I have to add some meaningless text, I am very sorry.
  • I am a newbie, the first time I use this website, I don't know what else to say but it seems to have a word limit so I have to add some meaningless text, I am very sorry.
  • I am a newbie, the first time I use this website, I don't know what else to say but it seems to have a word limit so I have to add some meaningless text, I am very sorry.
  • I am a newbie, the first time I use this website, I don't know what else to say but it seems to have a word limit so I have to add some meaningless text, I am very sorry.
  • I am a newbie, the first time I use this website, I don't know what else to say but it seems to have a word limit so I have to add some meaningless text, I am very sorry.

Day.h

#pragma once
#include<iostream>
#include<string>
typedef unsigned ud;
using std::string;
class Day
{
public:
    Day() = default;
    Day(string a) {
        decltype(a.size()) pos;
        if (a.find("Jan") != string::npos)
            month = 1;
        else if (a.find("Feb") != string::npos)
            month = 2;
        else if (a.find("Mar") != string::npos)
            month = 3;
        else if (a.find("Apr") != string::npos)
            month = 4;
        else if (a.find("May") != string::npos)
            month = 5;
        else if (a.find("Jun") != string::npos)
            month = 6;
        else if (a.find("Jul") != string::npos)
            month = 7;
        else if (a.find("Aug") != string::npos)
            month = 8;
        else if (a.find("Sep") != string::npos)
            month = 9;
        else if (a.find("Oct") != string::npos)
            month = 10;
        else if (a.find("Nov") != string::npos)
            month = 11;
        else if (a.find("Dec") != string::npos)
            month = 12;
        else {
            pos = a.find_first_not_of("123456789");
            month = stoi(a.substr(0, pos));
        }
        pos++;
        auto now = a.find_first_not_of("123456789", pos);
        day = stoi(a.substr(pos, now - pos));
        pos = now + 1;
        year = stoi(a.substr(pos, a.size() - pos));
    }
    ud get_year() {
        return year;
    }
    ud get_month() {
        return month;
    }
    ud get_day() {
        return day;
    }
    std::ostream& print(std::ostream& os) {
        os << year << ' ' << month << ' ' << day;
        return os;
    }
private:
    ud year;
    ud month;
    ud day;
    bool iszm(char x) {
        return (x >= 'A'&&x <= 'z');
    }
};

main.cpp

#include"pch.h"
#include<iostream>
#include<forward_list>
#include<deque>
#include<vector>
#include<string>
#include<list>
#include<array>
#include<cstdlib>
#include"Day.h"
using namespace std;
int main()
{
Day tmp("March 27,2019");
    tmp.print(cout);
return 0;
}

Aucun commentaire:

Enregistrer un commentaire