Thread: Error handling for multiple inputs at once

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    2

    Error handling for multiple inputs at once

    Greetings, working on some basic code here.

    Usually I run the following code to test input as non-integers or negatives, as most of my programs use positive integers.

    Code:
    while (scanf("%f", &input) != 1 ? scanf("%s") : 0 || input< 0)    {
            printf("Error! ");
            printf("Please enter input: ");
        }
    However, I can't seem to apply that same principle to multiple inputs at once...
    Code:
    printf("Enter a date (mm/dd/yy): ");
    
    while (scanf("%d/%d/%d", &m2, &d2, &y2) != 1 ? scanf("%s") : m2 < 0 || d2 < 0 || y2 < 0)
        {
            printf("Error! ");
            printf("Enter a date (mm/dd/yy): ");
        }
    Does anyone know a good way to solve this problem?

    The entire point of this program is to determine the earliest date out of however many dates the user inputs.

    Full code here: [C] c06p10.c - Pastebin.com

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,404
    You forgot to change the number of expected assignments, i.e., scanf("%d/%d/%d", &m2, &d2, &y2) != 1 should be: scanf("%d/%d/%d", &m2, &d2, &y2) != 3
    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 2015
    Posts
    2
    Oh wow! Can't believe I missed that. Thank you!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,670
    > scanf("%s")
    And where do you suppose this is going to write a string?
    Or for that matter, what the return result is.

    Try pressing ctrl-d (on Unix/Linux) or ctrl-z (dos/windows) when you're supposed to type something and see what happens.


    Code:
    char buff[BUFSIZ];
    while ( fgets(buff,BUFSIZ,stdin) != NULL ) {
      if ( sscanf(buff,"%d/%d/%d", &m2, &d2, &y2) == 3 ) {
        // success
        break;
      } else {
        // error
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanning multiple inputs
    By casey123 in forum C Programming
    Replies: 2
    Last Post: 10-17-2011, 01:47 AM
  2. Multiple inputs at one line?
    By farukyaz in forum C Programming
    Replies: 4
    Last Post: 04-10-2011, 03:41 PM
  3. multiple inputs of multiple types
    By frontz in forum C Programming
    Replies: 8
    Last Post: 01-19-2004, 02:57 PM
  4. Multiple inputs with cin
    By DJPG5 in forum C++ Programming
    Replies: 2
    Last Post: 02-16-2003, 04:10 PM
  5. Getting multiple inputs using cin
    By edk in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2001, 02:34 PM

Tags for this Thread