testing input values

Discussion in 'C++' started by TechNovice, Oct 16, 2003.

  1. TechNovice

    TechNovice Guest

    Hi:

    I'm trying to find a way to test input values.
    To test an integer I tried this code:
    ******Code******
    int input_number;
    cin>>input_number;
    while(!input_number)
    cout<<"invalid input"<<endl;
    ******End Code******
    when I enter a character or a string I don't get the error message. I would
    appreciate if someone could help me solve this problem.

    Thanks.
     
    TechNovice, Oct 16, 2003
    #1
    1. Advertising

  2. TechNovice

    Mike Wahler Guest

    "TechNovice" <> wrote in message
    news:yjmjb.91087$k74.57013@lakeread05...
    > Hi:
    >
    > I'm trying to find a way to test input values.
    > To test an integer I tried this code:
    > ******Code******
    > int input_number;
    > cin>>input_number;
    > while(!input_number)


    Test the stream, not the int object. If the read
    failed, the int object was not affected.

    while(!cin)
    {

    > cout<<"invalid input"<<endl;


    // then before we can read more data we must
    // reset the stream state:

    cin.clear();

    // and extract and discard the offending characters:
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    'numeric_limits' is declared by <limits>
    'streamsize' is declared by <ios>

    > ******End Code******
    > when I enter a character or a string I don't get the error message. I

    would
    > appreciate if someone could help me solve this problem.


    HTH,
    -Mike
     
    Mike Wahler, Oct 16, 2003
    #2
    1. Advertising

  3. TechNovice

    Mike Wahler Guest

    Re: (corr) testing input values

    "Mike Wahler" <> wrote in message
    news:rJmjb.2562$...
    >
    > "TechNovice" <> wrote in message
    > news:yjmjb.91087$k74.57013@lakeread05...
    > > Hi:
    > >
    > > I'm trying to find a way to test input values.
    > > To test an integer I tried this code:
    > > ******Code******
    > > int input_number;
    > > cin>>input_number;
    > > while(!input_number)

    >
    > Test the stream, not the int object. If the read
    > failed, the int object was not affected.


    Not always true. e.g.

    Enter an int: 123X

    123 is assigned, and the stream goes into 'fail' state
    (!cin returns true) when it encounters the 'X' . So you
    would get the int changed but not necessarily to what you
    expected. Perhaps you meant to type 1234.

    -Mike
     
    Mike Wahler, Oct 16, 2003
    #3
  4. TechNovice

    TechNovice Guest

    Thanks for your response, but I found that this works very well
    ****code****
    int input_number;
    if(cin>>input_number)
    cout<<"valid number"<<endl;
    else
    cout<<"invalid number"<<endl;
    ****end of code****
    "Mike Wahler" <> wrote in message
    news:rJmjb.2562$...
    >
    > "TechNovice" <> wrote in message
    > news:yjmjb.91087$k74.57013@lakeread05...
    > > Hi:
    > >
    > > I'm trying to find a way to test input values.
    > > To test an integer I tried this code:
    > > ******Code******
    > > int input_number;
    > > cin>>input_number;
    > > while(!input_number)

    >
    > Test the stream, not the int object. If the read
    > failed, the int object was not affected.
    >
    > while(!cin)
    > {
    >
    > > cout<<"invalid input"<<endl;

    >
    > // then before we can read more data we must
    > // reset the stream state:
    >
    > cin.clear();
    >
    > // and extract and discard the offending characters:
    > cin.ignore(numeric_limits<streamsize>::max(), '\n');
    >
    > 'numeric_limits' is declared by <limits>
    > 'streamsize' is declared by <ios>
    >
    > > ******End Code******
    > > when I enter a character or a string I don't get the error message. I

    > would
    > > appreciate if someone could help me solve this problem.

    >
    > HTH,
    > -Mike
    >
    >
     
    TechNovice, Oct 16, 2003
    #4
  5. TechNovice

    TechNovice Guest

    that is true.
    Is there a function to test for int, or char and fail when there is a
    mixture of both types?
    I need a function to check for numeric values anf fails when I enter a char
    value or accepts only char values and fail when I enter int values.

    "TechNovice" <> wrote in message
    news:CNmjb.91099$k74.71902@lakeread05...
    > Thanks for your response, but I found that this works very well
    > ****code****
    > int input_number;
    > if(cin>>input_number)
    > cout<<"valid number"<<endl;
    > else
    > cout<<"invalid number"<<endl;
    > ****end of code****
    > "Mike Wahler" <> wrote in message
    > news:rJmjb.2562$...
    > >
    > > "TechNovice" <> wrote in message
    > > news:yjmjb.91087$k74.57013@lakeread05...
    > > > Hi:
    > > >
    > > > I'm trying to find a way to test input values.
    > > > To test an integer I tried this code:
    > > > ******Code******
    > > > int input_number;
    > > > cin>>input_number;
    > > > while(!input_number)

    > >
    > > Test the stream, not the int object. If the read
    > > failed, the int object was not affected.
    > >
    > > while(!cin)
    > > {
    > >
    > > > cout<<"invalid input"<<endl;

    > >
    > > // then before we can read more data we must
    > > // reset the stream state:
    > >
    > > cin.clear();
    > >
    > > // and extract and discard the offending characters:
    > > cin.ignore(numeric_limits<streamsize>::max(), '\n');
    > >
    > > 'numeric_limits' is declared by <limits>
    > > 'streamsize' is declared by <ios>
    > >
    > > > ******End Code******
    > > > when I enter a character or a string I don't get the error message. I

    > > would
    > > > appreciate if someone could help me solve this problem.

    > >
    > > HTH,
    > > -Mike
    > >
    > >

    >
    >
     
    TechNovice, Oct 16, 2003
    #5
  6. TechNovice

    Mike Wahler Guest

    "TechNovice" <> wrote in message
    news:CNmjb.91099$k74.71902@lakeread05...
    > Thanks for your response, but I found that this works very well
    > ****code****
    > int input_number;
    > if(cin>>input_number)
    > cout<<"valid number"<<endl;
    > else
    > cout<<"invalid number"<<endl;


    Yes, it does. This is because the expression 'cin>> input_number'
    returns a reference to 'cin', so has the same testing effect
    as 'if(cin)'. Indeed what you just showed me is the 'idomatic'
    way to use the >> operator.

    But now try it in a loop as in your original post. :)

    (or simply try a subsequent 'cin' operation without
    first doing the 'clear()' and 'ignore()')

    -Mike
     
    Mike Wahler, Oct 16, 2003
    #6
  7. TechNovice

    Mike Wahler Guest

    "TechNovice" <> wrote in message
    news:jWmjb.91103$k74.27929@lakeread05...
    > that is true.
    > Is there a function to test for int, or char and fail when there is a
    > mixture of both types?


    No, but one can be written.

    > I need a function to check for numeric values anf fails when I enter a

    char
    > value


    OK we've done this part already.

    >or accepts only char values and fail when I enter int values.


    Think about this for a moment. The digits '1', '2', '3'
    you type on your keyboard *are* characters. So of course
    a character input routine will not reject them. But if
    you do want do specify certain restrictions on a character
    input, you can write the code to do so.

    Also, you said "a function". Which type of data is being
    input? It seems you're wanting to input two different
    types at once. This doesn't make sense to me.

    I think you need a function for numeric input
    (which you have), and another for restrictive
    character input:

    #include <iostream>
    #include <string>

    void get_text(std::string& input, bool allow_digits = false)
    {
    do
    {
    std::cout << "Enter text "
    << (allow_digits
    ? ""
    : "(no digits allowed)") << ": ";

    std::getline(std::cin, input);
    } while(!allow_digits &&
    input.find_first_of("01234567890") != std::string::npos);
    }

    int main()
    {
    std::string s;

    get_text(s, false);
    std::cout << "You entered: " << s << '\n';

    get_text(s, true);
    std::cout << "You entered: " << s << '\n';

    return 0;
    }


    BTW please don't top-post. Thanks.

    -Mike
     
    Mike Wahler, Oct 16, 2003
    #7
  8. TechNovice

    TechNovice Guest

    Thank you.
    I also came up with another solution if anyone cares :)
    #include <cstring>
    #include <cctype>

    string input_number; //numeric data
    cin>>input_number;
    while(!valid(input_number)) //validate input
    cin>>input_number;

    //valid function
    bool valid(string input_number)
    {
    for(int i=0; i<x.length(); i++)
    {
    if(isdigit(x.at(i)) == 0) //checks everything in the string
    return false;
    }
    return true;
    }
    "Mike Wahler" <> wrote in message
    news:15qjb.2612$...
    >
    > "TechNovice" <> wrote in message
    > news:CNmjb.91099$k74.71902@lakeread05...
    > > Thanks for your response, but I found that this works very well
    > > ****code****
    > > int input_number;
    > > if(cin>>input_number)
    > > cout<<"valid number"<<endl;
    > > else
    > > cout<<"invalid number"<<endl;

    >
    > Yes, it does. This is because the expression 'cin>> input_number'
    > returns a reference to 'cin', so has the same testing effect
    > as 'if(cin)'. Indeed what you just showed me is the 'idomatic'
    > way to use the >> operator.
    >
    > But now try it in a loop as in your original post. :)
    >
    > (or simply try a subsequent 'cin' operation without
    > first doing the 'clear()' and 'ignore()')
    >
    > -Mike
    >
    >
     
    TechNovice, Oct 16, 2003
    #8
  9. TechNovice escribió:

    > bool valid(string input_number)
    > {
    > for(int i=0; i<x.length(); i++)
    > {
    > if(isdigit(x.at(i)) == 0) //checks everything in the string
    > return false;
    > }
    > return true;
    > }


    You can do also:

    if (x.find_first_not_of ("0123456789") != string::npos)
    return false;

    But I suspect input_number will be used instead of x ;)

    Regards.
     
    =?iso-8859-1?Q?Juli=E1n?= Albo, Oct 16, 2003
    #9
  10. TechNovice

    Mike Wahler Guest

    "Julián Albo" <> wrote in message
    news:...
    TechNovice escribió:

    > bool valid(string input_number)
    > {
    > for(int i=0; i<x.length(); i++)
    > {
    > if(isdigit(x.at(i)) == 0) //checks everything in the string
    > return false;
    > }
    > return true;
    > }


    You can do also:

    if (x.find_first_not_of ("0123456789") != string::npos)
    return false;

    But I suspect input_number will be used instead of x ;)

    Regards.
     
    Mike Wahler, Oct 16, 2003
    #10
  11. TechNovice

    Mike Wahler Guest

    "Julián Albo" <> wrote in message
    news:...
    > You can do also:


    > if (x.find_first_not_of ("0123456789") != string::npos)


    which is exactly the inverse of what I showed him for
    prohibiting digits in input text. :)

    -Mike
     
    Mike Wahler, Oct 16, 2003
    #11
  12. Mike Wahler escribió:

    > > You can do also:
    > > if (x.find_first_not_of ("0123456789") != string::npos)

    >
    > which is exactly the inverse of what I showed him for
    > prohibiting digits in input text. :)


    Mmm... seems I don't pay attention to the complete thread.

    By the way, the method the OP proposes does not mark as invalid an empty
    string.

    Regards.
     
    =?iso-8859-1?Q?Juli=E1n?= Albo, Oct 16, 2003
    #12
    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. Brian van den Broek
    Replies:
    3
    Views:
    863
    Jeremy Bowers
    Apr 3, 2005
  2. Brian van den Broek
    Replies:
    2
    Views:
    398
    Brian van den Broek
    Apr 5, 2005
  3. Chris

    Testing and Load Testing

    Chris, Mar 4, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    336
  4. John Maclean
    Replies:
    1
    Views:
    351
    Martin P. Hellwig
    Apr 13, 2010
  5. Avi
    Replies:
    0
    Views:
    500
Loading...

Share This Page