Program stuck at scanf









up vote
1
down vote

favorite












so basically I'm trying to write a program as an exercise from my textbook where the user inputs a 12 digits number, and after a series of operations, a "check digits" gets printed out. Here is my code.



#include <stdio.h>

int main()
int one = 0, two = 0, three = 0, four = 0, five = 0, six = 0, seven = 0,
eight = 0, nine = 0, ten = 0, eleven = 0, twelve = 0;
int check_digit;

printf("Enter the first 12 digits of an EAN: ");
scanf("%d%d%d%d%d%d%d%d%d%d%d%d", &one, &two, &three, &four, &five, &six, &seven,
&eight, &nine, &ten, &eleven, &twelve);

check_digit = 9 - (((two + four + six + eight + ten + twelve)*3 +
(one + three + five + seven + nine + eleven))-1)%10;

printf("Check digit: %dnn", check_digit);

return 0;



The problem is that the program outputs absolutely nothing and it never seems to go past the scanf as I'm always able to add more digits even after inputting the 12 initial ones and hitting enter. I don't even get any warnings or errors whatsoever.
What am I doing wrong?










share|improve this question

















  • 3




    Try %1d or just read the input as a string with "%s". The code you have will only work if you put spaces between the digits.
    – user3386109
    Nov 10 at 1:14







  • 1




    ohh yes works with %1d... I see what I was doing wrong. Thanks!
    – FoxyIT
    Nov 10 at 1:20






  • 1




    OT: the scanf() family of functions returns a value. that value indicates how many of the input format specifiers where successfully input. The code should always check that value to assure the operation was successful
    – user3629249
    Nov 10 at 1:57






  • 1




    Note: check_digit = 9 - (((two + four + six + eight + ten + twelve)*3 + (one + three + five + seven + nine + eleven))-1)%10; has a corner problem when input is "000000000000", check_digit will take on a value of 10. To handle that and not mess up other % 10 calculation, simply replace - 1 with + 9.
    – chux
    Nov 10 at 2:34














up vote
1
down vote

favorite












so basically I'm trying to write a program as an exercise from my textbook where the user inputs a 12 digits number, and after a series of operations, a "check digits" gets printed out. Here is my code.



#include <stdio.h>

int main()
int one = 0, two = 0, three = 0, four = 0, five = 0, six = 0, seven = 0,
eight = 0, nine = 0, ten = 0, eleven = 0, twelve = 0;
int check_digit;

printf("Enter the first 12 digits of an EAN: ");
scanf("%d%d%d%d%d%d%d%d%d%d%d%d", &one, &two, &three, &four, &five, &six, &seven,
&eight, &nine, &ten, &eleven, &twelve);

check_digit = 9 - (((two + four + six + eight + ten + twelve)*3 +
(one + three + five + seven + nine + eleven))-1)%10;

printf("Check digit: %dnn", check_digit);

return 0;



The problem is that the program outputs absolutely nothing and it never seems to go past the scanf as I'm always able to add more digits even after inputting the 12 initial ones and hitting enter. I don't even get any warnings or errors whatsoever.
What am I doing wrong?










share|improve this question

















  • 3




    Try %1d or just read the input as a string with "%s". The code you have will only work if you put spaces between the digits.
    – user3386109
    Nov 10 at 1:14







  • 1




    ohh yes works with %1d... I see what I was doing wrong. Thanks!
    – FoxyIT
    Nov 10 at 1:20






  • 1




    OT: the scanf() family of functions returns a value. that value indicates how many of the input format specifiers where successfully input. The code should always check that value to assure the operation was successful
    – user3629249
    Nov 10 at 1:57






  • 1




    Note: check_digit = 9 - (((two + four + six + eight + ten + twelve)*3 + (one + three + five + seven + nine + eleven))-1)%10; has a corner problem when input is "000000000000", check_digit will take on a value of 10. To handle that and not mess up other % 10 calculation, simply replace - 1 with + 9.
    – chux
    Nov 10 at 2:34












up vote
1
down vote

favorite









up vote
1
down vote

favorite











so basically I'm trying to write a program as an exercise from my textbook where the user inputs a 12 digits number, and after a series of operations, a "check digits" gets printed out. Here is my code.



#include <stdio.h>

int main()
int one = 0, two = 0, three = 0, four = 0, five = 0, six = 0, seven = 0,
eight = 0, nine = 0, ten = 0, eleven = 0, twelve = 0;
int check_digit;

