473,395 Members | 1,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
3 4899
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: aGAric | last post by:
i use std::getling to read a line in unicoude file to buffer,like: WCHAR buf wifstream in; std::getling(buf,in,100); but i can't get the right result,it seems can only read by 1 byte;e.g first...
33
by: Chen shuSheng | last post by:
I have a code: --------------------------- #include <iostream.h> #include <stdlib.h> int main() { int max=15; char line; getline(line,max); system("PAUSE");
6
by: bryant058 | last post by:
#include<iostream> #include<cstring> #include<string> #include<iomanip> using namespace std; int main() { string s; char *tokenptr; int space;
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.