Leading Zeros used in int
I am trying to complete a program but the leading zero gets removed when it is a read as an int. I need this leading zero in the event a user enters a zero at the start because I am using it to do math with later in the program and can't just add the leading zero in the printf.
printf("Enter the first 6 digits of the barcode: n");
scanf("%i", &n1);
printf("Enter the first 6 digits of the barcode: n");
scanf("%i", &n2);
//Splits number1 into individual digits
count1 = 0;
while (n1 != 0){
array1[count1] = n1 % 10;
n1 /= 10;
count1++;
}
count2 = 0;
while (n2 > 0){
array2[count2] = n2 % 10;
n2 /= 10;
count2++;
//Steps 1-3
int sumo = array1[5]+array1[3]+array1[1]+array2[5]+array2[3]+array2[1]; //adds odd
int sume = array1[4]+array1[2]+array1[0]+array2[4]+array2[2]; //adds even without 12
int sumd = 3*sumo; //multiplies odds
int sum = sume+sumd; //adds above and evens
int chec = sum%10;
int check = 10-chec;
Entire program can be found here
c arrays int modulo leading-zero
add a comment |
I am trying to complete a program but the leading zero gets removed when it is a read as an int. I need this leading zero in the event a user enters a zero at the start because I am using it to do math with later in the program and can't just add the leading zero in the printf.
printf("Enter the first 6 digits of the barcode: n");
scanf("%i", &n1);
printf("Enter the first 6 digits of the barcode: n");
scanf("%i", &n2);
//Splits number1 into individual digits
count1 = 0;
while (n1 != 0){
array1[count1] = n1 % 10;
n1 /= 10;
count1++;
}
count2 = 0;
while (n2 > 0){
array2[count2] = n2 % 10;
n2 /= 10;
count2++;
//Steps 1-3
int sumo = array1[5]+array1[3]+array1[1]+array2[5]+array2[3]+array2[1]; //adds odd
int sume = array1[4]+array1[2]+array1[0]+array2[4]+array2[2]; //adds even without 12
int sumd = 3*sumo; //multiplies odds
int sum = sume+sumd; //adds above and evens
int chec = sum%10;
int check = 10-chec;
Entire program can be found here
c arrays int modulo leading-zero
1
So, scan using a string....
– Sourav Ghosh
Nov 12 '18 at 5:10
1
You need to use%d
to scan a decimal integer, not%i
. Especially when it has leading zeroes.
– Antti Haapala
Nov 12 '18 at 6:31
with%i
sequences with leading zeroes are scanned as octal numbers, which means that 0149 will be scanned as 12. not even 149. And definitely not with leading zeroes! Just unlearn the%i
, it is always wrong.
– Antti Haapala
Nov 12 '18 at 6:34
add a comment |
I am trying to complete a program but the leading zero gets removed when it is a read as an int. I need this leading zero in the event a user enters a zero at the start because I am using it to do math with later in the program and can't just add the leading zero in the printf.
printf("Enter the first 6 digits of the barcode: n");
scanf("%i", &n1);
printf("Enter the first 6 digits of the barcode: n");
scanf("%i", &n2);
//Splits number1 into individual digits
count1 = 0;
while (n1 != 0){
array1[count1] = n1 % 10;
n1 /= 10;
count1++;
}
count2 = 0;
while (n2 > 0){
array2[count2] = n2 % 10;
n2 /= 10;
count2++;
//Steps 1-3
int sumo = array1[5]+array1[3]+array1[1]+array2[5]+array2[3]+array2[1]; //adds odd
int sume = array1[4]+array1[2]+array1[0]+array2[4]+array2[2]; //adds even without 12
int sumd = 3*sumo; //multiplies odds
int sum = sume+sumd; //adds above and evens
int chec = sum%10;
int check = 10-chec;
Entire program can be found here
c arrays int modulo leading-zero
I am trying to complete a program but the leading zero gets removed when it is a read as an int. I need this leading zero in the event a user enters a zero at the start because I am using it to do math with later in the program and can't just add the leading zero in the printf.
printf("Enter the first 6 digits of the barcode: n");
scanf("%i", &n1);
printf("Enter the first 6 digits of the barcode: n");
scanf("%i", &n2);
//Splits number1 into individual digits
count1 = 0;
while (n1 != 0){
array1[count1] = n1 % 10;
n1 /= 10;
count1++;
}
count2 = 0;
while (n2 > 0){
array2[count2] = n2 % 10;
n2 /= 10;
count2++;
//Steps 1-3
int sumo = array1[5]+array1[3]+array1[1]+array2[5]+array2[3]+array2[1]; //adds odd
int sume = array1[4]+array1[2]+array1[0]+array2[4]+array2[2]; //adds even without 12
int sumd = 3*sumo; //multiplies odds
int sum = sume+sumd; //adds above and evens
int chec = sum%10;
int check = 10-chec;
Entire program can be found here
c arrays int modulo leading-zero
c arrays int modulo leading-zero
asked Nov 12 '18 at 5:04
Alec Mauro
42
42
1
So, scan using a string....
– Sourav Ghosh
Nov 12 '18 at 5:10
1
You need to use%d
to scan a decimal integer, not%i
. Especially when it has leading zeroes.
– Antti Haapala
Nov 12 '18 at 6:31
with%i
sequences with leading zeroes are scanned as octal numbers, which means that 0149 will be scanned as 12. not even 149. And definitely not with leading zeroes! Just unlearn the%i
, it is always wrong.
– Antti Haapala
Nov 12 '18 at 6:34
add a comment |
1
So, scan using a string....
– Sourav Ghosh
Nov 12 '18 at 5:10
1
You need to use%d
to scan a decimal integer, not%i
. Especially when it has leading zeroes.
– Antti Haapala
Nov 12 '18 at 6:31
with%i
sequences with leading zeroes are scanned as octal numbers, which means that 0149 will be scanned as 12. not even 149. And definitely not with leading zeroes! Just unlearn the%i
, it is always wrong.
– Antti Haapala
Nov 12 '18 at 6:34
1
1
So, scan using a string....
– Sourav Ghosh
Nov 12 '18 at 5:10
So, scan using a string....
– Sourav Ghosh
Nov 12 '18 at 5:10
1
1
You need to use
%d
to scan a decimal integer, not %i
. Especially when it has leading zeroes.– Antti Haapala
Nov 12 '18 at 6:31
You need to use
%d
to scan a decimal integer, not %i
. Especially when it has leading zeroes.– Antti Haapala
Nov 12 '18 at 6:31
with
%i
sequences with leading zeroes are scanned as octal numbers, which means that 0149 will be scanned as 12. not even 149. And definitely not with leading zeroes! Just unlearn the %i
, it is always wrong.– Antti Haapala
Nov 12 '18 at 6:34
with
%i
sequences with leading zeroes are scanned as octal numbers, which means that 0149 will be scanned as 12. not even 149. And definitely not with leading zeroes! Just unlearn the %i
, it is always wrong.– Antti Haapala
Nov 12 '18 at 6:34
add a comment |
3 Answers
3
active
oldest
votes
The leading zeroes are always going to be lost when you store the value as an Integer so you'll need to store the value as something else (probably a string)
add a comment |
You should scan the input as a string instead of int. You can later change it to int (for calculating sum) using atoi
.
You can later change it to int using atoi
Again you lost the leading zeros.
– kiran Biradar
Nov 12 '18 at 5:28
There is no direct way to add leading zeros toint
, that's why there was a suggestion to use strings which will keep leading zeros intact. I suggested to useatoi
for doing this kind of operationarray1[5]+array1[3]+array1[1]+array2[5]+array2[3]+array2[1];
– Sandeep
Nov 12 '18 at 5:45
Why can't he directly add ASCII values representing the digits?
– kiran Biradar
Nov 12 '18 at 5:46
He can, but ultimately he needs the value as anint
, right?
– Sandeep
Nov 12 '18 at 5:47
1
Based off what I have read I would need to use atoi and then convert it back to an int before doing the math for sum and such.
– Alec Mauro
Nov 12 '18 at 6:10
add a comment |
Leading Zeros used in int
First, improve code by:
Check the return value from
scanf()
.
Be sure to use
"%d"
instead of"%i"
when leading zeros are possible with decimal input. With"%i"
, a leading 0 indicates octal input. @Antti Haapala. This change alone with help OP.
"%d" "012" --> 12 decimal
"%d" "078" --> 78 decimal
"%i" "012" --> 10 decimal
"%i" "078" --> 7 decimal, with "8" left in stdin
Various approaches to find the leading '0'
follow:
To count the number of characters entered, record the scan position before and after the int
using "%n"
. "%n"
does not contribute to the return value of scanf()
.
int n1;
int offset1, offset2;
if (scanf(" %n%d%n", &offset1, &n1, &offset2) == 1) {
int width = offset2 - offset1;
printf("%0*dn", width, n1);
}
This counts the characters and not just digits as "+123"
has a width of 4.
A more robust approach would read input as a string and then process it.
// printf("Enter the first 6 digits of the barcode: n");
char buf[6+1];
if (scanf(" %6[0-9]", buf) == 1) {
int n1 = atoi(buf);
int width = strlen(buf);
printf("%0*dn", width, n1);
}
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f53256203%2fleading-zeros-used-in-int%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The leading zeroes are always going to be lost when you store the value as an Integer so you'll need to store the value as something else (probably a string)
add a comment |
The leading zeroes are always going to be lost when you store the value as an Integer so you'll need to store the value as something else (probably a string)
add a comment |
The leading zeroes are always going to be lost when you store the value as an Integer so you'll need to store the value as something else (probably a string)
The leading zeroes are always going to be lost when you store the value as an Integer so you'll need to store the value as something else (probably a string)
answered Nov 12 '18 at 5:13
A. Timms
764
764
add a comment |
add a comment |
You should scan the input as a string instead of int. You can later change it to int (for calculating sum) using atoi
.
You can later change it to int using atoi
Again you lost the leading zeros.
– kiran Biradar
Nov 12 '18 at 5:28
There is no direct way to add leading zeros toint
, that's why there was a suggestion to use strings which will keep leading zeros intact. I suggested to useatoi
for doing this kind of operationarray1[5]+array1[3]+array1[1]+array2[5]+array2[3]+array2[1];
– Sandeep
Nov 12 '18 at 5:45
Why can't he directly add ASCII values representing the digits?
– kiran Biradar
Nov 12 '18 at 5:46
He can, but ultimately he needs the value as anint
, right?
– Sandeep
Nov 12 '18 at 5:47
1
Based off what I have read I would need to use atoi and then convert it back to an int before doing the math for sum and such.
– Alec Mauro
Nov 12 '18 at 6:10
add a comment |
You should scan the input as a string instead of int. You can later change it to int (for calculating sum) using atoi
.
You can later change it to int using atoi
Again you lost the leading zeros.
– kiran Biradar
Nov 12 '18 at 5:28
There is no direct way to add leading zeros toint
, that's why there was a suggestion to use strings which will keep leading zeros intact. I suggested to useatoi
for doing this kind of operationarray1[5]+array1[3]+array1[1]+array2[5]+array2[3]+array2[1];
– Sandeep
Nov 12 '18 at 5:45
Why can't he directly add ASCII values representing the digits?
– kiran Biradar
Nov 12 '18 at 5:46
He can, but ultimately he needs the value as anint
, right?
– Sandeep
Nov 12 '18 at 5:47
1
Based off what I have read I would need to use atoi and then convert it back to an int before doing the math for sum and such.
– Alec Mauro
Nov 12 '18 at 6:10
add a comment |
You should scan the input as a string instead of int. You can later change it to int (for calculating sum) using atoi
.
You should scan the input as a string instead of int. You can later change it to int (for calculating sum) using atoi
.
answered Nov 12 '18 at 5:17
Sandeep
647311
647311
You can later change it to int using atoi
Again you lost the leading zeros.
– kiran Biradar
Nov 12 '18 at 5:28
There is no direct way to add leading zeros toint
, that's why there was a suggestion to use strings which will keep leading zeros intact. I suggested to useatoi
for doing this kind of operationarray1[5]+array1[3]+array1[1]+array2[5]+array2[3]+array2[1];
– Sandeep
Nov 12 '18 at 5:45
Why can't he directly add ASCII values representing the digits?
– kiran Biradar
Nov 12 '18 at 5:46
He can, but ultimately he needs the value as anint
, right?
– Sandeep
Nov 12 '18 at 5:47
1
Based off what I have read I would need to use atoi and then convert it back to an int before doing the math for sum and such.
– Alec Mauro
Nov 12 '18 at 6:10
add a comment |
You can later change it to int using atoi
Again you lost the leading zeros.
– kiran Biradar
Nov 12 '18 at 5:28
There is no direct way to add leading zeros toint
, that's why there was a suggestion to use strings which will keep leading zeros intact. I suggested to useatoi
for doing this kind of operationarray1[5]+array1[3]+array1[1]+array2[5]+array2[3]+array2[1];
– Sandeep
Nov 12 '18 at 5:45
Why can't he directly add ASCII values representing the digits?
– kiran Biradar
Nov 12 '18 at 5:46
He can, but ultimately he needs the value as anint
, right?
– Sandeep
Nov 12 '18 at 5:47
1
Based off what I have read I would need to use atoi and then convert it back to an int before doing the math for sum and such.
– Alec Mauro
Nov 12 '18 at 6:10
You can later change it to int using atoi
Again you lost the leading zeros.– kiran Biradar
Nov 12 '18 at 5:28
You can later change it to int using atoi
Again you lost the leading zeros.– kiran Biradar
Nov 12 '18 at 5:28
There is no direct way to add leading zeros to
int
, that's why there was a suggestion to use strings which will keep leading zeros intact. I suggested to use atoi
for doing this kind of operation array1[5]+array1[3]+array1[1]+array2[5]+array2[3]+array2[1];
– Sandeep
Nov 12 '18 at 5:45
There is no direct way to add leading zeros to
int
, that's why there was a suggestion to use strings which will keep leading zeros intact. I suggested to use atoi
for doing this kind of operation array1[5]+array1[3]+array1[1]+array2[5]+array2[3]+array2[1];
– Sandeep
Nov 12 '18 at 5:45
Why can't he directly add ASCII values representing the digits?
– kiran Biradar
Nov 12 '18 at 5:46
Why can't he directly add ASCII values representing the digits?
– kiran Biradar
Nov 12 '18 at 5:46
He can, but ultimately he needs the value as an
int
, right?– Sandeep
Nov 12 '18 at 5:47
He can, but ultimately he needs the value as an
int
, right?– Sandeep
Nov 12 '18 at 5:47
1
1
Based off what I have read I would need to use atoi and then convert it back to an int before doing the math for sum and such.
– Alec Mauro
Nov 12 '18 at 6:10
Based off what I have read I would need to use atoi and then convert it back to an int before doing the math for sum and such.
– Alec Mauro
Nov 12 '18 at 6:10
add a comment |
Leading Zeros used in int
First, improve code by:
Check the return value from
scanf()
.
Be sure to use
"%d"
instead of"%i"
when leading zeros are possible with decimal input. With"%i"
, a leading 0 indicates octal input. @Antti Haapala. This change alone with help OP.
"%d" "012" --> 12 decimal
"%d" "078" --> 78 decimal
"%i" "012" --> 10 decimal
"%i" "078" --> 7 decimal, with "8" left in stdin
Various approaches to find the leading '0'
follow:
To count the number of characters entered, record the scan position before and after the int
using "%n"
. "%n"
does not contribute to the return value of scanf()
.
int n1;
int offset1, offset2;
if (scanf(" %n%d%n", &offset1, &n1, &offset2) == 1) {
int width = offset2 - offset1;
printf("%0*dn", width, n1);
}
This counts the characters and not just digits as "+123"
has a width of 4.
A more robust approach would read input as a string and then process it.
// printf("Enter the first 6 digits of the barcode: n");
char buf[6+1];
if (scanf(" %6[0-9]", buf) == 1) {
int n1 = atoi(buf);
int width = strlen(buf);
printf("%0*dn", width, n1);
}
add a comment |
Leading Zeros used in int
First, improve code by:
Check the return value from
scanf()
.
Be sure to use
"%d"
instead of"%i"
when leading zeros are possible with decimal input. With"%i"
, a leading 0 indicates octal input. @Antti Haapala. This change alone with help OP.
"%d" "012" --> 12 decimal
"%d" "078" --> 78 decimal
"%i" "012" --> 10 decimal
"%i" "078" --> 7 decimal, with "8" left in stdin
Various approaches to find the leading '0'
follow:
To count the number of characters entered, record the scan position before and after the int
using "%n"
. "%n"
does not contribute to the return value of scanf()
.
int n1;
int offset1, offset2;
if (scanf(" %n%d%n", &offset1, &n1, &offset2) == 1) {
int width = offset2 - offset1;
printf("%0*dn", width, n1);
}
This counts the characters and not just digits as "+123"
has a width of 4.
A more robust approach would read input as a string and then process it.
// printf("Enter the first 6 digits of the barcode: n");
char buf[6+1];
if (scanf(" %6[0-9]", buf) == 1) {
int n1 = atoi(buf);
int width = strlen(buf);
printf("%0*dn", width, n1);
}
add a comment |
Leading Zeros used in int
First, improve code by:
Check the return value from
scanf()
.
Be sure to use
"%d"
instead of"%i"
when leading zeros are possible with decimal input. With"%i"
, a leading 0 indicates octal input. @Antti Haapala. This change alone with help OP.
"%d" "012" --> 12 decimal
"%d" "078" --> 78 decimal
"%i" "012" --> 10 decimal
"%i" "078" --> 7 decimal, with "8" left in stdin
Various approaches to find the leading '0'
follow:
To count the number of characters entered, record the scan position before and after the int
using "%n"
. "%n"
does not contribute to the return value of scanf()
.
int n1;
int offset1, offset2;
if (scanf(" %n%d%n", &offset1, &n1, &offset2) == 1) {
int width = offset2 - offset1;
printf("%0*dn", width, n1);
}
This counts the characters and not just digits as "+123"
has a width of 4.
A more robust approach would read input as a string and then process it.
// printf("Enter the first 6 digits of the barcode: n");
char buf[6+1];
if (scanf(" %6[0-9]", buf) == 1) {
int n1 = atoi(buf);
int width = strlen(buf);
printf("%0*dn", width, n1);
}
Leading Zeros used in int
First, improve code by:
Check the return value from
scanf()
.
Be sure to use
"%d"
instead of"%i"
when leading zeros are possible with decimal input. With"%i"
, a leading 0 indicates octal input. @Antti Haapala. This change alone with help OP.
"%d" "012" --> 12 decimal
"%d" "078" --> 78 decimal
"%i" "012" --> 10 decimal
"%i" "078" --> 7 decimal, with "8" left in stdin
Various approaches to find the leading '0'
follow:
To count the number of characters entered, record the scan position before and after the int
using "%n"
. "%n"
does not contribute to the return value of scanf()
.
int n1;
int offset1, offset2;
if (scanf(" %n%d%n", &offset1, &n1, &offset2) == 1) {
int width = offset2 - offset1;
printf("%0*dn", width, n1);
}
This counts the characters and not just digits as "+123"
has a width of 4.
A more robust approach would read input as a string and then process it.
// printf("Enter the first 6 digits of the barcode: n");
char buf[6+1];
if (scanf(" %6[0-9]", buf) == 1) {
int n1 = atoi(buf);
int width = strlen(buf);
printf("%0*dn", width, n1);
}
edited Nov 12 '18 at 6:44
answered Nov 12 '18 at 6:25
chux
80.5k870148
80.5k870148
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%2f53256203%2fleading-zeros-used-in-int%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
1
So, scan using a string....
– Sourav Ghosh
Nov 12 '18 at 5:10
1
You need to use
%d
to scan a decimal integer, not%i
. Especially when it has leading zeroes.– Antti Haapala
Nov 12 '18 at 6:31
with
%i
sequences with leading zeroes are scanned as octal numbers, which means that 0149 will be scanned as 12. not even 149. And definitely not with leading zeroes! Just unlearn the%i
, it is always wrong.– Antti Haapala
Nov 12 '18 at 6:34