Thread: Program skips scanf

  1. #1
    Registered User
    Join Date
    Jun 2015

    Program skips scanf

    I have the following code, and it works fine, except for when it gets to the last if statement. The scanf inside the if statement is skipped and the program ends. Can anyone tell me why this happens? I've tried putting printf statements inside the last if, and they work fine.

    #include <stdio.h>
    #include <ctype.h>
    #define J 30
    int main (void)
    int i, order;    
    char first_name[J], initial[J], last_name[J];
    printf ("Please enter your first, middle and then last name.\n");
    scanf ("%s %s %s", first_name, initial, last_name);
    first_name[i] = toupper(first_name[i]);
    initial[i] = toupper (initial[i]);
    while (i<J)
        last_name [i] = toupper (last_name[i]);
    printf ("\n");
    printf ("MORTGAGE CALCULATIONS PRINTED SPECIALLY FOR %s %.1s. %s\n\n", first_name, initial, last_name);
    printf ("We offer the following terms for mortgages (in years).\n");
    printf ("Please select a term by choosing its order number:\n\n");
    printf ("1.        5 years\n");
    printf ("2.        6 years\n");
    printf ("3.        7 years\n");
    printf ("4.        10 years\n");
    printf ("5.        18 years\n");
    while (scanf ("%d", &order)!=1)
        printf ("Please enter an order number from 1-5\n");
        fflush (stdin);
    if (getchar ()!='\n')
        scanf ("%d", &order);
    return 0;

  2. #2
    Registered User
    Join Date
    Jun 2015
    Nevermind, have found my dumb mistake.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program skips functions in main
    By En-Motion in forum C++ Programming
    Replies: 5
    Last Post: 02-18-2009, 09:35 PM
  2. scanf skips lines of code in DOS using Dev-C++ and Windows XP
    By jenovanomusuko in forum C Programming
    Replies: 9
    Last Post: 12-21-2008, 03:10 AM
  3. program skips over code
    By willc0de4food in forum C Programming
    Replies: 9
    Last Post: 11-16-2006, 06:38 PM
  4. First scanf() skips next scanf() !
    By grahampatten in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:47 AM
  5. my program skips scanf and getchar()
    By jk81 in forum C Programming
    Replies: 15
    Last Post: 11-29-2002, 05:54 PM

Tags for this Thread