0

I'm attempting to set a date in C++ via a string via Google Tests, and when I am doing this only a part of the string displays. The string being used is ""23/07/1997", however what is being returned in the test is: "23/7/1997" -- I can't seem to work out why it's removing the leading 0 in the month. If someone would be able to explain it to me it would be greatly appreciated!

For this code I am using a UUEntry.ccp, UUEntry.h, UUDate.ccp, and UUDate.h:

UUEntry.cpp


void UUEntry::SetEntryDate(std::string date) {
    entryDate = date;
}

UUEntry.h

{
private:
    UUDate entry_date_; //Date of the entry
    UUTime entry_time_; //Start time of the entry
    int entry_duration_; //in seconds - cannot be > 12 hours, cannot be negative, can be 0
    std::string entry_title_;
    std::string entry_description_;

    //Used for reminders only
    UUReminderType entry_repeat_mode_; //Entries may or may not repeat, see comment above (on UUReminderType)
    int entry_repeat_duration_; //How many times the entry repeats. Valid range [0..30]

public:
std::string GetEntryDateString() const;
    UUDate GetEntryDate() const;
    void SetEntryDate(std::string date);

UUDate.cpp



    if (date.length() == 10) {
        std::string sday = date.substr(0, 2);
        std::string smonth = date.substr(3, 4);
        std::string syear = date.substr(6, 9);
        day_ = stoi(sday);
        month_ = stoi(smonth);
        year_ = stoi(syear);
    }
    else if (date.length() == 8) {
        std::string sday = date.substr(0);
        std::string smonth = date.substr(2);
        std::string syear = date.substr(4, 7);
        day_ = stoi(sday);
        month_ = stoi(smonth);
        year_ = stoi(syear);
    }
    else {
        date = "01/01/2000";
        std::string sday = date.substr(0, 2);
        std::string smonth = date.substr(3, 4);
        std::string syear = date.substr(6, 9);
        day_ = stoi(sday);
        month_ = stoi(smonth);
        year_ = stoi(syear);
    }

}

UUDate.h

#include <string>


class UUDate
{

private:
    int day_;
    int month_;
    int year_;

public:
    UUDate();


    UUDate(int day, int month, int year);
    UUDate(std::string date);

    int Between(UUDate date);
    std::string GetDate();



    int GetDay() const;
    void SetDay(int day);
    int GetMonth() const;
    void SetMonth(int month);
    int GetYear() const;
    void SetYear(int year);

    void IncrementDate();


};
  • Where is the display (and or formatting) code? Please read [ask] with a [mcve]. This means that you should post a complete program that only contains relevant code. – Richard Critten May 22 '20 at 12:59
  • where do you use `day_` to build the output ? – Jeffrey May 22 '20 at 13:00
  • `stoi` converts a string to an `int` and printing an `int` normally never prints leading 0s. You need to tell `cout` when you want unusual formatting. See the duplicate. – François Andrieux May 22 '20 at 13:00

0 Answers0