0

I would like to develop application which provides day, hour, minute and second inputs from a user with setters. I am getting values from the user with setters correctly.

But I can not return value as DD:HH:MM:SS. I coded a method called getDate and expect to get all values in that method but it only returns the second value of Date2 object.

Here is what I tried.

#include "pch.h"
#include <iostream>
using namespace std;

class Date
{
public:
    int getDate(void)
    {
        return day, std::string(":"), hour, std::string(":"), minute, std::string(":"), second;
    }
    int getDay(void) {
        return day;
    }
    int getHour(void) {
        return hour;
    }
    int getMinute(void) {
        return minute;
    }
    int getSecond(void) {
        return second;
    }
    void setDay(int a) {
        day= a;
    }
    void setHour(int b) {
        hour = b;
    }
    void setMinute(int c) {
        minute = c;
    }
    void setSecond(int d) {
        second = d;
    }

private:    
    int day;
    int hour;
    int minute;
    int second;
};

// Main function for the program
int main()
{
    Date Date1;                
    Date Date2;                                
    int day= 0;     
    int hour= 0;
    int minute = 0;
    int second= 0;
    // Date1 specification
    Date1.setDay(23);
    Date1.setHour(14);
    Date1.setMinute(43);
    Date1.setSecond(21);

    // Tarih2  specification
    Date2.setDay(12);
    Date2.setHour(43);
    Date2.setMinute(25);
    Date2.setSecond(49);
    day = Date1.getDay();
    cout << "Day of Date1 : " << day << endl;
    hour = Date1.getHour();
    cout << "Hour of Date1 : " << hour << endl;
    minute = Date1.getMinute();
    cout << "Minute of Date1 : " << minute << endl;
    second = Date1.getSecond();
    cout << "Second of Date1 : " << second << endl;

    cout << "Date1 : " << Date1.getDate()<< endl;

    cout << "Date2 : " << Date2.getDate()<< endl;
    return 0;
}
JeJo
  • 30,635
  • 6
  • 49
  • 88
  • 3
    What do you expect `return day, std::string(":"), hour, std::string(":"), minute, std::string(":"), second;` to give you? Why does the function have the return type `int`? – NathanOliver May 16 '19 at 16:06
  • 1
    You want a `string`, yet you are returning `int`. That makes *no* sense. – Jesper Juhl May 16 '19 at 17:04

4 Answers4

2

Your int getDate(void) function is wrong. To format the member variables to the for DD:HH:MM:SS, you could

  1. either rewrite the getDate function in such a way that it returns a std::string after concatenating the class members together to DD:HH:MM:SS format.

    #include <iostream>
    #include <string>
    #include <sstream>  // std::stringstream
    
    class Date
    {
    private:
        // members
    public:
        std::string getDate() /* const noexcept */
        {
            std::stringstream sstr; // DD:HH:MM:SS
            sstr << day << ":" << hour << ":" << minute << ":" << second << '\n';
            return sstr.str();
        }
        // other codes
    };
    
    int main()
    {
        Date obj{};
        std::cout << obj.getDate(); // should print as DD:HH:MM:SS
    }
    
  2. Or provide an output stream operator<<overload which is the usual approach.

    #include <iostream>
    #include <sstream>   // std::stringstream
    
    class Date
    {
    private:
        // members
    public:
        friend std::ostream& operator<< (std::ostream& out, const Date& obj) /* noexcept */;
        // other member functions
    };
    // outside the class defenition
    std::ostream& operator<< (std::ostream& out, const Date& obj) /* noexcept */
    {
        std::stringstream sstr; // DD:HH:MM:SS
        sstr << obj.day << ":" << obj.hour << ":" << obj.minute << ":" << obj.second << '\n';
        return out << sstr.str();
    }
    
    int main()
    {
        Date obj{};
        std::cout << obj; // will console out in DD:HH:MM:SS format
    }
    
JeJo
  • 30,635
  • 6
  • 49
  • 88
