![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
How to determine whether a string is a number
I need a way to determine whether the user has entered a floating point number. I know I how to convert a string to a double using stringstreams or sscanf, but what happens if the string isn't a number?
|
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
It won't convert. Depending upon the particular method employed, it'll either generate an error or stop short of consuming all the input. Are you checking for these important things?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Expert Programmer
|
I'm trying to catch the error when using a stringstream, but it doesn't seem to work. As far as I can tell, result simply becomes a large random number. What am I doing wrong?
istringstream ss("not a number");
double result;
try {
ss >> result;
} catch (exception e) {
// This code never gets executed!
} |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
Read up on streams. Exceptions are not generally thrown for minor conversion errors on I/O in any programming language (unless you use Java, because that library is poorly designed). Conversion errors affect the state of the stream until you clear them. Look up member functions and operators offered by istringstream and its base classes (particularly basic_ios, which is - directly or indirectly - a base class of most stream classes) and you will find some that do what you want.
|
|
|
|
|
|
#5 | |||
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Beat me to it.
Quote:
Quote:
Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|||
|
|
|
|
|
#6 |
|
Expert Programmer
|
Ok, I now have the following:
string in_str("3.2 46"); // sample input
stringstream ss(in_str);
float d1, d2;
ss >> d1 >> d2;
if (!ss.fail() && ss.eof())
cout << "Valid input detected."';
else
cout << "Please enter two numeric values, floating point or otherwise.";
ss.clear() |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I think you've overlooked a point. The extraction operator is not the only extraction method available for streams. Re-read my second quote. A few zillion people, some of them programmers for the standard library, have encountered the very same problems and considered the very same issues.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#8 |
|
Expert Programmer
|
Your second quote seems to suggest using some form of tokenizer, which is probably good advice.
Regardless, I was finally able to solve my problem using MFC CString functions. Thanks anyway. |
|
|
|
|
|
#9 |
|
Professional Programmer
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3
![]() |
I know you have solved your problem, but maybe this will be of some help.
Generally the way I handle user input is to make a regular expression that defines valid input, and then use my favorite string parsing functions (or the regular expression itself) to pull the needed parts of the string out and then covert the values. I use boost::lexical_cast, which internally uses iostreams to covert between values, to go from string to number or vice versa. You don't have to worry about the conversion failling because you report an error to the user if the regular expression doesn't match the whole string. Btw, DaWei's second quote says you could use a tokenizer, or just some special string functions, which is probably what you did with CString (how I hate hungarian notation). |
|
|
|
|
|
#10 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
Quote:
It is quite possible to do what you want portably with basic std::strings, with stringstreams, with basic functions inherited by C++ from C like sscanf(), and many other ways. All of those approaches are portable. The trick is to define what correct and incorrect input is. Let's say your requirement is for two integers separated by a colon, the the generic approach is to attempt to read a string, read a character, then read an integer. The basic logic you then need to follow is then; 1) Attempt to read an integer 2) Attempt to read a character 3) Attempt to read an integer If an error occurs at any of these three points, the string is not in the required format, so there is no need to perform subsequent steps. Examples of conditions that will be an error for the first step are no data present or an error converting whatever data there is to an integer. Examples of conditions that you probably need to report as an error in the second step will be no data available (eg at the end of the string) or that the character there is not a colon. Tokenisers allow you to program the above sort of logic, with some variations and constraints. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| C# corruption!!! | Kilo | C++ | 32 | May 21st, 2006 9:44 PM |
| Array issues :( | Alo Tsum | Java | 10 | Nov 26th, 2005 6:45 PM |
| FiveDigit + RandomeNumber Game. | TecBrain | Java | 0 | Nov 18th, 2005 3:53 PM |
| replace space with ' * ' | TecBrain | C++ | 15 | Apr 13th, 2005 1:32 PM |
| function | solomon_13000 | Java | 6 | Apr 3rd, 2005 12:42 AM |