Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cybrscot

macrumors 6502
Original poster
Dec 7, 2010
282
0
Somewhere in Southeast Asia
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$
 
Last edited by a moderator:
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.
 
Last edited by a moderator:
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.
 
Last edited:
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?
 
Can I tell my printf string to show the 0 before the digit if a single digit is entered by the user?

man printf
Code:
           0       A zero `0' character indicating that zero-padding should
                     be used rather than blank-padding.  A `-' overrides a `0'
                     if both are used;

Use %02d instead of %d to print an integer field 2 characters wide with leading zeros.

B
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.