Not getting output I expect....

Discussion in 'Mac Programming' started by cybrscot, Dec 31, 2010.

  1. cybrscot, Dec 31, 2010
    Last edited by a moderator: Dec 31, 2010

    cybrscot macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #1
    Okay, in this program I want a user to enter a date in the form of mm/dd/yy, and then have it displayed in the form yymmdd.

    Although I rearranged the variables in the printf string, so I thought they would display in the order I asked, they did not, they displayed the date exactly as the user input. (but added two zeros, one at the beginning and one at the end, why?) Also, as I entered the date (as the user) it just entered as 110671. Can I get my program to insert a / immediately after I input the first two integers, then another / before the last two? Without doing a scanf string just for the first two, then a printf string just for the /, then a scanf string for the next two digits, then another printf string for another /, etc, etc??? Here's my code... below the code is the output of the program....

    Code:
    #include <stdio.h>
    
    main ( )
    {
    
    	int month ;
    	int day ;
    	int year ;
    
    	printf ("Enter a date (mm/dd/yy):") ;
    	scanf  ("%d %d %d", &month, &day, &year) ;
    	printf ("You entered the date: %d %d %d\n", year, month, day) ;
    	
    	return 0 ;
    	
    }	
    
    Here is the output.....

    Enter a date (mm/dd/yy):110671
    You entered the date: 0 110671 0
    Scott-Deans-MacBook-Pro:documents scottdean$
     
  2. Mitthrawnuruodo Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #2
    Hint: What are you using as seperator when inputting the date?
     
  3. cybrscot, Dec 31, 2010
    Last edited by a moderator: Dec 31, 2010

    cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #3
    Hmmm.....well that's part of the problem, nothing. I'm just entering 110671, and I'm getting 0 110671 0. I would like to enter, 11 then a / pops up, then enter 06, then another / pops up, then I finish with my 71. Or maybe I will type the / myself. Let me take another look at it.

    Thanks!

    Well so much for that, I entered (as user) 11/06/71 instead of 110671 and I got


    Enter a date (mm/dd/yy):11/06/71
    You entered the date: 0 11 0 /* Argggghhhhh!!! */


    Still stumped....

    My scanf string is only expecting %d%d%d.

    The problem lies here I'm sure, because the compiler doesn't know what numbers I want in each %d. It could be 01, or 111, or my entire date 110671, could be interpreted as just for the first %d.
     
  4. Mitthrawnuruodo, Dec 31, 2010
    Last edited: Dec 31, 2010

    Mitthrawnuruodo Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #4
    The problem is the when you use scanf to get the month, date and year you need to be aware which seperators you typed in (and it's nice if those are the same you asked for in the printf) and then use those in the scanf string, so if you use:
    Code:
    	printf ("Enter a date (mm dd yy):") ;
    	scanf  ("%d %d %d", &month, &day, &year) ;
    then you need to type in "12 31 10", notice the spaces between the numbers.

    If you use:
    Code:
    	printf ("Enter a date (mm/dd/yy):") ;
    	scanf  ("%d/%d/%d", &month, &day, &year) ;
    then you need to type "12/31/10", ie. with the two '/'-s when prompted.
     
  5. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #5
    Yep, you're right, but you knew that already!! This is so simple, yet so difficult at the same time. However, I think I'm not repeating the same mistakes. Once I learn it, I do it right.

    One thing however, my output was:

    /*the below is my program output */

    Enter a date (mm/dd/yy):11/06/71
    You entered the date: 71/11/6

    /*program output has ended */

    Which is exactly what I wanted, but is there a way to get the output to be 71/11/06? Can I tell my printf string to show the 0 before the digit if a single digit is entered by the user?
     
  6. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #6
    man printf
    Use %02d instead of %d to print an integer field 2 characters wide with leading zeros.

    B
     
  7. robvas macrumors 68020

    Joined:
    Mar 29, 2009
    Location:
    USA
    #7
    You might be better off entering each part of the date one at a time (month, day, year).

    Or, enter it as one big number '110211' (input) and then just put some code to split that up (input) into the three values.
     

Share This Page