Determining next date using structures

This is a discussion on Determining next date using structures within the C Programming forums, part of the General Programming Boards category; Hello all, I am trying to construct a c program that accepts a user entered date and then prints the ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    43

    Determining next date using structures

    Hello all, I am trying to construct a c program that accepts a user entered date and then prints the following day. Eventually I need to get it so it is accurate to the month that it is in, but as a first step I am just trying to get the next day part to work correctly. This is the code that I have currently. I think my problem is with my scanf statements but I'm not sure exactly what the problem is. Help would be greatly appreciated.
    Code:
    #include <stdio.h>
    
    struct date
    {
       int month;
       int day;
       int year;
    };
    
    
    int main()
    {
    int tomorrow;
    struct date tom;
     
    puts("Enter date in the format m/d/yy\n");
        scanf("%d",&tom.month);
        scanf("%d",&tom.day);
        scanf("%d",&tom.year);
    
    
    tomorrow=tom.day+1;
    
    
    printf("Tomorrow's date is: %d/%d/%d \n",tom.month,tomorrow,tom.year % 100);
    
    
    system ("PAUSE");
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,650
    Can you describe the problem you're having? What are you expecting, and what are you actually seeing? What are you entering?

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    scanf won't edit:automatically skip over the /'s if you are typing those in. your scanf probably should be scanf("%d/",&tom.month) and same for day

    edit: also, if you are getting user input, you should check the return value of scanf. it returns a count of items that matched or in your case the second scanf would be returning 0 meaning it wanted an int but saw a '/'
    Last edited by dmh2000; 11-27-2012 at 03:31 PM.

  4. #4
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    The character '/' does not make part of an integer.
    When you type a date as, for instance, "11/27/2012", the first scanf successfuly parses the 11 leaving "/27/2012" pending.
    The second scanf catches the '/' and stops (as does the third scanf).

    What you can do is enter the date without 'strange' symbols, just spaces:

    Code:
    puts("Enter month day and year separated by spaces");
    if (scanf("%d", &tom.month) != 1) /* error */;
    /* ... */
    or ignore the character in the scanf itself
    Code:
    puts("Enter month day and year separated by spaces");
    if (scanf("%d", &tom.month) != 1) /* error */;
    if (scanf("/%d", &tom.day) != 1) /* error */; /* parse and ignore a / */
    if (scanf("/%d", &tom.year) != 1) /* error */; /* parse and ignore a / */
    Note, putting it all together, for the last option, is (I think) more usual
    Code:
    puts("Enter month day and year separated by spaces");
    if (scanf("%d/%d/%d", &tom.month, &tom.day, &tom.year) != 3) /* error */;
    Also, like I did above, you should always check the return value of scanf ...

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    43
    Sorry for the late reply, I made some changes suggested but the program is producing a result like: if the user enters '11 14 83' then the program prints 11/1/32. This is the code I have right now after changes have been made.
    Code:
    #include <stdio.h>
    
    struct date
    {
       int month;
       int day;
       int year;
    };
    
    
    int main()
    {
    int tomorrow;
    struct date tom;
     
    puts("Enter month, day, and year separated by spaces (mm dd yy):\n");
    if (scanf("%d/%d/%d", &tom.month, &tom.day, &tom.year) != 3);
    
    
    
    
    tomorrow=tom.day+1;
    
    
    printf("Tomorrow's date is: %d/%d/%d \n",tom.month,tomorrow,tom.year % 100);
    
    
    system ("PAUSE");
    return 0;
    }

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Change this
    Code:
    scanf("%d/%d/%d", &tom.month, &tom.day, &tom.year
    to this
    Code:
    scanf("%d %d %d", &tom.month, &tom.day, &tom.year
    and let scanf do her job

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    43
    Thank you all so much, I learned alot about the different options for setting up scanf statements. Thanks all!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-07-2012, 08:37 AM
  2. Replies: 10
    Last Post: 03-28-2012, 11:30 AM
  3. Replies: 11
    Last Post: 03-28-2012, 12:37 AM
  4. Two date objects showing same date?
    By dxfoo in forum C++ Programming
    Replies: 7
    Last Post: 06-17-2010, 07:06 PM
  5. Determining the Date of the [nth] [Day] in [Month]
    By Dave_Sinkula in forum C Programming
    Replies: 5
    Last Post: 05-25-2007, 05:12 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21