Thread: invalid input

  1. #16
    Registered User
    Join Date
    Sep 2010
    Posts
    33
    Hi Whiteflags! Lol, I’m working with Elysia’s code, which I got compiled at last.

    The code I posted (#3) was just to show a fellow beginner programmer that there are alternatives you can mess around with. But I wasn’t suggesting this as a serious practise. Entering an overly large number would cause some problems too.

    Input using string, validating string and then converting using stringstreams seems to be the most solid approach.

    Hi Elysia! Looking forward to that tutorial, or at least a hint, because I haven’t as yet managed to work out how to use it, though I am learning about namespaces, which is good.

    Thank you everyone; everything is useful.

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    So basically there are two functions that you should be concerned about:
    Read and ReadNoVal.
    Read takes a number of arguments and comes in several flavors.
    Now, here is the general structure of how the function works:

    - Print question.
    - Retrieve input.
    - Convert input to appropriate data type.
    - If fail, print error message, goto beginning.
    - Verify input by calling verification function.
    - If verification function returns false, print error message, goto beginning.
    - Everything works and the function returns.

    Essentially, you call the function once to get some valid input. When the function returns, you have a valid input.

    The argument the read function takes are:

    - Reference to the variable to hold the input data.
    - A string that contains the questions to print out before getting input.
    - A string to print if the input cannot be converted into appropriate type.
    - A string to print if the input doesn't pass the verification.
    - A validator functor.

    The strings can be of any type that can be printed. It can be std::string, const char*, etc. So long as they can be passed to std::cout or std::wcout. This opens up possibilities for your own string types, or boost's string types.

    The validator function is a pointer to a function or a functor that returns a bool and accepts a reference to the converted input. It shall return true if the data is valid; false otherwise.

    Note that the strings for valid inputs can be omitted, in which case, "Invalid input!" will be printed instead.

    ReadNoVal is the same as Read, except it does not take a validation function. So that means if the input can be converted into the proper data type, the function returns.

    The data type to convert to is determined by the type of the variable to hold the answer.

    So, for example, say that we want an integer in the range of 100 - 200. We could write:
    Code:
    int Num;
    Stuff::Input::Read(Num, "Enter a number between 100 and 200: ", "That is not a number!",
        "The number must be within the range 100 - 200!",
        [](int Number) -> bool { return Number >= 100 && Number <= 200; });
    Assuming I haven't made any mistakes, this should compile.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I'm surprised something you want other people to use actually comes in a namespace called Stuff and not, oh, ElysiaWroteMe. It just seems like a dubiously available name.

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's a name I chose long, long, long ago, and I see no reason to change it. This code comes from my static library of code that I keep around. Essentially everytime I do something new, I integrate that code in an easier form into the "Stuff" library.
    If I wanted people to know I wrote it, I could just add a copyright. But I don't see the point. It's code that I willfully share.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Registered User
    Join Date
    Sep 2010
    Posts
    33
    Thank you for the explanation. It all works fine now.

    Of course I don’t wish to use this code. I just want to understand it so I can write my own, tailored to my own needs.

    Thanks for sharing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IDE for C embedded advanced editing
    By Undici77 in forum Tech Board
    Replies: 32
    Last Post: 01-16-2010, 05:17 PM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. input files in C
    By LWisne in forum C Programming
    Replies: 4
    Last Post: 09-30-2002, 06:24 AM