Thread: Scanf Issue with Segmented input(?)

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    15

    Scanf Issue with Segmented input(?)

    Greetings, first post here so I hope everything's kosher.

    Code:
    main()
    {
    printf ("Enter initial date: ");
    scanf("%d/%d/%d", &init_month, &init_day, &init_year);
    printf ("Enter initial time: ");
    scanf ("%d:%d", &init_hour, &init_minute);
        
    printf ("Enter return date: ");
    scanf("%d/%d/%d", &fin_month, &fin_day, &fin_year);
    printf ("Enter return time: ");
    scanf ("%d:%d", &final_hour, &final_minute);
     
    if (init_month == 02)
     days_in_month = leapchecker(init_year);
    ...
    So, I have have a series of inputs before branching out into a set of if statements. The problem is that the final scanf statement does not end, and proceed with the rest of the code; I have to terminate manually. If I were to write a printf immediately following the last scanf, it does not execute. However, if I close the code immediately after the last scanf, it terminates normally.
    Any advice would be appreciated.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I tested with this program:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int init_month;
        int init_day;
        int init_year;
        int init_hour;
        int init_minute;
        int fin_month;
        int fin_day;
        int fin_year;
        int final_hour;
        int final_minute;
    
        printf ("Enter initial date: ");
        scanf("%d/%d/%d", &init_month, &init_day, &init_year);
        printf ("Enter initial time: ");
        scanf ("%d:%d", &init_hour, &init_minute);
    
        printf ("Enter return date: ");
        scanf("%d/%d/%d", &fin_month, &fin_day, &fin_year);
        printf ("Enter return time: ");
        scanf ("%d:%d", &final_hour, &final_minute);
    
        printf("printf immediately following the last scanf\n");
    
        return 0;
    }
    But with expected input, the printf immediately following the last scanf was executed.

    Therefore, I suggest that you post the smallest and simplest compilable program that demonstrates the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    15
    Essentially what I wrote up top is the fastest return of error I could muster. If I close the program immediately after any input or printf it works fine. It's when the code is introduced to the rest to the program that's when the infinite input scenario exists. As long as an if statement succeeds the displayed code, the input is stuck, essentially. Even when I introduce code between the input and the if statement. Hope that helps. I've seeded the rest of the code with periodic printfs between logic, but this is where it fails.
    Last edited by capitalCORN; 10-22-2014 at 12:48 PM. Reason: more info

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by capitalCORN
    Essentially what I wrote up top is the fastest return of error I could muster. If I close the program immediately after any input or printf it works fine. It's when the code is introduced to the rest to the program that's when the infinite input scenario exists. As long as an if statement succeeds the displayed code, the input is stuck, essentially. Even when I introduce code between the input and the if statement. Hope that helps. I've seeded the rest of the code with periodic printfs between logic, but this is where it fails.
    Compile and run the program that I posted. It demonstrates that you are mistaken, i.e., the problem probably lies in code that you did not show, or only showed a part from which we can deduce nothing useful. This is why I suggest that you post the smallest and simplest compilable program that demonstrates the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2014
    Posts
    15
    My apologies. It seems my code needs some cracker jack tuning. Thanks for the help, and if anything else comes up, I'll post again Cheers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple printf/scanf code issue
    By kumczak in forum C Programming
    Replies: 2
    Last Post: 04-08-2014, 08:33 AM
  2. scanf issue - character in buffer
    By demps in forum C Programming
    Replies: 2
    Last Post: 01-19-2010, 07:51 PM
  3. Issue with scanf and char
    By Covalent in forum C Programming
    Replies: 6
    Last Post: 11-03-2008, 12:06 AM
  4. 'cin' and 'scanf()' issue
    By Brain Cell in forum C++ Programming
    Replies: 4
    Last Post: 11-07-2004, 10:53 AM
  5. scanf issue
    By fkheng in forum C Programming
    Replies: 6
    Last Post: 06-20-2003, 07:28 AM