1

I want to scan in values and add 0's ahead of the number until it is 4 digits long.

Example:

scanf("%d",&value); //entering '4'..modify the scan somehow with format specifier?  
printf("%d",value); //prints 0004

or another example:

scanf("%d",&value); //entering '12'  
printf("%d",value); //prints 0012

or another example:

scanf("%d",&value); //entering '123'  
printf("%d",value); //prints 0123

and for the case where the scanned value is already 4 digits long, ignore.

There are format specifiers like printf("%04d",value), but specifying the format in the scanning statement is crucial to me.

input:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

my output

            _______1_______
           /                  \
     __2__                __3__
    /        \              /        \
     4         5             6         7
  / \        / \          / \        / \
 8     9    10   11     12    13    14   15

It isn't lined up like I'd like it to be, because I hard coded a situation where all digits are of length 4.

desired output:(to line up nicely)

                _______0001_______
               /                  \
         __0002__                __0003__
        /        \              /        \
      0004      0005          0006      0007
      / \        / \          / \        / \
   008   009  0010 0011   0012  0013  0014 0015

how it is currently called:

int main()
{
int q[100];
int inputValue;
int qSize=1;

while (inputValue != -1) //scanning until -1 is reached
   {
   cin >> inputValue; //here is each number

//What can I add here to make it 4 digits long? (i.e. adding 0's)

   if (inputValue != -1)
    insertAndSort (q, qSize, inputValue); //sorts a heap
   }

 drawSortedTree (q, qSize);//output
}
user3507072
  • 287
  • 1
  • 4
  • 13
  • 2
    Suggest explaining more of how scanning 4 digits is important. – chux - Reinstate Monica Oct 28 '14 at 00:12
  • Scanning in 4 digits is important because I currently formatted all of my print statements(hardcoded) to format nicely with 4 digit numbers. If it's possible, my life would be much easier if I can modify the scan statement instead to include leading 0's until input is of length 4 – user3507072 Oct 28 '14 at 00:59
  • Simple: `scanf("%4d", &value);` will read no more that 4 digits. It will accept leading `'0'` or `' '` equivalently. If code needs to insure the leading digits are `'0'` rather than `' '`, please advise. – chux - Reinstate Monica Oct 28 '14 at 01:46
  • This does not add 0's to make input '3' into '0003' – user3507072 Oct 28 '14 at 01:57
  • No. The format for scanning doe not affect how data is printed. To be clear, when code scans, using `"%4d"`, `"%d"`, etc, the value saved is an `int`. Had the text been read as `"0003"` or `"3"`, the value is still 3. – chux - Reinstate Monica Oct 28 '14 at 02:01
  • So, in other words, there is no way to store "0003" into x and have x print out as "0003" without print format specifiers? – user3507072 Oct 28 '14 at 02:04
  • You've now stated an entirely different problem. It has nothing to do with specifying the format of your input because at that stage you don't know where in the tree the number will be. The numbers must be formatted as 4 digits, on a background of spaces and/or underscores, aligned in rows so you get a nice tree. So focus on the output, and write a program, that to start with, just puts the numbers on the correct row. When you have done that, focus on the alignment. PS did you really want 008 and 009 to be 3-digit numbers? – Weather Vane Oct 28 '14 at 02:09
  • There is no way if it is stored as an `int`. The input could be stored as a string and converted to a `int` when the numeric value is needed. But I think there may be a higher level concern. IMO, it should make no difference if the input if " 3" or "0003". – chux - Reinstate Monica Oct 28 '14 at 02:10
  • a integer is ALWAYS read by scanf() and automatically converted from characters to an integer (on a 32bit machine that means the integer is 4 bytes) it ALWAYS has all the non significant bits (high order bits) set to 0. The suggested method of putting the desired display format into printf() as %04d will work perfectly. – user3629249 Oct 28 '14 at 17:34

3 Answers3

0

Do this:

scanf("%d", &value);    //entering '12'  
printf("%04d", value);  //prints 0012
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • I would like to specify format in the scanning statement because I hardcoded lots of print statements that I don't want to change – user3507072 Oct 28 '14 at 00:56
0

You say printf ("%04d", value); is not what you want - you want to specify the format in the input statement, but your examples do not specify leading zeros in your input. But if they did, the only way to preserve them would be by capturing the whole string.

int main()
{
    int value;
    char string[100];
    scanf ("%s", string);
    value = atoi (string);
    printf( "%d\n", value);
    printf( "%s\n", string);
    return 0;
}
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • My example does not specify leading zeros in my input because that is the part I am stumped on. What magic goes into the scan statement to add 0's until the final number is 4 digits long? – user3507072 Oct 28 '14 at 00:57
  • The `scanf()` statement will ignore your leading zeros. A number, once scanned, does not have leading zeros. It only keeps leading zeros if you scan as a string. If you want them on your ouput later you can do so. It's still unclear as to what you are asking, what problem you want solving. Please make a clear statement about your input data, how you want it represented within your program, and how you want it outputted. We are stuck at the GIGO stage. – Weather Vane Oct 28 '14 at 01:11
  • Details added to original post. My goal is to allow the user to input digits from length 1 to length 4, and to allow my hardcoded 4 digit scenario to work each time without changing the print statements. – user3507072 Oct 28 '14 at 01:26
0
Scanf(%40d",x); 
//wont work

You can scan & print the value as string just give the input as 00001 ,00002,00003..... instead of ,1,2,3,....

AVIK DUTTA
  • 736
  • 6
  • 23