E
eli m
In my program i ask for input and then i want it to display a certain message when they enter anything except numbers. How would i do this?
eli m said:In my program i ask for input and then i want it to display a
certain message when they enter anything except numbers. How
would i do this?
The way I have been taught is:In my program i ask for input and then i want it to display a certain message when they enter anything except numbers. How would i do this?
In my program i ask for input and then i want it to display a certain
message when they enter anything except numbers. How would i do this?
Juha Nieminen said:In general, parsing textual input is not one of the easiest things to
do in C++.
To parse an input, one first need to know its grammar. Once
the grammar is written, writing a parser is straightforward. ....
Even if C++ would provide more support for parsing (for
example, with libraries like YACC or LAX), one still would
need to know the grammar to be used.
(You mean lex, not LAX, I think.)
Actually C++ /does/ support parsing today, via its regex support.
And you're right of course: the language cannot do your interface
design for you.
Regular expressions are best for matching and replacing. They are a rather
poor tool for parsing. You could try to parse some constructs with regular
expressions, but many such constructs become cumbersome, if not even
impossible, to parse with regexes. (Regular expressions have their limits
on what they support. For example, it's not possible to write a regex that
says "a substring with as many '(' characters as ')' characters", which
could be used to parse expressions with nested parentheses.)
Regular expressions are best for matching and replacing. They are a rather
poor tool for parsing.
On 3/9/2013 3:39 PM, eli m wrote:
The way I have been taught is:
int i = 0;
if (!cin >> i)
cout << "error";
Regular expressions are best for matching and replacing. They are a rather
poor tool for parsing. You could try to parse some constructs with regular
expressions, but many such constructs become cumbersome, if not even
impossible, to parse with regexes. (Regular expressions have their limits
on what they support. For example, it's not possible to write a regex that
says "a substring with as many '(' characters as ')' characters", which
could be used to parse expressions with nested parentheses.)
True, they don't replace e.g. yacc or even lex.
Lex *is* regular expressions.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.