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. Ok new Interger query!

Ok new Interger query!

Scheduled Pinned Locked Moved C / C++ / MFC
databasequestionlearning
9 Posts 7 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

    Ok if I have a number (stored in a regular integer) and say that number is 51.... Is there a function to search the Integer and return how many numbers there are in the Integer? e.g. 51 has a 5 and a 1 in it so it should return 2. I tried -> sprintf(IntLength, "%d", Prime); but I get compilation errors :-( does anyone have any ideas? Thanks of course in advance :-)

    A N T D D 5 Replies Last reply
    0
    • M Michael101

      Ok if I have a number (stored in a regular integer) and say that number is 51.... Is there a function to search the Integer and return how many numbers there are in the Integer? e.g. 51 has a 5 and a 1 in it so it should return 2. I tried -> sprintf(IntLength, "%d", Prime); but I get compilation errors :-( does anyone have any ideas? Thanks of course in advance :-)

      A Offline
      A Offline
      ashishbhatt 0
      wrote on last edited by
      #2

      Here sprintf() will convert your prime integer to the string representation. so it will not give you your answer that how many numbers in integer. And Regarding to your error , check the type of variable IntLength used in sprintf() function.

      R 1 Reply Last reply
      0
      • M Michael101

        Ok if I have a number (stored in a regular integer) and say that number is 51.... Is there a function to search the Integer and return how many numbers there are in the Integer? e.g. 51 has a 5 and a 1 in it so it should return 2. I tried -> sprintf(IntLength, "%d", Prime); but I get compilation errors :-( does anyone have any ideas? Thanks of course in advance :-)

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

        Michael101 wrote:

        sprintf(IntLength, "%d", Prime);

        Whats the error? any way you can use the following function for it int n = 12345678; int nCount = 1; while( n ) { n = n/10; if( n > 0 ) { nCount++; } }

        nave [OpenedFileFinder]

        T 1 Reply Last reply
        0
        • M Michael101

          Ok if I have a number (stored in a regular integer) and say that number is 51.... Is there a function to search the Integer and return how many numbers there are in the Integer? e.g. 51 has a 5 and a 1 in it so it should return 2. I tried -> sprintf(IntLength, "%d", Prime); but I get compilation errors :-( does anyone have any ideas? Thanks of course in advance :-)

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

          int iCount = 0; int iurNumber= any number; while(iurNumber !=0) { iurNumber /=10; iCount++; } iCount will contain number of element integer have

          "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
          • N Naveen

            Michael101 wrote:

            sprintf(IntLength, "%d", Prime);

            Whats the error? any way you can use the following function for it int n = 12345678; int nCount = 1; while( n ) { n = n/10; if( n > 0 ) { nCount++; } }

            nave [OpenedFileFinder]

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

            Naveen.R wrote:

            nave

            you are superfast dude!:)

            "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

            N 1 Reply Last reply
            0
            • T ThatsAlok

              Naveen.R wrote:

              nave

              you are superfast dude!:)

              "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

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

              :)

              nave [OpenedFileFinder]

              1 Reply Last reply
              0
              • A ashishbhatt 0

                Here sprintf() will convert your prime integer to the string representation. so it will not give you your answer that how many numbers in integer. And Regarding to your error , check the type of variable IntLength used in sprintf() function.

                R Offline
                R Offline
                Roger Broomfield
                wrote on last edited by
                #7

                technically you are not 100% correct. the return value of sprintf() is the number of characters stored in buffer so adapting the answer to the previous question Michael asked given by Naveen.R here[^] you could end up with int IntLength; int n = 123456; char c[33] = {0}; IntLength = sprintf( c, "%d", n );

                1 Reply Last reply
                0
                • M Michael101

                  Ok if I have a number (stored in a regular integer) and say that number is 51.... Is there a function to search the Integer and return how many numbers there are in the Integer? e.g. 51 has a 5 and a 1 in it so it should return 2. I tried -> sprintf(IntLength, "%d", Prime); but I get compilation errors :-( does anyone have any ideas? Thanks of course in advance :-)

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  Michael101 wrote:

                  ...but I get compilation errors...

                  Are we supposed to guess what those are?


                  "A good athlete is the result of a good and worthy opponent." - David Crow

                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                  1 Reply Last reply
                  0
                  • M Michael101

                    Ok if I have a number (stored in a regular integer) and say that number is 51.... Is there a function to search the Integer and return how many numbers there are in the Integer? e.g. 51 has a 5 and a 1 in it so it should return 2. I tried -> sprintf(IntLength, "%d", Prime); but I get compilation errors :-( does anyone have any ideas? Thanks of course in advance :-)

                    D Offline
                    D Offline
                    DQNOK
                    wrote on last edited by
                    #9

                    I've never seen sprintf with this signature. Have you overloaded it yourself? I think the error you are getting is because you are passing an integer instead of a char*. Instead of: sprintf(IntLength, "%d", Prime); use: IntLength = sprintf( someCharBuffer, "%d", Prime ); Or better yet, use one of the other solutions already given. David

                    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