Integer input

Discussion in 'C++' started by Pycco, Oct 24, 2004.

  1. Pycco

    Pycco Guest

    I need the user to input an integer, and do some calculation with that
    integer. Should the user input an invalid integer, I must return a
    warning, saying it's an invalid input.

    E.g.

    int a;
    cin >> a;
    if (cin.fail())
    cout << "Invalid input.";
    else (...)

    If the user inputs "four" instead of "4", it'll say "Invalid input".
    But if the user inputs "4 + 0", the program won't enter in fail state,
    and accept "4" as the value of "a", when it should say "Invalid
    input."

    What should I do in order to warn the user that "4 + 0" instead of "4"
    is also an invalid input?

    Thx!
    Pycco, Oct 24, 2004
    #1
    1. Advertising

  2. "Pycco" <> wrote...
    >I need the user to input an integer, and do some calculation with that
    > integer. Should the user input an invalid integer, I must return a
    > warning, saying it's an invalid input.
    >
    > E.g.
    >
    > int a;
    > cin >> a;
    > if (cin.fail())
    > cout << "Invalid input.";
    > else (...)
    >
    > If the user inputs "four" instead of "4", it'll say "Invalid input".
    > But if the user inputs "4 + 0", the program won't enter in fail state,
    > and accept "4" as the value of "a", when it should say "Invalid
    > input."
    >
    > What should I do in order to warn the user that "4 + 0" instead of "4"
    > is also an invalid input?


    You should read the whole string, then try converting and see if you
    have any left-overs in the string after conversion. If you do, it's
    invalid. If you use up the whole string, it's valid.

    Victor
    Victor Bazarov, Oct 24, 2004
    #2
    1. Advertising

  3. Pycco wrote:

    > What should I do in order to warn the user that "4 + 0" instead of "4"
    > is also an invalid input?


    I have this tiny template that can be useful:

    template <typename T>
    std::istream & lineread (std::istream & is, T & value)
    {
    std::string line;
    getline (is, line);
    if (is)
    {
    std::istringstream iss (line);
    iss >> value;
    if (! iss)
    is.clear (std::ios::badbit);
    else
    {
    char c;
    iss >> c;
    if (! iss.eof () )
    is.clear (std::ios::badbit);
    }
    }
    return is;
    }

    To use it:


    int i;
    lineread (std::cin, i);

    --
    Salu2
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Oct 24, 2004
    #3
  4. "Pycco" <> wrote in message
    news:...
    >I need the user to input an integer, and do some calculation with that
    > integer. Should the user input an invalid integer, I must return a
    > warning, saying it's an invalid input.
    >
    > E.g.
    >
    > int a;
    > cin >> a;
    > if (cin.fail())
    > cout << "Invalid input.";
    > else (...)
    >
    > If the user inputs "four" instead of "4", it'll say "Invalid input".
    > But if the user inputs "4 + 0", the program won't enter in fail state,
    > and accept "4" as the value of "a", when it should say "Invalid
    > input."


    It is a normal behavior. The operator>> will read a string until the first
    whitespace is reached. In your example it is the string "4" and it converts
    it successfully to int.

    >
    > What should I do in order to warn the user that "4 + 0" instead of "4"
    > is also an invalid input?


    I didn't try it to see it working, but you might read the whole line (use
    std::getline function) and then parse it to implement additional rules that
    you need.

    Catalin
    Catalin Pitis, Oct 25, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9l?=

    CType(x,Integer) vs. Integer.Parse(x)

    =?Utf-8?B?Sm9l?=, Feb 6, 2006, in forum: ASP .Net
    Replies:
    7
    Views:
    5,950
    =?Utf-8?B?RGF2aWQgQW50b24=?=
    Feb 7, 2006
  2. =?ISO-8859-1?Q?Thomas_Gagn=E9?=

    No Math.min(Integer, Integer)?

    =?ISO-8859-1?Q?Thomas_Gagn=E9?=, Jul 29, 2003, in forum: Java
    Replies:
    0
    Views:
    503
    =?ISO-8859-1?Q?Thomas_Gagn=E9?=
    Jul 29, 2003
  3. Sebastian Stelzer

    How do I add an Integer to another Integer?

    Sebastian Stelzer, Oct 14, 2004, in forum: Java
    Replies:
    2
    Views:
    491
    Yu SONG
    Oct 15, 2004
  4. Sebastian Stelzer

    How do I add an Integer to another Integer?

    Sebastian Stelzer, Oct 14, 2004, in forum: Java
    Replies:
    6
    Views:
    45,407
    JavaBean2010
    Apr 7, 2010
  5. Johannes Zellner
    Replies:
    22
    Views:
    3,599
Loading...

Share This Page