Thread: Can a scanf function see a number input "1234" as 4 separate numbers?

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    13

    Can a scanf function see a number input "1234" as 4 separate numbers?

    I am doing an ISBN digit check program. I'm not actually in school, but it's on the curriculum in a lot of C classes. Naturally want to be able to do it.

    I am trying to figure it all out, having tried twice and failed. My biggest hurdle/question is:

    Can you have a "scanf" function that sees the user input/ISBN like this: 1234567890.

    With no spaces or dashes, and have the program not treat the ISBN input as one number? Have the program understand that it should take each digit and place each one in a slot on an array? So, using the above number, it would get: isbn[0] = 1, isbn[1] = 2, etc.

    I have no program to show because I have scrapped them all, seeing as how i was going about it in completely the wrong way. I want to start fresh with new knowledge.
    Last edited by Krash005; 10-06-2009 at 11:29 AM. Reason: Prematurely hit enter.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Read it as a string instead. It's not a number in the sense that you wouldn't do arithmetic with an ISBN. What would that mean? Not all numbers are numeric data, some are only IDs.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    I was under the impression that you would need to take those ISBN numbers, and multiply them with the their placement number, then add up all of those numbers. Is that still possible if it reads as a string?

    Placement number as in would multiply like this: ISBN 7364956740, 1x7, 2x3, 3x6, 4x4, 5x9, etc.
    Last edited by Krash005; 10-06-2009 at 11:41 AM.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    See even if you talk about doing math with an ISBN it's meaningless.

    Read it as a string. Then, copy the string to an array of ints one character at a time.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Provide the width to the control string format specifier ie "%1d" which tells scanf() to pick only one digit at a time from the input.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The check digit of an ISBN can also be X.

    I suppose it would also be nice to be able to handle separators like spaces or hyphens, since ISBNs normally come grouped.

    Hint: to convert chars '0'-'9' into numbers 0-9, subtract '0'.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    Thanks for all your help so far. Here's what I have. Don't mind the printf checks. As you can see I am really new at this, and I need to see that even easy things like scans worked, by printing them.

    Code:
    #include<stdio.h>
    #include <windows.h>
    
    int main()
    #define MAX 10
    
    {
         /*  fill the first aray with their space numbers, 
         then print it to make sure*/
        char  isbn[10];
        int a, b, c, space[10];
        
        for (a=1; a <= 10; a++)
        {
            space[a]=a;
            printf("%d\n",space[a]);
        }
                 /*  Enter isbn */
            printf("\nPlease enter ISBN: ");
            scanf("%s",isbn);
            printf("\nChecking that the scanf string worked: %s",isbn);
            
            /* So, right now, isbn is not an array of ints yet, its a
            string.  It needs to be copied 1 by 1 into an array of ints*/
            
            "%1d", b=isbn;
            
            for (b=0; b<10; ++b)
               {
               printf("\n%d", isbn[0]);    
              }
     
        getch();
        return 0;
    }
    This is where I am stuck. I want to have something like ""%1d", isbn[b],=isbn", thinking that it would assign the first digit of the isbn that the user typed in. And convert it to b, which was declared as a int, therefore making the isbn digit an integer. I wasn't worried about replicating this for the otehr digits, I just wanted to see if this would work.

    Problem is I don't know if the "%1d" I have is having that effect. When I print I get an array of the same number, usually 48. So I'm afraid I'm on the wrong track now.

  8. #8
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    string "12345" into arrayStr[5]

    now ..i want to convert this into decimal ..


    STRLEN arrayStr gives 5

    arrayInt[5] = arrayStr[5] - 48

    LOOP AGAIN

    arrayInt[4] = arrayStr[4] - 48

    LOOP AGAIN

    .....

    till

    array
    arrayInt[0] = arrayStr[0] - 48


    Now arrayInt[5] = 1,2,3,4,5

    I think thats the logic ..if not i must be higher than a kite
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  9. #9
    Registered User
    Join Date
    Oct 2009
    Posts
    7

    Simply the simplest solution

    First remember my friend its about algorithm and not the syntax of the language

    Here is a standalone program run it to understand what is my solution
    I printed every number-every entry of the array on a new line to prove my point
    you have to edit it to match your needs , but the key idea is here

    Code:
    #include<stdio.h>
    #include<string.h>
    void isbn();
    main()
    {
        isbn();
    }
    void isbn()
    {
        int length;
        int i=0;
        char isbn[10]={'\0'};
        int i_isbn[10];
        printf("Enter ISBN ");
        scanf("%s", isbn);
        length=strlen(isbn);
        for(i==0; i<length; i++)
        {
            i_isbn[i]=(((int)isbn[i])-48);
            printf("\n%d", i_isbn[i]);
        }
    }

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by 8.brahim View Post
    First remember my friend its about algorithm and not the syntax of the language

    Here is a standalone program run it to understand what is my solution
    I printed every number-every entry of the array on a new line to prove my point
    you have to edit it to match your needs , but the key idea is here

    Code:
    #include<stdio.h>
    #include<string.h>
    void isbn();
    main()
    {
        isbn();
    }
    void isbn()
    {
        int length;
        int i=0;
        char isbn[10]={'\0'};
        int i_isbn[10];
        printf("Enter ISBN ");
        scanf("%s", isbn);
        length=strlen(isbn);
        for(i==0; i<length; i++)
        {
            i_isbn[i]=(((int)isbn[i])-48);
            printf("\n%d", i_isbn[i]);
        }
    }
    No... it really isn't.
    • Don't use implicit main()
    • 48 is a rather magical number
    • You have a buffer overrun if the user enters more than 9 characters -- odd considering ISBNs are of length 10.
    • for(i == 0 ... is "wrong"


    Sure, there is a lazy way
    Code:
    [19:12:13] zac@neux:code (0) $ cat lazy.c 
    #include <stdio.h>
    
    int main(void)
    {
       char isbn[11] = {0};
    
       if(scanf("%10[0-9]", isbn) == 1)
          printf("isbn = %s\n", isbn);
       else
          printf("Invalid isbn\n");
    
       return 0;
    }
    But I wouldn't suggest doing it that way. But it does answer your original query.
    Last edited by zacs7; 10-07-2009 at 02:09 AM.

  11. #11
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    Quote Originally Posted by zacs7 View Post
    No... it really isn't.
    • Don't use implicit main()
    • 48 is a rather magical number
    • You have a buffer overrun if the user enters more than 9 characters -- odd considering ISBNs are of length 10.
    • for(i == 0 ... is "wrong"


    Sure, there is a lazy way
    Code:
    [19:12:13] zac@neux:code (0) $ cat lazy.c 
    #include <stdio.h>
    
    int main(void)
    {
       char isbn[11] = {0};
    
       if(scanf("%10[0-9]", isbn) == 1)
          printf("isbn = %s\n", isbn);
       else
          printf("Invalid isbn\n");
    
       return 0;
    }
    But I wouldn't suggest doing it that way. But it does answer your original query.
    hey many dont get that excited i builded and run it without a single error
    and he just want a key idea to transfer a string of numbers to an array of numers
    so let the one who asked decide what is the best for him
    whats wrong with i==0 !!!!!!!!!!!!!
    are you sure you are talking about C ?

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    i == 0 is comparing i with 0, as in:

    Code:
    if(i == 0)
      //do something
    What you want for assignment, is just one equal sign:

    Code:
    for(i = 0; i < someNumber; i++)
      //do something

  13. #13
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    Quote Originally Posted by Adak View Post
    i == 0 is comparing i with 0, as in:

    Code:
    if(i == 0)
      //do something
    What you want for assignment, is just one equal sign:

    Code:
    for(i = 0; i < someNumber; i++)
      //do something
    hey yes i know i want to compare it because i already initialized it (review the code)
    I told you i built the program and run it without an error why do you insist

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by 8.brahim
    hey yes i know i want to compare it because i already initialized it
    It does not make sense to compare it in that context because that expression has no net effect. In fact, it just confuses the reader.

    Quote Originally Posted by 8.brahim
    I told you i built the program and run it without an error why do you insist
    Because:
    Quote Originally Posted by Abelson and Sussman
    Programs must be written for people to read, and only incidentally for machines to execute.
    And of course just because your program runs without an error does not mean it is correct.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    Quote Originally Posted by laserlight View Post
    It does not make sense to compare it in that context because that expression has no net effect. In fact, it just confuses the reader.


    Because:

    And of course just because your program runs without an error does not mean it is correct.
    yes i know but he was talking about syntax errors and by the way your sense doesn't mean whats true and whats not
    i mean try running it
    it just put string of number into an array on numbers
    Last edited by 8.brahim; 10-07-2009 at 02:20 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 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. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM