#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    15
    Rep Power
    0

    Code Assistance Please


    I am trying to write a program that accepts a date from the user in the form mm/dd/yy and then displays it in the format yymmdd:

    Enter a date: (mm/dd/yy): 09/11/03

    You entered the date: 031109

    This is what I have and it will not print out correctly. What is wrong??


    /*Program that accepts a date from the user in the form mm/dd/yy and then displays it in the form yymmdd*/


    #include "stdafx.h"
    #include <stdio.h>

    main()
    {
    int mm;
    int dd;
    int yy;

    printf("Enter a Date mmddyy:\n");
    scanf("%d""%d""%d", &yy, &mm, &dd);

    printf("Date is:" , "%d", yy);


    return 0;

    }
  2. #2
  3. No Profile Picture
    Mid-level Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    44
    Rep Power
    12
    Reading http://www.unidata.ucar.edu/cgi-bin/man-cgi?scanf+3 I noticed that maybe the scanf is wrong. Try
    Code:
    scanf("%2d""%2d""%2d", &yy, &mm, &dd);
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    15
    Rep Power
    0
    Fubar,

    Thank you for your fast response. I am just learning C Language and our teacher is no help at all my fellow student and I worked on this for 2 hours! I will try it and see if this solves the problem.

    Thanks!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Location
    NC, USA
    Posts
    364
    Rep Power
    13
    What is the stdafx.h header for?

    The way you're trying it isn't going to work. You're entering a string but your scanf is is looking for 3 integers. You probably need to just enter it as a string and create a function to pull the month, date, and year from it.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    15
    Rep Power
    0
    stdafx.h

    Our teacher said it is what we have to use with the compiler program we use at school.
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    15
    Rep Power
    0
    damonb-

    Just wondering you say I am entering a string but the dates are numbers so are they not considered integers?
  12. #7
  13. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    This little sample worked compiled with VC++ 6
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void invalid_date() {
    	printf("The date you entered did not parse correctly.\n"
    		"Are you sure it was in the specified format?\n");
    	exit(1);
    }
    
    int main(void) {
    	char month[3];
    	char day[3];
    	char year[3];
    	char input[10];
    	char *working = NULL;
    
    	printf("Enter a date (mm/dd/yy): ");
    	fgets(input, 10, stdin);
    
    	working = strtok(input, "/");
    	if (NULL != working) {
    		strcpy(month, working);
    		
    		working = strtok(NULL, "/");
    		if (NULL != working) {
    			strcpy(day, working);
    			
    			if (NULL != working) {
    				working = strtok(NULL, "/");
    
    				if (NULL != working) {
    					strcpy(year, working);
    					year[2] = '\0';
    				} else {
    					invalid_date();
    				}
    			} else {
    				invalid_date();
    			}
    		} else {
    			invalid_date();
    		}
    	} else {
    		invalid_date();
    	}
    
    	printf("\nModified date: %s%s%s\n", year, day, month);
    	return (0);
    }
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    15
    Rep Power
    0
    They are Beautiful! Thanks for the help!
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Location
    NC, USA
    Posts
    364
    Rep Power
    13
    Originally posted by LisaOgle0917
    damonb-

    Just wondering you say I am entering a string but the dates are numbers so are they not considered integers?
    When you add the / characters it reads it as one big string. What compiler are you using to have to include that header file? That's just odd.
    Last edited by damonbrinkley; September 12th, 2003 at 09:15 AM.
  18. #10
  19. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    15
    Rep Power
    0
    We use the MS Visual C++, Like I said I am just learning this stuff. She said I had to include it in the header. I don't really know why.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Location
    NC, USA
    Posts
    364
    Rep Power
    13
    I understand. I don't have any experience with Visual C++ so it may be something specific to it.
  22. #12
  23. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    the only reason why you would have to include that header is if you are going to use anything mfc related.
  24. #13
  25. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    15
    Rep Power
    0
    Onslaught I tried your program in the compiler located at:
    http://www.delorie.com/djgpp/compile/

    It gives us what we were already getting. We get the first part but the 2nd part where it is supposed to say Date is: and spit it out in yymmdd it doesn't do. That's where the problem is.
  26. #14
  27. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    sorry, your first example returned yyddmm (which I thought was wierd) and didn't really see the /* comment */ line
    printf("\nModified date: %s%s%s\n", year, day, month);
    should be
    printf("\nModified date: %s%s%s\n", year, month, day);

    This gives the end result of:
    Modified date: yymmdd
    Last edited by Onslaught; September 12th, 2003 at 01:52 PM.
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    12
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
    
        int year;
        int month;
        int day;
        
        printf("Enter a date in MM/DD/YYYY format: ");
        scanf("%d/%d/%d", &month, &day, &year);
        printf("In compressed format: %4.4d%2.2d%2.2d\n", year, month, day);
      
      system("PAUSE");	
      return 0;
    }
    This works exceptionally well in Dev-C++. I would be suprised at any problems from VC++ with this code, since it's very standards compliant. You don't need the stdafx.h include at all for a console program like this. Your instructor is used to creating MFC apps is all, so includes it by default.

    The previous advice to handle the data as strings is problematic, by the way. You won't get expansion of numeric values to deal properly with single digit values.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/

IMN logo majestic logo threadwatch logo seochat tools logo