Keresés

Új hozzászólás Aktív témák

  • Gyuri16

    senior tag

    válasz j0k3r! #737 üzenetére

    #include <iostream>
    #include <sstream>
    #include <time.h>

    using namespace std;

    int main() {
    time_t t;
    time(&t);
    struct tm * tt = localtime(&t); // feltoltjuk a tm structot

    // kiszamoljuk az ertekeket a struct leirasa alapjan
    int year = 1900 + tt->tm_year;
    int month = 1 + tt->tm_mon;
    int day = tt->tm_mday;

    stringstream ss; // Int -> String konverziohoz
    ss << year << '.'; //Ev

    if (month < 10) { // 1 helyett 01 legyen
    ss << 0;
    }
    ss << month << '.'; //Honap

    if (day < 10) {
    ss << 0;
    }
    ss << day << '.'; //Nap

    string datum = ss.str(); //stringet csinalunk belole
    cout << datum << endl;

    return 0;
    }

    most vettem csak eszre, hogy char tombot hasznalsz, szoval akkor meg stringbol csinalni kell egy char[]-t, pl igy:

    char d[12];
    strcpy(d, datum.c_str());

    (strcpynek a string.h include kell)
    vigyazz mert a te kododban csak 11 byteot foglalsz le a datumnak, de az 12 byteot foglal (a null byte a vegen)

    Nem vagyok egoista, csak uborkagyalu!

Új hozzászólás Aktív témák