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

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by 8.brahim
    yes i know but he was talking about syntax errors
    No, zacs7 just noted that it is wrong, and it is indeed is wrong, even though it is syntactically correct.

    Quote Originally Posted by 8.brahim
    i mean try running it
    I can get your program to crash if I wanted to... again due to the buffer overflow problem that zacs7 mentioned.

    If you wanted to describe your suggested algorithm without providing a correct C program as an example, then use pseudocode, or describe your algorithm in prose (or use a flowchart if you can be bothered to do so).
    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

  2. #17
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    Quote Originally Posted by laserlight View Post
    No, zacs7 just noted that it is wrong, and it is indeed is wrong, even though it is syntactically correct.


    I can get your program to crash if I wanted to... again due to the buffer overflow problem that zacs7 mentioned.

    If you wanted to describe your suggested algorithm without providing a correct C program as an example, then use pseudocode, or describe your algorithm in prose (or use a flowchart if you can be bothered to do so).
    Hey if you want to ! is that a joke ?! becuz its not funny
    I ran the program with a number (string) of 18 digits and it works fine (limited by the int data type )at last I think you didn't understand the code
    by the way alot of books start i=0
    then for (i==0)
    so im not taking you as C role models or sth
    you cant understand the code thats it

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by 8.brahim View Post
    I ran the program with a number (string) of 18 digits and it works fine (limited by the int data type )
    How did you get 18 characters to fit into a 10-character array?

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by 8.brahim
    Hey if you want to ! is that a joke ?! becuz its not funny
    I ran the program with a number (string) of 18 digits and it works fine (limited by the int data type )at last I think you didn't understand the code
    It is not a joke. You do not understand your own code, and believe that it is correct because you have not discovered any bugs. Unless they are absolutely exhaustive, tests cannot prove the absence of bugs. Look at this line that you wrote:
    Code:
    scanf("%s", isbn);
    isbn is an array of 10 char. But due to the %s format specifier, one can read into isbn more characters than isbn can hold.

    Quote Originally Posted by 8.brahim
    by the way alot of books start i=0
    then for (i==0)
    If you are talking about examples of for loops, then you are mistaken. A canonical example would be:
    Code:
    for (i = 0; i < n; ++i)
    If you read a book where it reads:
    Code:
    for (i == 0; i < n; ++i)
    then that is a typographical error. As you noted, this does not matter in your code because you initialise i to 0 much earlier. Hence, it only matters in that it becomes a useless expression that can confuse the reader.
    Last edited by laserlight; 10-07-2009 at 02:42 PM.
    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

  5. #20
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Whoops, next time I'll just keep my mouth closed .

    Although the results are undefined, this will demonstrate your "buffer overrun" and why it's dangerous. Ask yourself how did the value of x change if I didn't explicitly refer to it? And to top it off, it's const! It's unlucky that it's writing to memory allocated to my process, otherwise it should crash.
    Code:
    [09:52:07] zac@neux:code (0) $ cat bo.c
    #include <stdio.h>
    
    int main(void)
    {
       char buf[3];
       const char x = 'a';
    
       printf("x = %c\n", x);
    
       scanf("%s", buf);
    
       printf("x = %c\n", x);
    
       return 0;
    }
    
    [09:52:24] zac@neux:code (0) $ gcc -o bo bo.c
    [09:52:25] zac@neux:code (0) $ ./bo 
    x = a
    abcd
    x = d

  6. #21
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    Ok, my understanding of this has been greatly improved, thank you very much.

    Unfortunately the program itself has changed little.

    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_string[10];
        int a, b, c, x, y, z, isbn_dec[10];
    
        for (a=0; a < 10; a++)
        {
        isbn_dec[a]=a;
        printf("%d\n",isbn_dec[a]);
        }
        {
            isbn_dec[a]=a;
        } 
            printf("\nPlease enter ISBN: ");
            scanf("%s",isbn_string);
            printf("\nisbn is now the string: %s",isbn_string);
            
            /* 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*/
      
                for (b=0; b<10; ++b)
            {   
                for (a=0; a<10; ++a)
                for (y=1; y<=10; ++y)
    
               /* string[b] should start as string[0], which is should be the first
               char in the string.*/
               
                isbn_string[b]= 0 - isbn_dec[a]; 
                isbn_dec[a] = y;  
    
              /*  on first loop, isbn_dec[0] is assigned to 1*/
    
                z = isbn_string[b] * isbn_dec[a] ;   
                printf("\n%d", z);
            }
        
        getch();
        return 0;
    }
    I'm still getting strange numbers back at the end. I think it has something to do with the way I'm handling the string and the string conversion. I'm still new to handling strings. Do i need a "*" in front of it or something here? I'm pretty stumped.

  7. #22
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    One of the values of indentation is that we can see what you believe your program to be doing, even if it does not match reality. Unfortunately, this:
    Code:
                for (b=0; b<10; ++b)
            {   
                for (a=0; a<10; ++a)
                for (y=1; y<=10; ++y)
    
               /* string[b] should start as string[0], which is should be the first
               char in the string.*/
               
                isbn_string[b]= 0 - isbn_dec[a]; 
                isbn_dec[a] = y;  
    
              /*  on first loop, isbn_dec[0] is assigned to 1*/
    
                z = isbn_string[b] * isbn_dec[a] ;   
                printf("\n%d", z);
            }
    doesn't give us much help. If you allow your editor/IDE to indent it, you should come up with something like this:
    Code:
    for (b=0; b<10; ++b)
    {   
        for (a=0; a<10; ++a)
            for (y=1; y<=10; ++y)
    
                /* string[b] should start as string[0], which is should be the first
                char in the string.*/
               
                isbn_string[b]= 0 - isbn_dec[a]; 
        isbn_dec[a] = y;  
    
        /*  on first loop, isbn_dec[0] is assigned to 1*/
    
        z = isbn_string[b] * isbn_dec[a] ;   
        printf("\n%d", z);
    }
    which may give you some insight. (For instance, notice that 2/3 of your usage of the letter a is invalid.) Also I don't know what you intend by this line:
    Code:
    isbn_string[b]= 0 - isbn_dec[a];
    but whatever it is, you're not doing it.

  8. #23
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    Thanks for the indentation tip, I haven't gotten to the "Indentation" chapter of the tutorial sets yet (and there actually is one, so I can see its importance). This is only my 4th or 5th program. I knew indenting was important, but wasn't sure on exactly how the style should look. So, thanks for the example.

    Let me be more clear. I'm really confused about how to get each character is a string, say: "12345" to be assigned to to an integer array. Some on this forum hinted that doing this will involve subracting zero.

    Code:
     isbn_string[b]= 0 - isbn_dec[a];
    This was one of the many pathetic trial and error attempts I made before realizing I must be missing some key information and knowledge here and came back to the forum. The above code is simply a relic of my experiments (which has been extremely helpful, if odd looking to a veteran)

    The entire last section of the program are my attempts to get each character of the string, into its own int, then multiply each char by its placement number in the string. i.e. string: 5467, will have 5*1, 4*2, 6*3 etc.

    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_string[10];
        int a, b, c, x, y, z, isbn_dec[10];
    
        for (a=0; a < 10; a++)
        {
            isbn_dec[a]=a;
            printf("%d\n",isbn_dec[a]);
        }
    
        printf("\nPlease enter ISBN: ");
        scanf("%s",isbn_string);
        printf("\nisbn is now the string: %s",isbn_string);
            
        /* 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*/
        
        for (b=0; b<10; ++b)
        {   
            for (a=0; a<10; ++a)
                for (y=1; y<=10; ++y)
                
                    /* Code below will not attempt to attach each string char to 
                    an array of ints*/
               
                    isbn_string[b]= 0 - isbn_dec[a]; 
                    isbn_dec[a] = y;  
                
                    /* Code below attemps to multiply the string char, now turned int
                    by the its placement number in the string, then printf the result.
                    i.e. string: 5467, will have 5*1, 4*2, 6*3 etc */
                
                    z = isbn_string[b] * isbn_dec[a] ;   
                    printf("\n%d", z);
        }
        
        getch();
        return 0;
    }
    Thank you for you patience. I know from teaching other subjects that a noob's ignorance and lack of knowledge can make you want to pull your hair out.

  9. #24
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You should read around in the forums a bit -- if so, you'll have seen this
    Code:
    value_of_char = digit - '0';
    which does indeed look like subtracting zero.

  10. #25
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    Wow, I think I actually got it now. I thought it would take me much longer to understand all this, let alone get the code right.

    Code:
    #include<stdio.h>
    #include <windows.h>
    
    int main()
    #define MAX 10
    
         
    {
        char isbn_string[10];
        int a, b, x[10], y, z, isbn_dec[10];
    
        printf("\nPlease enter ISBN: ");
        scanf("%s",isbn_string);
        printf("\nisbn is now the string: %s",isbn_string);
            
        /* 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*/
        
        for (b=0, y=1; b<9, y<=9; ++b,++y)
        {   
            /* the string char is assigned to an array int*/
            
            isbn_dec[b]= isbn_string[b] - '0'; 
            
            /* the new int is multiplied by y.  y represents the int's 
            digit placement*/
            
            x[b] = isbn_dec[b] * y;  
            printf("\n%d", x[b]);         
        }
        
        z = x[0] + x[1] + x[2] + x[3] + x[4] + x[5] + x[6] + x[7] + x[8];
        printf("\nSum of the first 9 digits: %d", z); 
        
        a = z%11;
        printf("\nRemainder of %d/11 = %d", z, a);
        
        if(a = isbn_dec[10]);
        {
             printf("\nYes, the ISBN is legit!");
        }
             
        getch();
        return 0;
    }
    Simplification of the code, especially in the loop helped big time.

    I still have a question though.

    Code:
    isbn_dec[b]= isbn_string[b] - '0';
    All my online searches in this yield info about both '0' and '/0'. As i understand it, '\0' is a character constant and called nul, and '0' is an integer constant. This sites FAQ is clear about this. And it seems C uses null termination strings, and '/0' signifies the end of a string.

    I am confused as to why it subtracting '0' was necessary in the above code though. When I remove that part, the program doesn't work. I don't understand why.

  11. #26
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    '\0' is the end-of-string marker. '0' is just the character 0. You should look up ASCII.

  12. #27
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Say I have a char, and that char is '1'.

    Now I want to change that char, into a number. The ascii number for '1' is 49, so that's not what I want.

    But all the ascii numbers (and letters), run in a continuous manner: 0 is 48, 1 is 49, etc.

    So, if I subtract 48, or '0', from any char which is a digit, I will have changed it into a number - presto, chango!

    0, '0' and '\0' have nothing to do with one another, conceptually. First one is a number, second one is a char, and third one is the end of string char.

  13. #28
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    ASCII MAGIC

    learn to make your code look clean and smooth .. try smoking a blunt and being extra carefull with those identions
    "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 ???

  14. #29
    Registered User
    Join Date
    Oct 2009
    Posts
    1

    surfing kites

    Nice! bookmarked it! i will check it later. Thanks very much for sharing
    __________
    kite sale
    kite lesson

  15. #30
    Registered User
    Join Date
    Oct 2009
    Posts
    1
    I found this ..and i used this code to split and convert decimal to hex(change 10->16)

    Code:
    #include<stdio.h>
    void main()
    {
      int n  , i ;
      int stk[15] , top = 0;
      printf("Enter the number :");
      scanf("%d" , &n);
      while( n > 0 )
      {
            i = n % 10;
            stk[++top] = i;
            n = n / 10;
    
      }
      while(top)printf("%d " , stk[top--]);
    }

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