Need help with console
| Rope (20) | |||||
| Hi all, I'm trying to read the console after I press CTRL+C. Now I've been able to catch the CTRL+C and then do some stuff. However I can't get the input. It's like it needs a '/n' to get the input. Here's the code:
main.h
Here's an example of what I enter. If I enter "test" in the console it prints out this:"Input = " If I enter "test" end press enter "bla" it prints out this: "Input = test" So my question is, How do I get the first line without having to press enter? Same goes for multiple lines in which case I need to get the last line. Thanks in advance, Rope. | |||||
| Rope (20) | |||
| I'm I not giving enough information? Or does nobody know? | |||
| Grey Wolf (637) | |||
You could try using a bool to control the input while loop.
Then set finished to true in your CTRL-C handeler | |||
| DiptenduDas (84) | |||
| Hi Rope, I have a small piece of code. But the problem with this is that while inputting the data the cursor does not move to new line when user enters carriage return. But after CTRL + C ur required data is getting displayed. If somebody can sort out this then it will solve ur problem.
I know this have some problems with it still den if it helps :-O | |||
| Duoas (1378) | |||
| The answer depends entirely on what OS you are using. Are you using Windows or Unix/Linux? BTW. Ctrl-C is a poor choice. You'd be better off using ^Z (on Windows) or ^D (on Unix) to terminate input, then simply cin.clear() to reset the input stream. Or even better, watch the input for a specific string, or two consecutive newlines, or something. Let me know what you want to do. | |||
| DiptenduDas (84) | |||||
| Hi Rope, A small correction to my above post... Please make a small change to the below code.
to
For Windows platform and U have to include conio.h header file. | |||||
| ropez (312) | |||
Really? For which function? | |||
| DiptenduDas (84) | |||
What header file needs to be included for getch() in Windows then??? | |||
| ropez (312) | |||
Ok. I suggest two better alternatives:
c = std::getchar();http://www.cplusplus.com/reference/clibrary/cstdio/getchar.html
c = std::cin.get();http://www.cplusplus.com/reference/iostream/istream/get.html Don't make your code depend on deprecated headers from the MS-DOS era when there's no reason for it. | |||
| DiptenduDas (84) | |||
| Thnx Ropez for the links.. I had earlier tried the same[std::getchar() n std::cin.get()] instead of getch(). But that didn't fulfill the requirement of Rope [:-(]. I Think [Please clarify].... The problem with std::getchar() and std::cin.get() is that it doesn't execute the next line until I press carriage return. Thus the inputted data is only available iff user presses carriage return, which is the main problem of Rope. Waiting for suggestion in this regard. | |||
| Duoas (1378) | |||
| That has nothing to do with the functions. It is an OS-dependent quality of the console input device. You have to turn it off. On windows:
To turn off line buffering, use:
linebuffered( false );and to turn it back on, use:
linebuffered( true ); | |||
This topic is archived - New replies not allowed.
