End of cin

N

newbiecpp

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.
 
J

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)
 
A

Ali Cehreli

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
 
C

Chiap Zap

newbiecpp said:
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
 
C

Chiap Zap

Chiap said:
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
 
N

newbiecpp

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?
 
A

Ali Cehreli

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
 
J

johnny

newbiecpp said:
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()
 
J

Jonathan Turkanis

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
 
G

Gernot Frisch

// ------------------------------------------------------------- //
// 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)ConsoleHandler,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
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top