Thread: a problem with the check digi in the isbn prog

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    10

    a problem with the check digi in the isbn prog

    this is the first problem set in cs50

    i think that ihave a problem with thefor loop
    plz help

    this is my code for isbn.c

    Code:
    #include <cs50.h>
    #include <stdio.h>
    
    int
     main( int argc , char *argv[])
     {
    
    printf("isbn-num :");
    long long isbnum = GetLongLong();
    int sum=0;
    int i;
    
    for ( i= 10;i>0;i--)
    
    {
    int ldigi = (isbnum % 10) ;
    
    isbnum = (isbnum-ldigi);
    
    printf("%d \n",isbnum );
    
    
    }
    
    }
    the code decremented by 9 ? when i give 5641615 the prog give me 5641606
    thanx
    Last edited by polypic; 03-16-2010 at 11:46 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you want to "peel off" digits, then replace the subtraction, with division by 10.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    10
    whati want to say here that
    when there isno for loop
    i inter
    :11111
    gives me
    11110
    okay

    put with the loop gives my some thing like this
    even if i`m peeling off digit`s with division by 10


    C:\esso>a.exe

    isbn-num :11111

    1717988029
    -257697927
    2121713855
    1500661574
    1438556346
    -715137825
    -501010513
    379395678
    37939567
    862787415







    why ?

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    10
    up
    up
    up
    up

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > int ldigi = (isbnum % 10) ;
    > isbnum = (isbnum-ldigi);
    So next time around, ldigi will be 0

    isbnum /= 10;
    is needed as well, if you want to peel each digit off in turn.


    > printf("%d \n",isbnum );
    It's the wrong format for printing a long long
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    10
    thanx salem
    okay
    nowi`m printing long long like this
    > printf("%lld \n",isbnum );
    but still thesame strange result

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Post ALL of your latest code, not one random line.

    Also, are you using the Mingw compiler underneath dev-c++ (and perhaps even code::blocks)?
    You might have to use the Microsoft format specifiers for long long
    Format Specification Fields: printf and wprintf Functions (CRT)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    10
    thanx
    here is the code

    Code:
    #include <cs50.h>
    #include <stdio.h>
    
    int
     main( int argc , char *argv[])
     {
    
    printf("isbn-num :");
    long long isbnum = GetLongLong();
    int sum=0;
    int i;
    
    for ( i= 10;i>0;i--)
    
    {
    int ldigi = (isbnum % 10) ;
    isbnum/=10 ;
    
    printf("%lld \n",isbnum );
    
    
    }
    
    }
    & yes i`m using Mingw
    i don`t know how to use Microsoft format specifiers for long long ?
    thanx

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    microsoft format would be %I64d
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Or %lld works too.

  11. #11
    Registered User
    Join Date
    Mar 2010
    Posts
    10
    plz help me with this
    i can`t get any much farther here is the code

    Code:
    #include <cs50.h>
    #include <stdio.h>
    
    int
     main( int argc , char *argv[])
     {
    
    printf("isbn-num :");
    long long isbnum = GetLongLong();
    int sum=0;
    int i;
    
    for ( i= 10;i>0;i--)
    
    {
    isbnum/=10 ;
    
    printf("%I64d \n",isbnum );
    
    
    }
    
    }

    & here is theouput


    C:\esso>gcc isbn.c -lcs50

    C:\esso>a
    isbn-num :454854854
    862789023206188871
    86278902320618887
    8627890232061888
    862789023206188
    86278902320618
    8627890232061
    862789023206
    86278902320
    8627890232
    862789023




    plz help

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If your for loop only runs 10 times, then you'll only get 10 lines of answers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Odd Problem with Calculator Prog.
    By dw3rby in forum C++ Programming
    Replies: 4
    Last Post: 02-22-2003, 10:25 PM
  2. isbn problem (barcodes on books)
    By djxtremor in forum C Programming
    Replies: 3
    Last Post: 10-16-2002, 09:34 PM
  3. problem with prog
    By katal in forum C Programming
    Replies: 5
    Last Post: 01-10-2002, 09:16 PM
  4. prog problem
    By katal in forum C Programming
    Replies: 5
    Last Post: 01-10-2002, 06:13 PM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM