Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Converting int to string

Converting int to string

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    J_E_D_I
    wrote on last edited by
    #1

    I have two questions. The first one is regarding the conversion from int to string. I can successfully convert a numeric string to int with the follwing code (particularly useful if you need to make mathematical operations on the numeric values contained in a string): string VALUE_AS_STRING = "1234"; int VALUE_AS_INT = atof (VALUE_AS_STRING.c_str()); cout << "The numeric value of the string is: " << VALUE_AS_INT; ...but I am not able to do the opposite, eg convert an int into a string. Any help please? Second question : How come the condition below results in an error? How should it rather be written? char CHARACTER = "1"; if (CHARACTER == "1") {cout << "True";} Many thanks

    M M J T 4 Replies Last reply
    0
    • J J_E_D_I

      I have two questions. The first one is regarding the conversion from int to string. I can successfully convert a numeric string to int with the follwing code (particularly useful if you need to make mathematical operations on the numeric values contained in a string): string VALUE_AS_STRING = "1234"; int VALUE_AS_INT = atof (VALUE_AS_STRING.c_str()); cout << "The numeric value of the string is: " << VALUE_AS_INT; ...but I am not able to do the opposite, eg convert an int into a string. Any help please? Second question : How come the condition below results in an error? How should it rather be written? char CHARACTER = "1"; if (CHARACTER == "1") {cout << "True";} Many thanks

      M Offline
      M Offline
      Maxwell Chen
      wrote on last edited by
      #2

      J_E_D_I wrote:

      int VALUE_AS_INT = atof (VALUE_AS_STRING.c_str());

      Should use atoi().

      J_E_D_I wrote:

      I am not able to do the opposite, eg convert an int into a string.

      sprintf()

      J_E_D_I wrote:

      char CHARACTER = "1"; if (CHARACTER == "1") {cout << "True";}

      Use single quote.

      if(character == 'a') {
      cout << "It is a";
      }

      Maxwell Chen

      1 Reply Last reply
      0
      • J J_E_D_I

        I have two questions. The first one is regarding the conversion from int to string. I can successfully convert a numeric string to int with the follwing code (particularly useful if you need to make mathematical operations on the numeric values contained in a string): string VALUE_AS_STRING = "1234"; int VALUE_AS_INT = atof (VALUE_AS_STRING.c_str()); cout << "The numeric value of the string is: " << VALUE_AS_INT; ...but I am not able to do the opposite, eg convert an int into a string. Any help please? Second question : How come the condition below results in an error? How should it rather be written? char CHARACTER = "1"; if (CHARACTER == "1") {cout << "True";} Many thanks

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        J_E_D_I wrote:

        but I am not able to do the opposite, eg convert an int into a string. Any help please?

        Another method in addition to Maxwell's sprintf()...

        #include <sstream>
        ...
        int SomeInt = 5;

        std::ostringstream NumericStringStream;

        NumericStringStream << SomeInt;

        std::string NumericString = NumericStringStream.str();

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        1 Reply Last reply
        0
        • J J_E_D_I

          I have two questions. The first one is regarding the conversion from int to string. I can successfully convert a numeric string to int with the follwing code (particularly useful if you need to make mathematical operations on the numeric values contained in a string): string VALUE_AS_STRING = "1234"; int VALUE_AS_INT = atof (VALUE_AS_STRING.c_str()); cout << "The numeric value of the string is: " << VALUE_AS_INT; ...but I am not able to do the opposite, eg convert an int into a string. Any help please? Second question : How come the condition below results in an error? How should it rather be written? char CHARACTER = "1"; if (CHARACTER == "1") {cout << "True";} Many thanks

          J Offline
          J Offline
          Joe Woodbury
          wrote on last edited by
          #4

          itoa

          Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

          1 Reply Last reply
          0
          • J J_E_D_I

            I have two questions. The first one is regarding the conversion from int to string. I can successfully convert a numeric string to int with the follwing code (particularly useful if you need to make mathematical operations on the numeric values contained in a string): string VALUE_AS_STRING = "1234"; int VALUE_AS_INT = atof (VALUE_AS_STRING.c_str()); cout << "The numeric value of the string is: " << VALUE_AS_INT; ...but I am not able to do the opposite, eg convert an int into a string. Any help please? Second question : How come the condition below results in an error? How should it rather be written? char CHARACTER = "1"; if (CHARACTER == "1") {cout << "True";} Many thanks

            T Offline
            T Offline
            ThatsAlok
            wrote on last edited by
            #5

            For Question One :- would wsprintf or CString::format is of any use!

            J_E_D_I wrote:

            Second question : How come the condition below results in an error? How should it rather be written? char CHARACTER = "1"; if (CHARACTER == "1") {cout << "True";}

            use ' instead of "

            "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
            Never mind - my own stupidity is the source of every "problem" - Mixture

            cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You/codeProject$$>

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups