-2

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

Alec Mauro
  • 9
  • 1
  • 3

3 Answers3

3

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)

C. Timms
  • 86
  • 4
0

You should scan the input as a string instead of int. You can later change it to int (for calculating sum) using atoi.

Sandy
  • 895
  • 6
  • 17
  • `You can later change it to int using atoi` Again you lost the leading zeros. – kiran Biradar Nov 12 '18 at 05: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];` – Sandy Nov 12 '18 at 05:45
  • Why can't he directly add ASCII values representing the digits? – kiran Biradar Nov 12 '18 at 05:46
  • He can, but ultimately he needs the value as an `int`, right? – Sandy Nov 12 '18 at 05: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 06:10
0

Leading Zeros used in int

First, improve code by:

  1. Check the return value from scanf().

  2. 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*d\n", 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*d\n", width, n1);
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256