C ++ to_string error with date

I am trying to write a function that returns a date as a string. I did it like this:

string date() // includes not listed for sake of space, using namespace std { tm timeStruct; int currentMonth = timeStruct.tm_mon + 1; int currentDay = timeStruct.tm_mday; int currentYear = timeStruct.tm_year - 100; string currentDate = to_string(currentMonth) + "/" + to_string(currentDay) + "/" + to_string(currentYear); return currentDate; } 

this gives four compile-time errors. 1 of them:

to_string was not declared in this scope

and 3 of them:

Function to_string could not be resolved

one for each use of to_string.

Worldwide on the Internet, this code should work. Can someone shed light on an item?

+4
source share
2 answers

As mentioned in the comments, what you are trying to use requires C ++ 11. This means both a compiler that supports C ++ 11 (for example, GCC 4.7+), and possibly manual inclusion of C ++ 11 (Eg flag -std=c++11 ), so check them both out if you think it should work for you.

If you are stuck in a compiler that does not support C ++ 11, you can use the following to achieve what you want with regular C ++:

 string date() { tm timeStruct; int currentMonth = timeStruct.tm_mon + 1; int currentDay = timeStruct.tm_mday; int currentYear = timeStruct.tm_year - 100; char currentDate[30]; sprintf(currentDate, "%02d/%02d/%d", currentMonth, currentDay, currentYear); return currentDate; // it will automatically be converted to string } 

Note that for the Day and Month parameters, I used %02d to display at least 2 digits, so 5/1 will actually be represented as 05/01 . If you don't want this, you can simply use %d instead, which will behave like your original to_string . (I'm not sure which format you use for currentYear , but you probably also want to use %02d or %04d for this parameter)

+3
source

std::to_string is part of C ++ 11 and is located in the <string> header. The following code works with g ++ 4.7, as well as the latest versions of clang and VC ++. If compiling a file with this content does not work for you, you either call the compiler for C ++ 11 incorrectly or use a compiler version with insufficient support for C ++ 11.

 #include <string> int main() { int i; auto s = std::to_string(i); } 

However, there is a better way to print dates in C ++ 11. Here is a program that prints the current date (in ISO 8601 format).

 #include <ctime> // time, time_t, tm, localtime #include <iomanip> // put_time #include <iostream> // cout #include <sstream> // stringstream #include <stdexcept> // runtime_error #include <string> // string std::string date() { static constexpr char const *date_format = "%Y-%m-%d"; // ISO 8601 format auto t = std::time(nullptr); if (static_cast<std::time_t>(-1) == t) { throw std::runtime_error{"std::time failed"}; } auto *cal = std::localtime(&t); if (nullptr == cal) { throw std::runtime_error{"std::localetime failed"}; } std::stringstream ss; ss << std::put_time(cal, date_format); return ss.str(); } int main() { std::cout << date() << '\n'; } 

Unfortunately, gcc 4.8 lacks put_time (and, of course, VC ++ currently does not have constexpr and universal initializers, but it is easy to work. VC ++ has put_time ).

+1
source

Source: https://habr.com/ru/post/1482862/


All Articles