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

How can I exit from "getline()" without pressing any key?

P: n/a
Hi,

When I capture the SIGINT event, I encounter a probelm.

Please look at the following code, if you don't press any key, the
'getline()' will block there all the time.

When I press 'ctrl+c', the function 'getline()' still block there.

My question is how can I exit from "getline()" while I just press 'ctrl
+c'.
#include <string>
#include <iostream>
using namespace std;

static bool g_bRun = true;

static void OnSIGINT(int)
{
g_bRun = false;
}

int main()
{
Signal(SIGINT, OnSIGINT);

string input;
while (g_bRun) {
if (getline(cin, input)) {
cout << input << endl;
}
}

cout << "exit." << endl;

return 0;
}
Thanks.

Jun 8 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
ca********@gmail.com wrote:
Hi,

When I capture the SIGINT event, I encounter a probelm.

Please look at the following code, if you don't press any key, the
'getline()' will block there all the time.

When I press 'ctrl+c', the function 'getline()' still block there.

My question is how can I exit from "getline()" while I just press
'ctrl +c'.
The behaviour of the program upon receiving _any_ input is very much
implementation- and platform-dependent. Please ask in the newsgroup
for your OS. For example, on the OS I used to write for, Ctrl+C would
not be converted into a signal for my program until the program tried
to output something. Yes, I know, it might seem strange, but that's
what it was.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 8 '07 #2

P: n/a
ca********@gmail.com wrote:
Hi,

When I capture the SIGINT event, I encounter a probelm.

Please look at the following code, if you don't press any key, the
'getline()' will block there all the time.

When I press 'ctrl+c', the function 'getline()' still block there.

My question is how can I exit from "getline()" while I just press 'ctrl
+c'.
I don't think there is a way of doing what you ask using the C++
standard. There are plenty of platform dependant ways of doing it.

The istream method "readsome" might help but you need to "wait" for
input, readsome does not.
Jun 8 '07 #3

P: n/a
Hi! Thanks for asking the question to us.
Buy the i would like to know the which version of software you are
using? Bcaz you know that there are different kinds of software for
the C++. For example i using the VB 6.0 and VB 2005 and both the thing
are totally different you cant match the code if you write the code
for the VB 6.0 you cant debug or run in the VB2005 it will give us an
error or it will gives us warning.
Buy the did you have tried to put anything like the following:
1. cout.flush();
2. system("pause");
3. In case of return 0 write the return option.

Hope this might help you.
Ok

Jun 8 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.