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?
c
add a comment |
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?
c
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: thescanf()
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
add a comment |
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?
c
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
c
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: thescanf()
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
add a comment |
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: thescanf()
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
add a comment |
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);
check_digit(EAN_int, MAX_SIZE);
will always triggerprintf("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
add a comment |
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'
add a comment |
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);
check_digit(EAN_int, MAX_SIZE);
will always triggerprintf("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
add a comment |
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);
check_digit(EAN_int, MAX_SIZE);
will always triggerprintf("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
add a comment |
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);
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);
edited Nov 11 at 0:18
answered Nov 10 at 2:23
Aniket
2241110
2241110
check_digit(EAN_int, MAX_SIZE);
will always triggerprintf("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
add a comment |
check_digit(EAN_int, MAX_SIZE);
will always triggerprintf("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
add a comment |
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'
add a comment |
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'
add a comment |
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'
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'
answered Nov 10 at 2:29
chux
78.9k869145
78.9k869145
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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