length of input number

This is a discussion on length of input number within the C++ Programming forums, part of the General Programming Boards category; I made a loop to return the number of digit entered by a user, but the problem is that loop ...

  1. #1
    Alabsi
    Guest

    length of input number

    I made a loop to return the number of digit entered by a user, but the problem is that loop does not work with big numbers like number which is more than 10 digit, I also used the sizeof() function but it is the same. Is there any Idea how to make it read bigger numbers, Thanks and the loop is :

    Code:
    cout<<"Please enter the number :"<<endl; //getting input
        cin>>num;
    
        temp1=num;
    	count=0;
    
    	
    
        while (temp1!=0){                      //counting number of digits
           temp1=temp1/10;
           count=count+1;	     
           }

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    change the variable type of num. It's probably an int right now - try float or double. (these support decimal places as well)
    Away.

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Or 4294967295 in an unsigned int.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    Alabsi
    Guest

    unsigned long

    I really used unsigned long for num but the reasult is the same I'm not allowed to use string. Could any body tell me about the constant variable UMAX_LONG please.Thanks for the fast reply

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: unsigned long

    Originally posted by Alabsi
    I really used unsigned long for num but the reasult is the same I'm not allowed to use string.
    Use a char array.

    Code:
    char input[1024];
    cout <<"Enter number:" ;
    cin >>input;
    Or if you really want to do this with a numeric variable, you'll have to find one big enough. Check your compiler documentation.

    To use UMAX_LONG:

    cout <<UMAX_LONG <<endl;
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Alabsi
    Guest

    UMAX_LONG

    Do I have to declare the UMAX_LONG before I use it, or I have to store the num variable in it, could you tell me more about the UMAX_LONG and I do think I can use the char[] array because it is a string. My compiler is g++ for the information.Thanks.

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    70
    hey...Alabsi,

    Why don't you use itoa(), ltoa() functions to convert numeric values to character array and then get length of it instead of using loop for count number of digits ???
    Chintan R Naik

  8. #8
    Absi
    Guest
    I do not know wheather that involves the use of string library because if so I'm not allowed yet to use it thanks

  9. #9
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    How many digits are you expecting/expected to process? If it is more than the basic 32/64bit types we have already given you, you are going to need compound data types. That being the case, your statement...

    >>> I'm not allowed yet to use it

    ... [the string library] implies this a pretty basic level program we're talking about.

    Are you sure you're not trying to hard?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  10. #10
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    If your using a Windows 32 bit system, then chances are your compiler will support a 64-bit integer type (not a standard type though). Usually, the name is either 'long long' or '__int64'.

    A compound type is the best solution (as it would be portable, and predictable, and standard compliant), but this usually will work on a 32 bit system.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 03:13 PM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  3. how to check if input is only positive number?
    By hanek in forum C Programming
    Replies: 16
    Last Post: 04-26-2004, 11:57 AM
  4. Very odd segmentation fault/core dump.
    By ChristianTool in forum C Programming
    Replies: 19
    Last Post: 04-26-2004, 06:38 AM
  5. Replies: 4
    Last Post: 04-21-2004, 04:18 PM

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