printf("Enter the first 12 digits of an EAN: ");
scanf("%d%d%d%d%d%d%d%d%d%d%d%d", &one, &two, &three, &four, &five, &six, &seven,
&eight, &nine, &ten, &eleven, &twelve);

check_digit = 9 - (((two + four + six + eight + ten + twelve)*3 +
(one + three + five + seven + nine + eleven))-1)%10;

printf("Check digit: %dnn", check_digit);

return 0;



The problem is that the program outputs absolutely nothing and it never seems to go past the scanf as I'm always able to add more digits even after inputting the 12 initial ones and hitting enter. I don't even get any warnings or errors whatsoever.
What am I doing wrong?










share|improve this question













so basically I'm trying to write a program as an exercise from my textbook where the user inputs a 12 digits number, and after a series of operations, a "check digits" gets printed out. Here is my code.



#include <stdio.h>

int main()
int one = 0, two = 0, three = 0, four = 0, five = 0, six = 0, seven = 0,
eight = 0, nine = 0, ten = 0, eleven = 0, twelve = 0;
int check_digit;

printf("Enter the first 12 digits of an EAN: ");
scanf("%d%d%d%d%d%d%d%d%d%d%d%d", &one, &two, &three, &four, &five, &six, &seven,
&eight, &nine, &ten, &eleven, &twelve);

check_digit = 9 - (((two + four + six + eight + ten + twelve)*3 +
(one + three + five + seven + nine + eleven))-1)%10;

printf("Check digit: %dnn", check_digit);

return 0;



The problem is that the program outputs absolutely nothing and it never seems to go past the scanf as I'm always able to add more digits even after inputting the 12 initial ones and hitting enter. I don't even get any warnings or errors whatsoever.
What am I doing wrong?







c






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 1:12









FoxyIT

517




517







  • 3




    Try %1d or just read the input as a string with "%s". The code you have will only work if you put spaces between the digits.
    – user3386109
    Nov 10 at 1:14







  • 1




    ohh yes works with %1d... I see what I was doing wrong. Thanks!
    – FoxyIT
    Nov 10 at 1:20






  • 1




    OT: the scanf() family of functions returns a value. that value indicates how many of the input format specifiers where successfully input. The code should always check that value to assure the operation was successful
    – user3629249
    Nov 10 at 1:57






  • 1




    Note: check_digit = 9 - (((two + four + six + eight + ten + twelve)*3 + (one + three + five + seven + nine + eleven))-1)%10; has a corner problem when input is "000000000000", check_digit will take on a value of 10. To handle that and not mess up other % 10 calculation, simply replace - 1 with + 9.
    – chux
    Nov 10 at 2:34












  • 3




    Try %1d or just read the input as a string with "%s". The code you have will only work if you put spaces between the digits.
    – user3386109
    Nov 10 at 1:14







  • 1




    ohh yes works with %1d... I see what I was doing wrong. Thanks!
    – FoxyIT
    Nov 10 at 1:20






  • 1




    OT: the scanf() family of functions returns a value. that value indicates how many of the input format specifiers where successfully input. The code should always check that value to assure the operation was successful
    – user3629249
    Nov 10 at 1:57






  • 1




    Note: check_digit = 9 - (((two + four + six + eight + ten + twelve)*3 + (one + three + five + seven + nine + eleven))-1)%10; has a corner problem when input is "000000000000", check_digit will take on a value of 10. To handle that and not mess up other % 10 calculation, simply replace - 1 with + 9.
    – chux
    Nov 10 at 2:34







3




3




Try %1d or just read the input as a string with "%s". The code you have will only work if you put spaces between the digits.
– user3386109
Nov 10 at 1:14





Try %1d or just read the input as a string with "%s". The code you have will only work if you put spaces between the digits.
– user3386109
Nov 10 at 1:14





1




1




ohh yes works with %1d... I see what I was doing wrong. Thanks!
– FoxyIT
Nov 10 at 1:20




ohh yes works with %1d... I see what I was doing wrong. Thanks!
– FoxyIT
Nov 10 at 1:20




1




1




OT: the scanf() family of functions returns a value. that value indicates how many of the input format specifiers where successfully input. The code should always check that value to assure the operation was successful
– user3629249
Nov 10 at 1:57




