Thread: Missing out lines

  1. #1
    Registered User Inept Pig's Avatar
    Join Date
    Apr 2002
    Posts
    140

    Post Missing out lines

    When using the following code I have a problem with the fact that, during running the program, it skips the entire enter date section and just prints the next line, not allowing for any input, most frustrating... the code is:


    printf("Enter customer name: ");
    scanf("%s", job.club);
    printf("Enter job number: ");
    scanf("%s", job.job_num);
    printf("Enter job date: ");
    scanf("2%d", job.job_date.day);
    scanf("2%d", job.job_date.month);
    scanf("4%d", job.job_date.year);
    printf("Enter fault code: ");
    scanf("%s", job.f_code);
    printf("Enter any comments: ");
    gets (job.comments[200]);


    Thanks in advance for any help/advice..

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    scanf("%s", job.club);

    Is this a pointer with allocated space?
    If not, you're using scanf wrong.

    printf("Enter job number: ");
    scanf("%s", job.job_num);

    Is this a pointer with allocated space?
    If not, you're using scanf wrong.

    printf("Enter job date: ");
    scanf("2%d", job.job_date.day);

    Is this a pointer with allocated space?
    If not, you're using scanf wrong.

    scanf("2%d", job.job_date.month);

    Is this a pointer with allocated space?
    If not, you're using scanf wrong.
    Repeat ad-nausium.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >scanf("%s", job.club);
    The scanf function being irresponsibly used with string data...I know that I've warned about this many times in the past. Friends don't let friends use scanf for user input, so I'll offer fgets as an alternative.

    >scanf("2%d", job.job_date.day);
    >scanf("2%d", job.job_date.month);
    >scanf("4%d", job.job_date.year);
    I'd bet my next paycheck that these structure members are not pointers.

    >gets (job.comments[200]);
    This really isn't much better.

    First, I'd recommend that you read up on scanf and gets before using them further. Then read all previous posts on the problems that they can cause. Then, if you still feel the desire to use them for some strange reason, you'll at least be in a better position to use them as correctly as they can be used.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User Inept Pig's Avatar
    Join Date
    Apr 2002
    Posts
    140
    Aaaaah... Thanks for the advice...

    And I promise never to post another program using scanf for user input... lesson learned...


  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >And I promise never to post another program using scanf for user input...
    Using scanf for very simple user input such as a single integer is fine. The problem is that scanf was designed for formatted input and users never format their input correctly, so you end up dealing with more problems than necessary.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM