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. Integers and Char Arrays!

Integers and Char Arrays!

Scheduled Pinned Locked Moved C / C++ / MFC
helpdata-structurestutorialquestion
5 Posts 4 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.
  • M Offline
    M Offline
    Michael101
    wrote on last edited by
    #1

    Hi everyone, I'm working on a solution and I've come accross a problem which seems to be trickier than I expected! I've got an Integer and I need to put the numbers from that Integer into a character array! This array is used for other operations but I don't know how to extract the numbers in the Integer and place them in their own single element in the array. E.g. 47 needs to be over two diffent elements and so on. Any ideas? Thanks in advance, I appreciate the help :-) Michael

    N T J 3 Replies Last reply
    0
    • M Michael101

      Hi everyone, I'm working on a solution and I've come accross a problem which seems to be trickier than I expected! I've got an Integer and I need to put the numbers from that Integer into a character array! This array is used for other operations but I don't know how to extract the numbers in the Integer and place them in their own single element in the array. E.g. 47 needs to be over two diffent elements and so on. Any ideas? Thanks in advance, I appreciate the help :-) Michael

      N Offline
      N Offline
      Naveen
      wrote on last edited by
      #2

      check the sprintf() function. eg: int n = 123456; char c[33] = {0}; sprintf( c, "%d", n );

      nave [OpenedFileFinder]

      M 1 Reply Last reply
      0
      • N Naveen

        check the sprintf() function. eg: int n = 123456; char c[33] = {0}; sprintf( c, "%d", n );

        nave [OpenedFileFinder]

        M Offline
        M Offline
        Michael101
        wrote on last edited by
        #3

        Legend :-) Worked like a charm! Thanks

        1 Reply Last reply
        0
        • M Michael101

          Hi everyone, I'm working on a solution and I've come accross a problem which seems to be trickier than I expected! I've got an Integer and I need to put the numbers from that Integer into a character array! This array is used for other operations but I don't know how to extract the numbers in the Integer and place them in their own single element in the array. E.g. 47 needs to be over two diffent elements and so on. Any ideas? Thanks in advance, I appreciate the help :-) Michael

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

          you can also use itoa, _itot function!

          "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

          1 Reply Last reply
          0
          • M Michael101

            Hi everyone, I'm working on a solution and I've come accross a problem which seems to be trickier than I expected! I've got an Integer and I need to put the numbers from that Integer into a character array! This array is used for other operations but I don't know how to extract the numbers in the Integer and place them in their own single element in the array. E.g. 47 needs to be over two diffent elements and so on. Any ideas? Thanks in advance, I appreciate the help :-) Michael

            J Offline
            J Offline
            jhwurmbach
            wrote on last edited by
            #5

            You can use

            #include const int number = 47;

            std::stringstream stream;
            stream << number;

            std::string numberAsString = stream.str();

            This way, you are using the power of the STL for you, and are avoiding literally thousands of error possibilities when fiddeling around with char-pointers and string lenghts. The std::string would fit in nicely with the std::vector (or any oher standard container), and as a plus you can get the content as a plain old C-String by calling its c_str()-member. Just to bring regular C++ to the mind, instead of this ugly 30+years old C code floating around here.


            Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
            George Orwell, "Keep the Aspidistra Flying", Opening words

            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