|
-
August 26th, 2007, 02:50 PM
#1
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
-
August 26th, 2007, 03:33 PM
#2
Re: Simple... TOO simple
If you just want to check if it's a number, why not use std::isdigit() from cctype?
-
August 26th, 2007, 04:33 PM
#3
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
-
August 26th, 2007, 04:59 PM
#4
Re: Simple... TOO simple
Unfortunately, your code will fail for input like this:
12ttt
-
August 26th, 2007, 05:38 PM
#5
Re: Simple... TOO simple
 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
-
August 27th, 2007, 04:37 AM
#6
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..
-
August 27th, 2007, 05:20 AM
#7
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.
-
August 27th, 2007, 06:36 AM
#8
Re: Simple... TOO simple
 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'
-
August 28th, 2007, 08:38 AM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|