C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 16th, 2013, 05:50 PM
Garrett85 Garrett85 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 21 Garrett85 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 42 m 15 sec
Reputation Power: 0
C++, getting the number of digits in an int

In the followig program I need to get the number of digits in an int value that will be different lengths depending on user input.

Code:
#include <iostream>

int main()
{
    int NumberOfGroups = 0;
    std::cout << "Please enter a date for validation, numbers only without dashes or slashes.";
    int NUM = 0;
    std::cin >> NUM;
    digits = //get number of digits in NUM

    if (NUM <= 299 )
    {
        int NumberOfGroups++;
        std::cout << "Num is less than 300" << std::endl;
    }
    else if ( digits % 3 == .33 )
    {
        // add two zeros to the left of NUM
    }
    else if ( digits % 3 == .66 )
    {
        // add one zero to the left of NUM
    }
    else
        NUM = NUM;


return 0;
}

Reply With Quote
  #2  
Old January 16th, 2013, 08:07 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,348 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 35 m 46 sec
Reputation Power: 383
The number of digits is
1 + (int)log10(NUM)


Your program uses integers.
int % int always results in int, unless perhaps there's a 0 divisor. The tests against .33 and .66 aren't helpful, in fact when you learn about floating point representation you'll realize that tests against .33 and .66 will have been used almost never in all the successful programs that have ever been written. Maybe these appear more often than I think in the decimal arithmetic of a common business oriented language.




SPLAT!

c.c: In function ‘main’:
c.c:4:18: warning: division by zero [-Wdiv-by-zero]
/bin/bash: line 1: 5089 Floating point exception(core dumped) $a
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #3  
Old January 18th, 2013, 12:41 PM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,804 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 16 h 42 m 47 sec
Reputation Power: 1800
Like this:

Code:
#include <<math.h>
int intDigits( int x )
{
    return (int)log10( abs(x) ) + 1 ;
}


Note it does not account for a possible - sign for negative inputs, if you need that add 1 if < 0.

An alternative non-mathematical but generally less efficient approach:
Code:
#include <stdio.h>
int intDigits( int x )
{
    char numstr[23] ;  // big enough for 64 bit int with sign
    return sprintf( numstr, "%d", x ) ;
}

In this second example, the - sign is included for negative values, so if this is not wanted subtract 1 if < 0.

Reply With Quote
  #4  
Old January 18th, 2013, 12:58 PM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,804 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 16 h 42 m 47 sec
Reputation Power: 1800
Using one of the solutions I suggested, this will do what I think you are trying to do far more succinctly:

Code:
    if (NUM < 300 )
    {
        NumberOfGroups++;
        std::cout << "Num is less than 300" << std::endl;
    }
    else 
    {
        // Pad digits to multiple of three
        int digits = intDigits( NUM ) ;
        int width = digits + (3 - digits % 3) ;
        cout << setfill ('0') << setw(width) << NUM ;
    }


On another note, it is not very clear how you want the user to enter a value, and your validation is minimal. Date formats are cultural and locale specific; the date 18 January 2013 for example might be expressed:

180113 // usual UK order ddmmyy
011813 // usual US order mmddyy
18012013 // UK ddmmyyyy
01182013 // US ddmmyyyy
20130118 // ISO 8601 order yyyymmdd

Given that a user might respond to your prompt in any of the above ways, and possibly more, it is not at all clear what you expect.

Last edited by clifford : January 18th, 2013 at 01:31 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > C++, getting the number of digits in an int

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap