By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,419 Members | 1,179 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,419 IT Pros & Developers. It's quick & easy.

std::cin woes ---- C style macros to the rescue

P: n/a
Sick and tired of the problems that cin was giving me in a menu-driven
CLI application, that accepts numerical inputs (I was getting into an
infinite loop when a non-numerical input was entered), I have gone
ahead and defined the following macro:
#define readStdin(x) while(true) \
{ \
cin >> x; \
if(!cin) \
{ \
cin.clear(); \
cin.ignore(MAX_LINE_LEN, '\n'); \
} \
else \
break; \
}

This macro has been working like a charm for me. Any other
suggestions are welcome. (Yes I know that I can read the line into a
buffer and read input from that buffer, but that's not what I want to
do).

Regards,
KP Bhat
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Tue, 02 Dec 2003 11:47:42 -0800, Generic Usenet Account wrote:
Sick and tired of the problems that cin was giving me in a menu-driven
CLI application, that accepts numerical inputs (I was getting into an
infinite loop when a non-numerical input was entered), I have gone
ahead and defined the following macro:
#define readStdin(x) while(true) \
{ \
cin >> x; \
if(!cin) \
{ \
cin.clear(); \
cin.ignore(MAX_LINE_LEN, '\n'); \
} \
else \
break; \
}

This macro has been working like a charm for me. Any other
suggestions are welcome. (Yes I know that I can read the line into a
buffer and read input from that buffer, but that's not what I want to
do).


I prefer to avoid macros where possible. I'd write it as a function
template:

template <typename T>
void readStdin(T& x)
{
....
}

Perhaps there ought to be a return value indicating success/failure.

Jul 22 '05 #2

P: n/a
Generic Usenet Account wrote:
Sick and tired of the problems that cin was giving me in a menu-driven
CLI application, that accepts numerical inputs (I was getting into an
infinite loop when a non-numerical input was entered), I have gone
ahead and defined the following macro:
#define readStdin(x) while(true) \
{ \
cin >> x; \
if(!cin) \
{ \
cin.clear(); \
cin.ignore(MAX_LINE_LEN, '\n'); \
} \
else \
break; \
}

This macro has been working like a charm for me. Any other
suggestions are welcome. (Yes I know that I can read the line into a
buffer and read input from that buffer, but that's not what I want to
do).


So what is your question?

Take a deep breath and tell us what's wrong.
Post a [short] complete program that demonstrates the problem
then show us the error messages that you got from your compiler
and any diagnostic messages that you got when you tried to run it.

Jul 22 '05 #3

P: n/a
E. Robert Tisdale wrote in news:3F**************@jpl.nasa.gov:
Generic Usenet Account wrote:
Sick and tired of the problems that cin was giving me in a menu-driven
CLI application, that accepts numerical inputs (I was getting into an
infinite loop when a non-numerical input was entered), I have gone
ahead and defined the following macro:
#define readStdin(x) while(true) \
{ \
cin >> x; \
if(!cin) \
{ \
cin.clear(); \
cin.ignore(MAX_LINE_LEN, '\n'); \
} \
else \
break; \
}

This macro has been working like a charm for me. Any other
suggestions are welcome. (Yes I know that I can read the line into a
buffer and read input from that buffer, but that's not what I want to
do).


So what is your question?


It's right there above your question: "...Any other suggestions are
welcome. ... "

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.