2

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;
      }
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Biplove Lamichhane
  • 3,995
  • 4
  • 14
  • 30
  • 3
    Possible duplicate of [How do you append an int to a string in C++?](https://stackoverflow.com/questions/64782/how-do-you-append-an-int-to-a-string-in-c) – Piotr Siupa Jul 22 '19 at 10:13
  • @NO_NAME it you see a dupe, just vote to close with respective reason (comment you have used will be auto added). – Marek R Jul 22 '19 at 10:16
  • 2
    @MarekR I did just that? Well, I cannot vote to close because I don't have 3,000 reputation but I've clicked option `flag` and it auto-added the comment. – Piotr Siupa Jul 22 '19 at 10:21

2 Answers2

5

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I included string still it shows : "to_string was not defined" error is shown." – Biplove Lamichhane Jul 22 '19 at 10:25
  • @BiploveLamichhane If you copied the call exactly as it is written that is you used the qualified name std::to_string then maybe you need a more modern C++ compiler. – Vlad from Moscow Jul 22 '19 at 10:27
  • 1
    `std::to_string` is part of the standard since `c++11`. What compiler do you use? – Piotr Siupa Jul 22 '19 at 10:28
  • @NO_NAME I am using dev c++ version 5.11 – Biplove Lamichhane Jul 22 '19 at 10:30
  • OK, Dev-C++ is an environment, not a compiler but close enough. Wiki says that it uses MinGW (basically GCC) in version that should support c++11. Maybe is is not default in this version? Try to manually add `-std=c++11` to compiler options. – Piotr Siupa Jul 22 '19 at 10:35
  • @VladfromMoscow I am using "using namespace std" do i have to use std:: ?? I heard that using "using namespace std" is bad practic. Is it true? – Biplove Lamichhane Jul 22 '19 at 10:36
  • @BiploveLamichhane Yes in general it is a bad practice but for small programs there is nothing bad. – Vlad from Moscow Jul 22 '19 at 10:40
  • @NO_NAME how do I add std=c++11 in dev c++. Sorry if i am irritating you with these questions, but these are so new for me . – Biplove Lamichhane Jul 22 '19 at 10:41
  • @BiploveLamichhane No idea. I've used Dev-C++ like 10 years ago but I've quickly stopped because this crashes was legendary (ah nostalgia). I remember nothing. Try to look in project setting for something names compiler arguments/options, though. – Piotr Siupa Jul 22 '19 at 10:44
  • @NO_NAME thank u so much.. I appreciate your help..... I added that and it worked as well – Biplove Lamichhane Jul 22 '19 at 10:51
0

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

Biplove Lamichhane
  • 3,995
  • 4
  • 14
  • 30