Thread: how to create an error message?

  1. #1
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105

    how to create an error message?

    In a program,I am giving input as two different time periods(HH::MM::SS) and getting the output as the total time.
    Suppose if the inputs are:
    1::30::40
    2::40::40
    then output is 4::11::20.
    Now the question I want to ask is during the input if the HH is typed wrongly as
    #::30::40, then how to create an error message fo this?

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    I suppose you use scanf?

    int a, b, c;
    int x = scanf("%d::%d::%d", &a, &b, &c);

    scanf() returns the number of input fields successfully scanned, converted, and stored,
    x will be given 3 on success.
    If you want to check each value separately you can use multiple scanf()s

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The problem with just using scanf() is what to do with the remainder "::30::40" which is still in the input buffer.

    If you use fgets() with a reasonable buffer size (BUFSIZ is a good choice), then this little problem is nicely stepped over.
    You then use sscanf() to have a go at parsing the input from the memory buffer. You even have the possibility of having several attempts, if you have any kind of "optional" interpretation of the input.
    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.

  4. #4
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    Quote Originally Posted by kmdv View Post
    I suppose you use scanf?

    int a, b, c;
    int x = scanf("%d::%d::%d", &a, &b, &c);

    scanf() returns the number of input fields successfully scanned, converted, and stored,
    x will be given 3 on success.
    If you want to check each value separately you can use multiple scanf()s
    hmmm, this works quite right.So I can put a condition to check if the return value of scanf() is <3 which will indicate if scanf() is successful.
    But is there any another way which checks the int individually?

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Yes, using multiple scanf()s:

    Code:
    int input;
    int result;
    result = scanf("%d", &input);
    // check if == 1
    result = scanf("::%d", &input);
    // check if == 1
    result = scanf("::%d", &input);
    // check if == 1


    And make a function that compares and prints an error.

    EDIT: use it with fgets as Salem said and replace 'scanf' with 'sscanf(buf, '
    Last edited by kmdv; 08-01-2010 at 11:30 AM.

  6. #6
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    ok....thanks!!!I need to use scanf() only to keep it short and simple.And it works fine.Thanks again for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't create child windows
    By OnionKnight in forum Windows Programming
    Replies: 4
    Last Post: 04-10-2011, 04:13 PM
  2. Cannot create shared memory
    By Phoenix_Rebirth in forum C Programming
    Replies: 3
    Last Post: 11-07-2008, 11:32 AM
  3. Create new combo boxes based on items selected in preview combo box
    By RealityFusion in forum Windows Programming
    Replies: 2
    Last Post: 01-10-2007, 09:50 AM
  4. Create a file from c++ program
    By Dan17 in forum C++ Programming
    Replies: 2
    Last Post: 05-08-2006, 04:25 PM
  5. How to Create reference to an array in C++
    By shiv_tech_quest in forum C++ Programming
    Replies: 2
    Last Post: 12-20-2002, 10:01 AM