OT: the scanf() family of functions returns a value. that value indicates how many of the input format specifiers where successfully input. The code should always check that value to assure the operation was successful
– user3629249
Nov 10 at 1:57




1




1




Note: check_digit = 9 - (((two + four + six + eight + ten + twelve)*3 + (one + three + five + seven + nine + eleven))-1)%10; has a corner problem when input is "000000000000", check_digit will take on a value of 10. To handle that and not mess up other % 10 calculation, simply replace - 1 with + 9.
– chux
Nov 10 at 2:34




Note: check_digit = 9 - (((two + four + six + eight + ten + twelve)*3 + (one + three + five + seven + nine + eleven))-1)%10; has a corner problem when input is "000000000000", check_digit will take on a value of 10. To handle that and not mess up other % 10 calculation, simply replace - 1 with + 9.
– chux
Nov 10 at 2:34












2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










Since your scanf is using %d%d%d%d%d%d%d%d%d%d%d%d. It is expecting 12 numbers. So if you typed in 123456789012, the program scan in just 1 number. It reads 123456789012 not how you want it where one = 1, then two = 2 etc.



You can solve this by changing your scanf to:



scanf("%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d", &one, &two, &three, &four, &five, &six, &seven, &eight, &nine, &ten, &eleven, &twelve);



%1d will read a single digit. So now it will do what you want it to, where one = 1, then two = 2 etc.



Another way to solve this would be reading the number as a string. The convert the string into an int array. I've provided the code for how to do that below.



#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 12

// Function prototypes
void str_arr_to_int_arr(char str, int num, int len);
int check_digit(int EAN, int len);

int main(void)

char EAN_str[MAX_SIZE + 1];
int EAN_int[MAX_SIZE + 1];

printf("Enter the first 12 digits of an EAN: ");
scanf("%12s", EAN_str);

str_arr_to_int_arr(EAN_str, EAN_int, MAX_SIZE);

int answer = check_digit(EAN_int, MAX_SIZE);

printf("Check digit: %dn", answer);

return 0;


// This function will convert a char array into an int array
void str_arr_to_int_arr(char str, int num, int len)

int i = 0;
while (i < len)

if (str[i] >= '0' && str[i] <= '9')
num[i] = str[i] - '0';
else
printf("ERROR: You entered a non-numbern");
exit(1);


i++;




// Does the check_digit formula on an EAN
int check_digit(int EAN, int len)

int sum_pos = 0;
int sum_neg = 0;

int i = 0;
while (i < len)

if (i % 2 == 0)
sum_pos += EAN[i];
else
sum_neg += EAN[i];


i++;


return (9 - (((sum_pos * 3) + sum_neg) - 1) % 10);






share|improve this answer






















  • check_digit(EAN_int, MAX_SIZE); will always trigger printf("ERROR: You entered a non-numbern"); and exit as it tests the null character.
    – chux
    Nov 10 at 16:18











  • check_digit() is checking 13 values unlike OP's 12. Better to make use #define MAX_SIZE 12, char EAN_str[MAX_SIZE + 1]; int EAN_int[MAX_SIZE];
    – chux
    Nov 10 at 16:19


















up vote
2
down vote














What am I doing wrong?




