Thread: Simple Question(maybe)

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    3

    Simple Question(maybe)

    Im doing a small thing for school, and i want to make a program to have you enter a number, and have it count the digits, and then tell you how many digits there are.

    I only know a little C++, and would like to know how to go about haveing it count the digits. thanks for anyone who helps.

  2. #2
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Hmm you could either input the numbers in a string and find the strings length. Or you could do a little checking.

    like if its less than 10 then its 1 digit... less the 100 and greater than or equal to 10 then 2 digits.
    What is C++?

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    3
    The first idea is what i was thinking i believe, because if i did the second one, its almost an infinite list.

    Now how would i go about doing that. Quick example (anyone)?
    Last edited by CC4CocoaPuffs; 10-06-2004 at 05:19 PM.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Simple formula:
    # of base b digits in n = floor(log{b}(n)) + 1 (where log{b}(n) means log base b of n)
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    It can't be an infinite list because the variables are only able to hold a number so large... But the string route is a good approach. try sprintf, that'll get you what you want.

    PK

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    3
    thanks all.

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Using a for loop along with the / operator might be more appropriate for learning.

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    60
    If you include the header file <string>, you can make a string using the variable type std::string; and if you include the header file <cstring>, you can find the length of a string using the strlen(name_of_std::string_var) if that is helpful.....
    Child who knows C++
    Using Borland C/C++ Compiler 5.5 (Command Line Version)

  9. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    C++Child, you can find the length of a std::string using its length() or size() function, and you don't need <cstring> to do that.
    Code:
    std::string name_of_stdstring_var("fifteen letters");
    int fifteen = name_of_stdstring_var.length();
    The strlen function in <cstring> applies to C-style, null terminated character arrays, not std::string.

  10. #10
    Registered User
    Join Date
    Jul 2004
    Posts
    60
    Thank you jlou, I didn't know that. I usually append my strings with '\0' anyways.
    Child who knows C++
    Using Borland C/C++ Compiler 5.5 (Command Line Version)

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    STL strings are null terminated as well. Under the hood STL strings have the same null terminated char array as in a C style strings, they are just hidden behind an interface that makes them easiert to be manipulated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  4. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM
  5. Simple Compile Time Problem - HELP!
    By kamikazeecows in forum Windows Programming
    Replies: 2
    Last Post: 12-02-2001, 01:30 PM