How to quit using 'Enter'. Advise please...

J

jelly

Hello All
Part of the code I am working on requires that a loop exits upon the 'ENTER'
key being pressed.
I would have thought its possible by doing something like the following:

while(input != '\n')
...do something;

I tried this but it obviously does not work. So it seems I must use the
Ascii
value of 'ENTER' key to quit in the while loop. How do i do this? Thanks
much.
 
K

Kevin Goodsell

jelly said:
Hello All
Part of the code I am working on requires that a loop exits upon the 'ENTER'
key being pressed.
I would have thought its possible by doing something like the following:

while(input != '\n')
...do something;

I tried this but it obviously does not work. So it seems I must use the
Ascii
value of 'ENTER' key to quit in the while loop. How do i do this? Thanks
much.

It has nothing to do with ASCII. ASCII is not required to be supported
in any way by a C++ implementation. We need real code to tell you what's
wrong.

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

-Kevin
 
M

Mike Wahler

jelly said:
Hello All
Part of the code I am working on requires that a loop exits upon the 'ENTER'
key being pressed.
I would have thought its possible by doing something like the following:

while(input != '\n')
...do something;

I tried this but it obviously does not work. So it seems I must use the
Ascii
value of 'ENTER' key to quit in the while loop. How do i do this? Thanks
much.

We cannot diagnose code we cannot see.
However, here is one of many possible approaches:

std::string& getinput()
{
static std::string s;
std::getline(std::cin, s);
}

while(!getinput().empty())
{
/* whatever */
}

-Mike
 
J

Jelly

Thanks guys. I did not post the whole code because in the past I have been
told to only post the specific piece ie the while loop.

Thanks again.
 
M

Mike Wahler

Jelly said:
Thanks guys. I did not post the whole code because in the past I have been
told to only post the specific piece ie the while loop.

[Please don't top-post.]

When asking a question about your code, you need to post
enough of it to provide sufficient context to those reading.

All you originally posted was:

while(input != '\n')
...do something;

... which could only be answered if context were provided.

A better code fragment would have been a complete small
program which showed how/where the 'input' was obtained,
what the type of 'input' is, how you attempted to evaluate
and act upon it, etc.

-Mike
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top