Reputation: 4095
I want to append integer value with string value but not a variable.
I tried to put an integer value which is variable with a string called February. I tried it using += operator but it did not work.
string getMonth(day)
{
if(day >=31 ){
day -= 31;
"February "+=day;
}
}
Upvotes: 2
Views: 882
Reputation: 311126
Do something like
#include <string>
// ...
std::string s( "February " );
s += std::to_string( day );
Here is a demonstrative program
#include <iostream>
#include <string>
int main()
{
std::string s( "February " );
int day = 20;
s += std::to_string( day );
std::cout << s << '\n';
}
Its output is
February 20
Another approach is to use a string stream.
Here is one more demonstrative program.
#include <iostream>
#include <sstream>
#include <string>
int main()
{
std::ostringstream oss;
int day = 20;
oss << "February " << day;
std::string s = oss.str();
std::cout << s << '\n';
}
Its output is the same as shown above.
Upvotes: 5
Reputation: 4095
The answer above solved my problem.. If you are using dev c++ like me then you might be in the same problem, I would recommend you to add -std=c++11. For this you can got to this link How to change mode from c++98 mode in Dev-C++ to a mode that supports C++0x (range based for)? and go to second answer. Thank you
Upvotes: 0