OP wants to read a 12 digit code, yet scanf("%d%d%d%d%d%d%d%d%d%d%d%d"... looks for 12 separate int.



@user3386109 well suggested using "%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d" to limit the length of each int to 1 digit.




Alternative:



As EAN can vary in length up to 18 digits, consider



#define EAN_N 18
char buf[2*EAN_N]; // extra for n, and other other characters.
if (fgets(buf, sizof buf, stdin))
int n = 0;
while (buf[n] >= '0' && buf[n] <= '9') n++;
if (buf[n] == 'n'





share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235182%2fprogram-stuck-at-scanf%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    Since your scanf is using %d%d%d%d%d%d%d%d%d%d%d%d. It is expecting 12 numbers. So if you typed in 123456789012, the program scan in just 1 number. It reads 123456789012 not how you want it where one = 1, then two = 2 etc.



    You can solve this by changing your scanf to:



    scanf("%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d", &one, &two, &three, &four, &five, &six, &seven, &eight, &nine, &ten, &eleven, &twelve);



    %1d will read a single digit. So now it will do what you want it to, where one = 1, then two = 2 etc.



    Another way to solve this would be reading the number as a string. The convert the string into an int array. I've provided the code for how to do that below.



    #include <stdio.h>
    #include <stdlib.h>

    #define MAX_SIZE 12

    // Function prototypes
    void str_arr_to_int_arr(char str, int num, int len);
    int check_digit(int EAN, int len);

    int main(void)

    char EAN_str[MAX_SIZE + 1];
    int EAN_int[MAX_SIZE + 1];

    printf("Enter the first 12 digits of an EAN: ");
    scanf("%12s", EAN_str);

    str_arr_to_int_arr(EAN_str, EAN_int, MAX_SIZE);

    int answer = check_digit(EAN_int, MAX_SIZE);

    printf("Check digit: %dn", answer);

    return 0;


    // This function will convert a char array into an int array
    void str_arr_to_int_arr(char str, int num, int len)

    int i = 0;
    while (i < len)

    if (str[i] >= '0' && str[i] <= '9')
    num[i] = str[i] - '0';
    else
    printf("ERROR: You entered a non-numbern");
    exit(1);


    i++;




    // Does the check_digit formula on an EAN
    int check_digit(int EAN, int len)

    int sum_pos = 0;
    int sum_neg = 0;

    int i = 0;
    while (i < len)

    if (i % 2 == 0)
    sum_pos += EAN[i];
    else
    sum_neg += EAN[i];


    i++;


    return (9 - (((sum_pos * 3) + sum_neg) - 1) % 10);






    share|improve this answer






















    • check_digit(EAN_int, MAX_SIZE); will always trigger printf("ERROR: You entered a non-numbern"); and exit as it tests the null character.
      – chux
      Nov 10 at 16:18











    • check_digit() is checking 13 values unlike OP's 12. Better to make use #define MAX_SIZE 12, char EAN_str[MAX_SIZE + 1]; int EAN_int[MAX_SIZE];
      – chux
      Nov 10 at 16:19















    up vote
    2
    down vote



    accepted










    Since your scanf is using %d%d%d%d%d%d%d%d%d%d%d%d. It is expecting 12 numbers. So if you typed in 123456789012, the program scan in just 1 number. It reads 123456789012 not how you want it where one = 1, then two = 2 etc.



    You can solve this by changing your scanf to:



    scanf("%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d", &one, &two, &three, &four, &five, &six, &seven, &eight, &nine, &ten, &eleven, &twelve);



    %1d will read a single digit. So now it will do what you want it to, where one = 1, then two = 2 etc.



    Another way to solve this would be reading the number as a string. The convert the string into an int array. I've provided the code for how to do that below.



    #include <stdio.h>
    #include <stdlib.h>

    #define MAX_SIZE 12

    // Function prototypes
    void str_arr_to_int_arr(char str, int num, int len);
    int check_digit(int EAN, int len);

    int main(void)

    char EAN_str[MAX_SIZE + 1];
    int EAN_int[MAX_SIZE + 1];

    printf("Enter the first 12 digits of an EAN: ");
    scanf("%12s", EAN_str);

    str_arr_to_int_arr(EAN_str, EAN_int, MAX_SIZE);

    int answer = check_digit(EAN_int, MAX_SIZE);

    printf("Check digit: %dn", answer);

    return 0;


    // This function will convert a char array into an int array
    void str_arr_to_int_arr(char str, int num, int len)

    int i = 0;
    while (i < len)

    if (str[i] >= '0' && str[i] <= '9')
    num[i] = str[i] - '0';
    else
    printf("ERROR: You entered a non-numbern");
    exit(1);


    i++;




    // Does the check_digit formula on an EAN
    int check_digit(int EAN, int len)

    int sum_pos = 0;
    int sum_neg = 0;

    int i = 0;
    while (i < len)

    if (i % 2 == 0)
    sum_pos += EAN[i];
    else
    sum_neg += EAN[i];


    i++;


    return (9 - (((sum_pos * 3) + sum_neg) - 1) % 10);






    share|improve this answer






















    • check_digit(EAN_int, MAX_SIZE); will always trigger printf("ERROR: You entered a non-numbern"); and exit as it tests the null character.
      – chux
      Nov 10 at 16:18











    • check_digit() is checking 13 values unlike OP's 12. Better to make use #define MAX_SIZE 12, char EAN_str[MAX_SIZE + 1]; int EAN_int[MAX_SIZE];
      – chux
      Nov 10 at 16:19













    up vote
    2
    down vote



    accepted







    up vote
    2
    down vote



    accepted






    Since your scanf is using %d%d%d%d%d%d%d%d%d%d%d%d. It is expecting 12 numbers. So if you typed in 123456789012, the program scan in just 1 number. It reads 123456789012 not how you want it where one = 1, then two = 2 etc.



    You can solve this by changing your scanf to:



    scanf("%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d", &one, &two, &three, &four, &five, &six, &seven, &eight, &nine, &ten, &eleven, &twelve);



    %1d will read a single digit. So now it will do what you want it to, where one = 1, then two = 2 etc.



    Another way to solve this would be reading the number as a string. The convert the string into an int array. I've provided the code for how to do that below.



    #include <stdio.h>
    #include <stdlib.h>

    #define MAX_SIZE 12

    // Function prototypes
    void str_arr_to_int_arr(char str, int num, int len);
    int check_digit(int EAN, int len);

    int main(void)

    char EAN_str[MAX_SIZE + 1];
    int EAN_int[MAX_SIZE + 1];

    printf("Enter the first 12 digits of an EAN: ");
    scanf("%12s", EAN_str);

    str_arr_to_int_arr(EAN_str, EAN_int, MAX_SIZE);

    int answer = check_digit(EAN_int, MAX_SIZE);

    printf("Check digit: %dn", answer);

    return 0;


    // This function will convert a char array into an int array
    void str_arr_to_int_arr(char str, int num, int len)

    int i = 0;
    while (i < len)

    if (str[i] >= '0' && str[i] <= '9')
    num[i] = str[i] - '0';
    else
    printf("ERROR: You entered a non-numbern");
    exit(1);


    i++;




    // Does the check_digit formula on an EAN
    int check_digit(int EAN, int len)

    int sum_pos = 0;
    int sum_neg = 0;

    int i = 0;
    while (i < len)

    if (i % 2 == 0)
    sum_pos += EAN[i];
    else
    sum_neg += EAN[i];


    i++;


    return (9 - (((sum_pos * 3) + sum_neg) - 1) % 10);






    share|improve this answer














    Since your scanf is using %d%d%d%d%d%d%d%d%d%d%d%d. It is expecting 12 numbers. So if you typed in 123456789012, the program scan in just 1 number. It reads 123456789012 not how you want it where one = 1, then two = 2 etc.



    You can solve this by changing your scanf to:



    scanf("%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d", &one, &two, &three, &four, &five, &six, &seven, &eight, &nine, &ten, &eleven, &twelve);



    %1d will read a single digit. So now it will do what you want it to, where one = 1, then two = 2 etc.



    Another way to solve this would be reading the number as a string. The convert the string into an int array. I've provided the code for how to do that below.



    #include <stdio.h>
    #include <stdlib.h>

    #define MAX_SIZE 12

    // Function prototypes
    void str_arr_to_int_arr(char str, int num, int len);
    int check_digit(int EAN, int len);

    int main(void)

    char EAN_str[MAX_SIZE + 1];
    int EAN_int[MAX_SIZE + 1];

    printf("Enter the first 12 digits of an EAN: ");
    scanf("%12s", EAN_str);

    str_arr_to_int_arr(EAN_str, EAN_int, MAX_SIZE);

    int answer = check_digit(EAN_int, MAX_SIZE);

    printf("Check digit: %dn", answer);

    return 0;


    // This function will convert a char array into an int array
    void str_arr_to_int_arr(char str, int num, int len)

    int i = 0;
    while (i < len)

    if (str[i] >= '0' && str[i] <= '9')
    num[i] = str[i] - '0';
    else
    printf("ERROR: You entered a non-numbern");
    exit(1);


    i++;




    // Does the check_digit formula on an EAN
    int check_digit(int EAN, int len)

    int sum_pos = 0;
    int sum_neg = 0;

    int i = 0;
    while (i < len)

    if (i % 2 == 0)
    sum_pos += EAN[i];
    else
    sum_neg += EAN[i];


    i++;


    return (9 - (((sum_pos * 3) + sum_neg) - 1) % 10);







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 11 at 0:18

























    answered Nov 10 at 2:23









    Aniket

    2241110




    2241110











    • check_digit(EAN_int, MAX_SIZE); will always trigger printf("ERROR: You entered a non-numbern"); and exit as it tests the null character.
      – chux
      Nov 10 at 16:18











    • check_digit() is checking 13 values unlike OP's 12. Better to make use #define MAX_SIZE 12, char EAN_str[MAX_SIZE + 1]; int EAN_int[MAX_SIZE];
      – chux
      Nov 10 at 16:19

















    • check_digit(EAN_int, MAX_SIZE); will always trigger printf("ERROR: You entered a non-numbern"); and exit as it tests the null character.
      – chux
      Nov 10 at 16:18











    • check_digit() is checking 13 values unlike OP's 12. Better to make use #define MAX_SIZE 12, char EAN_str[MAX_SIZE + 1]; int EAN_int[MAX_SIZE];
      – chux
      Nov 10 at 16:19
















    check_digit(EAN_int, MAX_SIZE); will always trigger printf("ERROR: You entered a non-numbern"); and exit as it tests the null character.
    – chux
    Nov 10 at 16:18





    check_digit(EAN_int, MAX_SIZE); will always trigger printf("ERROR: You entered a non-numbern"); and exit as it tests the null character.
    – chux
    Nov 10 at 16:18













    check_digit() is checking 13 values unlike OP's 12. Better to make use #define MAX_SIZE 12, char EAN_str[MAX_SIZE + 1]; int EAN_int[MAX_SIZE];
    – chux
    Nov 10 at 16:19





    check_digit() is checking 13 values unlike OP's 12. Better to make use #define MAX_SIZE 12, char EAN_str[MAX_SIZE + 1]; int EAN_int[MAX_SIZE];
    – chux
    Nov 10 at 16:19













    up vote
    2
    down vote














    What am I doing wrong?




    OP wants to read a 12 digit code, yet scanf("%d%d%d%d%d%d%d%d%d%d%d%d"... looks for 12 separate int.



    @user3386109 well suggested using "%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d" to limit the length of each int to 1 digit.




    Alternative:



    As EAN can vary in length up to 18 digits, consider



    #define EAN_N 18
    char buf[2*EAN_N]; // extra for n, and other other characters.
    if (fgets(buf, sizof buf, stdin))
    int n = 0;
    while (buf[n] >= '0' && buf[n] <= '9') n++;
    if (buf[n] == 'n'





    share|improve this answer
























      up vote
      2
      down vote














      What am I doing wrong?




      OP wants to read a 12 digit code, yet scanf("%d%d%d%d%d%d%d%d%d%d%d%d"... looks for 12 separate int.



      @user3386109 well suggested using "%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d" to limit the length of each int to 1 digit.




      Alternative:



      As EAN can vary in length up to 18 digits, consider



      #define EAN_N 18
      char buf[2*EAN_N]; // extra for n, and other other characters.
      if (fgets(buf, sizof buf, stdin))
      int n = 0;
      while (buf[n] >= '0' && buf[n] <= '9') n++;
      if (buf[n] == 'n'





      share|improve this answer






















        up vote
        2
        down vote










        up vote
        2
        down vote










        What am I doing wrong?




        OP wants to read a 12 digit code, yet scanf("%d%d%d%d%d%d%d%d%d%d%d%d"... looks for 12 separate int.



        @user3386109 well suggested using "%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d" to limit the length of each int to 1 digit.




        Alternative:



        As EAN can vary in length up to 18 digits, consider



        #define EAN_N 18
        char buf[2*EAN_N]; // extra for n, and other other characters.
        if (fgets(buf, sizof buf, stdin))
        int n = 0;
        while (buf[n] >= '0' && buf[n] <= '9') n++;
        if (buf[n] == 'n'





        share|improve this answer













        What am I doing wrong?




        OP wants to read a 12 digit code, yet scanf("%d%d%d%d%d%d%d%d%d%d%d%d"... looks for 12 separate int.



        @user3386109 well suggested using "%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d" to limit the length of each int to 1 digit.




        Alternative:



        As EAN can vary in length up to 18 digits, consider



        #define EAN_N 18
        char buf[2*EAN_N]; // extra for n, and other other characters.
        if (fgets(buf, sizof buf, stdin))
        int n = 0;
        while (buf[n] >= '0' && buf[n] <= '9') n++;
        if (buf[n] == 'n'






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 2:29









        chux

        78.9k869145




        78.9k869145



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235182%2fprogram-stuck-at-scanf%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo