fail to count digit of an integer (fail at 9)

This is a discussion on fail to count digit of an integer (fail at 9) within the C++ Programming forums, part of the General Programming Boards category; hi guys I"m new to C++. I'm actually physics major and conducting self-study for visual C++. Any help will be ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    43

    fail to count digit of an integer (fail at 9)

    hi guys
    I"m new to C++. I'm actually physics major and conducting self-study for visual C++.
    Any help will be greatly appreciated

    I came across a problem of counting how many digits are there in an input integer.
    My code is
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    	int number;
    	int digit=0;
    
    	cout << "Enter an integer number:";
    	cin >> number;
    	cout << endl;
    	while (number)
    	{
    		number /= 10;
    		digit++;
    	}
    	cout << "Number of digit: " << digit << endl;
    	return 0;
    }
    It worked well, but as I typed in more-than-9-digit number, the output was just 9
    for example:
    Input: 1234567890123
    output: 9 (supposed to be more than that!)

    I kept checking but didn't get any answer!
    Can you please help?
    thanks!!

  2. #2
    Banned
    Join Date
    Nov 2007
    Posts
    678
    just put this statement after reading the number:
    Code:
    cout << "You entered: " << number << endl;
    and you will get to know yourself!

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What manav is hinting at is that integers have a limited range, usually a bit more than 2000000000. So if you enter a much larger number, either the whole number won't be accepted, or the number will drop the upper part (and this gets complicated, because it relies on the binary formatting of the number, so it's not easy to say from the decimal input what the number will actually be). By doing what manav says and output what was input, you can see if the number is accepted in full or not.

    I beleive one of the error bits gets set in the input stream when a number is overflowing (too big to be accepted), but I'm not 100% sure.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    43
    thanks guys!!
    that's really helpful!

    you guys reminded that int has certain range and I went over it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error msg instead of the result so far
    By maybabier in forum C Programming
    Replies: 25
    Last Post: 03-20-2009, 03:45 PM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 11:28 PM
  4. Count Number of Digits in and Integer
    By redneon in forum C++ Programming
    Replies: 2
    Last Post: 08-18-2003, 05:16 PM
  5. Range of an integer.
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 03-10-2002, 09:43 AM

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