0
return day, std::string(":"), hour, std::string(":"), minute, std::string(":"), second;

What's this? Use sprintf or a variant of it.

char buff[100] = {0};
sprintf_s(buff, 100, "%02u:%02u", hours, minutes);
Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
Michael Chourdakis
  • 10,345
  • 3
  • 42
  • 78
  • I don't know how can I use this in my code.Can you make it on my code please? – Alex Webster May 16 '19 at 16:10
  • 1
    Why use `sprintf` in C++? Why not `std::string`? – Jesper Juhl May 16 '19 at 16:18
  • Because sprintf is also C++. – Michael Chourdakis May 16 '19 at 16:29
  • 1
    @MichaelChourdakis True. It *is*. But it's part of the clunky null-terminated-string stuff we inherited from C and that noone should be using unless forced to. C++ can do better than that legacy thing. – Jesper Juhl May 16 '19 at 17:12
  • sprintf (and the entire printf family) has no real C++ replacement. Even if 99% of the language is modern, there is no need to use something inferior just because it's more modern than the older one. And in any case my answer was referring to the way he should avoid the return line he has. – Michael Chourdakis May 16 '19 at 17:14
  • There may be times you will wish to use C functions such as sprintf, like when you have requirements for them, or your efforts are to provide 'end-of-life' support form some older code (just to last a little longer). I think these are about the only reasons to think about them. But the C style code is not for new efforts. Not for learning c++. Kudo's to you, Alex Webster, for your class date. Your effort is clearly not hospice for other peoples code.. – 2785528 May 16 '19 at 17:56
  • @jes: Using `std::string` doesn't solve the formatting requirements, unless you are willing to write lengthy workarounds, that are hard to write and hard to read. There is no value in being dogmatic about rejecting C library code just because it is C library code. C++ currently doesn't have anything equally succinct to offer (until C++20 introduces [std::chrono::format](https://en.cppreference.com/w/cpp/chrono/format)). – IInspectable May 17 '19 at 21:43
0

Why don't you put everything into a string ? Create one string, for example: string fullDate, and add everything into it and then return it

SomeName
  • 909
  • 2
  • 8
  • 15
0

Why dont you use 'tm' structure? struct tm - C++ Reference

Here go some code that I always have "on sleeve".

#include <iostream>
#include <chrono>
#include <sstream>
#include <iomanip>

using namespace std;

time_t GetDateTime_TT(int &ms)
{
  chrono::time_point<chrono::system_clock> TpNow = chrono::system_clock::now();
  chrono::system_clock::duration Durat = TpNow.time_since_epoch();
  long long Millis = chrono::duration_cast<chrono::milliseconds>(Durat).count();
  time_t Tt = chrono::system_clock::to_time_t(TpNow);
  ms = Millis % 1000;
  return Tt;
}

tm *GetDateTime_TM(int &ms)
{
  time_t Tt = GetDateTime_TT(ms);
  tm *Tm = localtime(&Tt);
  return Tm;
}

string DateTimeToString(tm *tm, const string &format)
{
  char buf[90];
  strftime(buf, 90, format.c_str(), tm);
  return string(buf);
}

tm StringToDateTime_TM(const string &value, const string &format)
{
  tm tm;
  tm.tm_year = 0;
  tm.tm_mon = 0;
  tm.tm_mday = 0;
  tm.tm_hour = 0;
  tm.tm_min = 0;
  tm.tm_sec = 0;
  tm.tm_yday = 0;
  tm.tm_wday = 0;
  tm.tm_isdst = 0;
  istringstream iss(value);
  iss >> get_time(&tm, format.c_str());
  return tm;
}

int main(int argc, char *argv[])
{
  int ms;
  tm *Now = GetDateTime_TM(ms);
  string DT = DateTimeToString(Now, "%Y-%m-%d %H:%M:%S");
  cout << "Now: " << DT.c_str() << "\n";
  tm *End = &StringToDateTime_TM("2019-12-31 23:59:59", "%Y-%m-%d %H:%M:%S");
  return 0;
}