Bug in Program - please help!

This is a discussion on Bug in Program - please help! within the C Programming forums, part of the General Programming Boards category; I have written this program, and it uses fgets to get data from the user keyboard. For some reason, my ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    20

    Bug in Program - please help!

    I have written this program, and it uses fgets to get data from the user keyboard. For some reason, my last question, which asks for them to enter the date and time, does not execute the fscanf and assignment. It just does the printf, then goes on ... it's like it's ignoring the { } under the if's ....

    I need a second pair of eyes here! It must be something really stupid, and I just can't see it myself!

    Here's the code: Thanks!!
    **************************************************


    /* Create a file */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    /* Function Prototypes */

    /* Declaratives */

    char input_file_name[20];
    char input_filename[24];

    char output_file_name[20];
    char output_filename[24];

    char report_file_name[20];
    char report_filename[24];

    char date_answer[4];
    char in_datetime[13];

    int main()
    {
    FILE *inputPtr;
    FILE *outputPtr;
    FILE *reportPtr;

    printf("Enter input file name & hit enter (example: input.txt) ... > ");
    fgets(input_file_name, sizeof(input_file_name), stdin);
    input_file_name[strlen(input_file_name) - 1] = '\0';

    strcpy(input_filename, "a:");
    strcat(input_filename, "\\");
    strcat(input_filename, input_file_name);

    printf("Enter output file name & hit enter (example: output.txt) ... > ");
    fgets(output_file_name, sizeof(output_file_name), stdin);
    output_file_name[strlen(output_file_name) - 1] = '\0';

    strcpy(output_filename, "a:");
    strcat(output_filename, "\\");
    strcat(output_filename, output_file_name);

    printf("Enter report file name & hit enter (example: report.txt) ... > ");
    fgets(report_file_name, sizeof(report_file_name), stdin);
    report_file_name[strlen(report_file_name) - 1] = '\0';

    strcpy(report_filename, "a:");
    strcat(report_filename, "\\");
    strcat(report_filename, report_file_name);

    printf("\nWould you like to override today's date for processing? (yes or no) ... > ");
    fgets(date_answer, sizeof(date_answer), stdin);
    date_answer[strlen(date_answer) - 1] = '\0';

    if (date_answer[0] == 'y')
    {
    printf("\nEnter processing date & hit enter (e.g. CCYYMMDDHHMM) ... > ");
    fgets(in_datetime, sizeof(in_datetime), stdin);
    in_datetime[strlen(in_datetime) - 1] = '\0';
    }
    else
    if (date_answer[0] == 'Y')
    {
    printf("\nEnter processing date & hit enter (e.g. CCYYMMDDHHMM) ... > ");
    fgets(in_datetime, sizeof(in_datetime), stdin);
    in_datetime[strlen(in_datetime) - 1] = '\0';
    }

    printf("\n**************************************** ****************************************");
    printf("\nThank you .... The program is creating your file .... Please wait.\n");
    printf("\n**************************************** ****************************************");

    rest of program follows .........

    The problem is that instead of letting the user enter the date and time, it just displays the prompt for that .... then just immediately displays the "Thank you .... The program is creating" message.

    Any help is greatly appreciated!!!
    muffin

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Hi could be wrong, but it could have something to do with not declaring time structure and variables within main().
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    7
    I didn't look too close at your code, but one thing I noticed is that you're trying to read from the keyboard with fgets. fgets is for reading data from files. You should use gets or scanf to get data from the keyboard.

    ps I'm an unregistered user, not Strahan. I don't know why I'm being displayed as Strahan.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    72
    Hi

    from MSDN:

    "fgets reads characters from the current stream position to and including the first newline character, to the end of the stream, or until the number of characters read is equal to n – 1, <B>whichever comes first</B>. The result stored in string is appended with a null character. The newline character, if read, is included in the string."

    so when you type 'yes'it appends a '\n' char but in the input buffer you have left an '\r' (on the dos/windows OS)

    so the next call to <B>fgets</B> didn't wait the user to enter something it just fiil-up the buffer with what was left and then imediately retrns.

    To improve the situation Enlarge the 'date_answer' array with one or more.

    or just enter a single 'y' to answer the question and all will be fine.

    damyan

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    20
    I didn't use gets or scanf because of the possibility of overflow problems ... pretty much of what I've read says to use fgets instead.

    I'll give your suggestion a try, and post the results (thanks damyan!).

    muffin

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  3. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 09:49 PM
  4. Replies: 3
    Last Post: 01-14-2003, 09:34 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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