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

End of cin

P: n/a
I have a simple program:

int main()
{
string buffer;
while( cin>> buffer)
cout << buffer << " ";

return 0;
}

When I input string from console, how can I stop input? I click "CTR+D" but
it won't work?

Thanks in advance.
Jul 22 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
J B
char strMyString[100];

I always do a cin.get(strMyString, '\n');

Then it will get the string until you hit the Enter key... : ) (I think..
..heh)
"newbiecpp" <ne*******@yahoo.com> wrote in message
news:zCzPc.245$qM.35@trndny01...
I have a simple program:

int main()
{
string buffer;
while( cin>> buffer)
cout << buffer << " ";

return 0;
}

When I input string from console, how can I stop input? I click "CTR+D" but it won't work?

Thanks in advance.

Jul 22 '05 #2

P: n/a
On Mon, 02 Aug 2004 15:54:23 -0700, newbiecpp wrote:
When I input string from console, how can I stop input?
That depends on the environment in which the application is started.
I click "CTR+D" but it won't work?


That works under Linux (and other unixes I think). For Windows, try
Ctrl-Z.

Ali
Jul 22 '05 #3

P: n/a
"newbiecpp" wrote:

I have a simple program:

int main()
{
string buffer;
while( cin>> buffer)
cout << buffer << " ";

return 0;
}

When I input string from console, how can I stop input? I click "CTR+D" but
it won't work?


Ctrl+C will do it.
^

Greets,
Chiap
Jul 22 '05 #4

P: n/a
Chiap Zap wrote:

"newbiecpp" wrote:

I have a simple program:

int main()
{
string buffer;
while( cin>> buffer)
cout << buffer << " ";

return 0;
}

When I input string from console, how can I stop input? I click "CTR+D" but
it won't work?


Ctrl+C will do it.
^


Alternatively you can build in an exit into your while loop,
e.g. q for quit:

#include <iostream>
#include <string>
using namespace std;

int main()
{
string buffer;
while ((cin >> buffer) && (buffer != "q"))
cout << buffer << " ";

return 0;
}

Greets,
Chiap
Jul 22 '05 #5

P: n/a
Thanks, Ali.

CTR+D works fine with Unix, but I tried every "CTR+any key" in Windowns, it
just didn't work! What's happen under Windows?
"Ali Cehreli" <ac******@yahoo.com> wrote in message
news:pa*********************************@yahoo.com ...
On Mon, 02 Aug 2004 15:54:23 -0700, newbiecpp wrote:
When I input string from console, how can I stop input?


That depends on the environment in which the application is started.
I click "CTR+D" but it won't work?


That works under Linux (and other unixes I think). For Windows, try
Ctrl-Z.

Ali

Jul 22 '05 #6

P: n/a
On Mon, 02 Aug 2004 18:12:59 -0700, newbiecpp wrote:
Thanks, Ali.

CTR+D works fine with Unix, but I tried every "CTR+any key" in Windowns,
it just didn't work! What's happen under Windows?


Sorry to get out of topic for this newsgroup but apparently you need to
hit Enter after pressing Ctrl-Z under Windows.

This maybe because the input is line-buffered and requires a new line to
hand the line over to the application. (?)

Ali
Jul 22 '05 #7

P: n/a
newbiecpp wrote:
I have a simple program:

int main()
{
string buffer;
while( cin>> buffer)
cout << buffer << " ";

return 0;
}

When I input string from console, how can I stop input? I click "CTR+D"
but it won't work?

Thanks in advance.


use cin.eof()
Jul 22 '05 #8

P: n/a

<jo****@n0sq.net> wrote in message
news:MM****************@newsread2.news.pas.earthli nk.net...
newbiecpp wrote:
I have a simple program:

int main()
{
string buffer;
while( cin>> buffer)
cout << buffer << " ";

return 0;
}

When I input string from console, how can I stop input? I click "CTR+D" but it won't work?

Thanks in advance.


use cin.eof()


cin.eof() just tests for end-of-file, which (cin) also does. The
question, I think, was how the user can trigger eof.

Jonathan
Jul 22 '05 #9

P: n/a
// ------------------------------------------------------------- //
// Handle termination from outside
// ------------------------------------------------------------- //
BOOL WINAPI ConsoleHandler(DWORD CEvent)
{
switch(CEvent)
{
case CTRL_C_EVENT:
case CTRL_BREAK_EVENT:
case CTRL_CLOSE_EVENT:
case CTRL_LOGOFF_EVENT:
case CTRL_SHUTDOWN_EVENT:
}
// Handle any break here (See CTRL_C_EVENT)
MsgError();
exit(1);
return TRUE;
}
if
(SetConsoleCtrlHandler((PHANDLER_ROUTINE)ConsoleHa ndler,TRUE)==FALSE)
{
// unable to install handler...
// display message to the user
printf("Unable to install handler!\n");
return -1;
}

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
Jul 22 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.