String/Int Length Issue

This is a discussion on String/Int Length Issue within the C++ Programming forums, part of the General Programming Boards category; Hello, I have just recently (yesterday) began learning cpp. I have been going over the tutorials offered on this site ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    7

    Unhappy String/Int Length Issue

    Hello, I have just recently (yesterday) began learning cpp. I have been going over the tutorials offered on this site and I have stumbled across a small problem. I am attempting to create a tester program to see if I am grasping what I have learned so far. My problem occurs when I attempt to discover the length of an integer.

    I am aware of the strlen function that will return the length of a string. However I desire to know the length of variable that is an integer.

    So far I have tried making a string that variable but the compiler refuses on the grounds that it cannot accept a 'char string' is equal to an 'int variable'. Like so
    Code:
    ...
    char vse[15];
    int vx;
    ...
    vse=vx;
    ...
    I understand that it cannot be done this way.

    Then I reread typcasting, in hopes that I could find a way to play with the int/char types and be allowed to convert my int variable into a string and use strlen. Unfortuneatly I cannot figure out a feasible way to use typcasting to solve my problem.

    In short, I am looking for a way to know how many digits a number has.
    ie:
    1 -->1
    11 -->2
    111 --> 3
    1111 --> 4
    ...

    Is there a way to find this?

    Cheers,
    Smiley

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You could try logarithms, or you could just count how many times you can divide an integer by ten.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    You use stringstreams.
    Code:
    stringstream s;
    s << 1408; // or whatever number or variable
    s.str() now holds the std::string for you, which is what you should be using for text.
    To quote Zahlman (from another forum):
    As a general rule, if you post in For Beginners and your code contains the word 'char', you have a bug. std::string roxors teh big one one one one.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    7
    Ah of course! Thank you Anon, I can't beleive I did not think to count the number of times I could divide the number by 10. I created this litte function that has acheived my goal flawlessly, thank's again Anon.

    Code:
    int digam(int oi) {
      if (oi!=0) {
        int in = 0;
        while (oi>0) {
          in++;
          oi /= 10;
        }
        return in;
      }
      else {
        return 1;
        }
    }
    I though this the most logical approach to doing this. Although I suspect it might not be as optimised as it could be, it works nonetheless.

    iMalc, you also bring an answer to my question, and a very interesting one. I wonder, though, is 'stringstream' a variable type(like 'int' and 'char')? Also, when you call it s.str() is that what I should type if I want to return the value/word?

    I'm sorry for having several questions but this seems like a very interesting and useful method to learn, and I would much like to understand.

    Again I thank the both of you, you're help was greatly appreciated.

    Cheers,
    Smiley

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Yes stringstream is a type and so is string. They live in the std namespace.
    Here is a complete exmple that does the reverse, it converts numeric strings back into integers:
    stringstream - C++ Reference
    Unforunately I don't have time to give better examples right now.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The do...while loop allows you to write the function a bit more concisely:
    Code:
    unsigned digits(unsigned n, unsigned radix = 10) {
      unsigned d = 0;
      do {
        ++d;
        n /= radix;
      } while(n > 0);
      return d;
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Yeah it irks me too when someone uses a while loop for that. Even a 0 is one digit long so clearly a do .. while is appropriate.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Jul 2010
    Posts
    7
    Oh, thanks a lot iMalc, knowing this will help a great deal. I have read through the link you gave me a couple of times and it seems to be sinking in. I'll go over it a couple more times and play around with it to be fully comfortable with it. Thanks again.

    Cornedbee, thank you for a more concise version of the function. If I understand correctly, the do...while loop is more efficient than a while loop? Is this because the do..while executes the loop once and then checks to go on, instead of checking every-time before executing, as the while loop does?

    I apologies if this all sounds overly basic, but I like to be sure of my facts when I learn something. If the foundation is weak then the building cannot hope to be solid.

    Thanks again,
    Smiley

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by xSmiley View Post
    Cornedbee, thank you for a more concise version of the function. If I understand correctly, the do...while loop is more efficient than a while loop? Is this because the do..while executes the loop once and then checks to go on, instead of checking every-time before executing, as the while loop does?
    It might be more efficient in this particular case, but what's important is that the code has lower complexity. It's easier to read (if you know how do...while works, at least). Efficiency on this low level is irrelevant; the compiler will know what to do.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Jul 2010
    Posts
    7
    Ah, I admit it does allow for a better read. Thanks again for the help, I honestly appreciate your time.

    Cheers,
    Smiley

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Strange string behavior
    By jcafaro10 in forum C Programming
    Replies: 2
    Last Post: 04-07-2009, 07:38 PM
  3. Need help with a snake game (ncurses)
    By Adam4444 in forum C Programming
    Replies: 11
    Last Post: 01-17-2007, 02:41 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. Weird modification to string length
    By ChwanRen in forum C Programming
    Replies: 0
    Last Post: 08-17-2003, 10:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21