Thread: sort of stuck

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    45

    sort of stuck

    hi all

    right now i got this code

    if (scanf("%d %d", &year, &month)!=2) {
    printf("Input error\n");
    }

    it sort of behaves how i wanted it to like when i didnt enter 2 integers will pop out input error
    however if i enter 1 integer the program will be stuck and just appear normal until i enter something twice

    how do i fix it so that it will print input error if i enter less than 2 integer?

    thx

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    One obvious solution, if you expect the user to input all the data on one line is to use a text input, and then "parse" the text input for the relevant data. For example:
    Code:
    char buffer[20];
    ...
    if (fgets(buffer, sizeof buffer, stdin) != NULL || (sscanf(buffer, "%d %d", &year, &month)!=2) {
    printf("Input error\n");
    }
    This will make sure that one line of data is input, and then try to translate it to year and month - and if either fails for some reason, it will produce "input error".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    45
    thx for the reply mats
    but i think this fgets and buffer is way too complicated for this program (we are only allowed to use what we learn)
    are there any other simpler methods?
    Last edited by bobbie18; 04-04-2008 at 04:21 PM.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Issue two scanf()'s, one for year, and the second for month.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    45
    thx for th reply todd but i dunno y it still doesnt work... if i enter a year but no month like XXXX -
    (- means nothing) program wont appear normal until i enter something
    the output of program will be like this
    Enter year and month: 2008


    1<---- random thing on keyboard
    twenty hundred eight
    has three hundred and sixty six (366) days
    January has 31 days

    Code:
    if (scanf("&#37;d", &year)!=1) {
    printf("Input error\n");
    }
    
    if (scanf("%d", &month)!=1) {
    printf("Input error\n");
    }

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, if you use fgets(), it will read a line. It's not very complicated really, and there is no other (simple) solution that works.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  2. threaded merge sort
    By AusTex in forum Linux Programming
    Replies: 4
    Last Post: 05-04-2005, 04:03 AM
  3. Sorting
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-10-2003, 05:21 PM
  4. radix sort and radix exchange sort.
    By whatman in forum C Programming
    Replies: 1
    Last Post: 07-31-2003, 12:24 PM
  5. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 08:05 PM