CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Simple... TOO simple

    I have built a very simple Parse class to detect whether a users input can be input into an integer variable or not. Here is the code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    class inputParser
    {
    public:
    	bool IntParse(istream& _stream1, int& _int1)
    	{
    			_stream1 >> _int1;
    			if (_stream1.fail() || _stream1.bad())
    				return false;
    			else
    				return true;
    	}
    };
    
    int main()
    {
    	int someInt;
    	inputParser Input;
    
    	cout << "Input an integer: ";
    	if (Input.IntParse(cin, someInt))
    		cout << "Is an integer";
    	else
    		cout << "Is NOT an integer";
    	system("pause");
    	return 0;
    }
    This code seems WAY too simple though, but it seems to work just fine... Am I missing something here or is this just how simple it should be?
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  2. #2
    Join Date
    Jun 2007
    Posts
    5

    Re: Simple... TOO simple

    If you just want to check if it's a number, why not use std::isdigit() from cctype?

  3. #3
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: Simple... TOO simple

    Well I wanted it to put the number into the variable as well. This class function does that. Its kinda like C#'s Int16.TryParse() boolean function.
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: Simple... TOO simple

    Unfortunately, your code will fail for input like this:

    12ttt

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Simple... TOO simple

    Quote Originally Posted by RaleTheBlade
    Well I wanted it to put the number into the variable as well. This class function does that. Its kinda like C#'s Int16.TryParse() boolean function.
    Instead of using C# as a model, why not just do the obvious that any C or C++ programmer would do.

    1) Get the input.
    2) Call one of the multiple functions to test if the number is really an integer.

    For 1), you read the data as a string.

    For 2), you have strtol, strtoul, etc. that check if the number is an integer, converts it to an int, and if the number isn't an int, will report where the parse fails.

    The problem of saying "this function exists in language X, so I'll write the same thing in C++" will in many cases cause you to write unnecessary code and reinvent the wheel.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    May 2007
    Location
    Bangalore India
    Posts
    262

    Re: Simple... TOO simple

    I think this code will give correct result.

    and it will not fail even for 12ttt...

    In this case it will simply return false..

  7. #7
    Join Date
    Jun 2006
    Posts
    437

    Re: Simple... TOO simple

    Hi all.

    A part from it works or not, at the moment what you have done is a "function that pretends to be a class"; in your class there isn't data members, only methods, so it doesn't implement a real object. It isn't a class, but a simple collection of procedures grouped into a class.
    In my opinion this isn't a good development principle. It's easier, and more correct, to write a function that checks if a value is an integer; better, to use a function that exists yet.

  8. #8
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: Simple... TOO simple

    Quote Originally Posted by code_carnage
    I think this code will give correct result.

    and it will not fail even for 12ttt...

    In this case it will simply return false..

    What compiler are you using ? if you enter 12tt for in int variable
    using operator >>, the result should be the following:

    a) the int variable is given a value of 12
    b) the stream position points at the first 't'

  9. #9
    Join Date
    Sep 2005
    Location
    New Delhi, India
    Posts
    332

    Re: Simple... TOO simple

    One way to validate input for integers
    Code:
    #include <iostream>
    #include <limits>
     
    using namespace std;
     
    int main() {
      int number = 0;
      cout << "Enter an integer: ";
      cin >> number;
      cin.ignore(numeric_limits<int>::max(), '\n');
     
      if (!cin || cin.gcount() != 1)
        cout << "Not a numeric value.";
      else
        cout << "Your entered number: " << number;
      return 0;
    }
    Appreciate others by rating good posts

    "Only buy something that you'd be perfectly happy to hold if the market shut down for 10 years." - Warren Buffett

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured