Wait until user presses enter in C++? - c++

waitForEnter() {
char enter;
do {
cin.get(enter);
} while ( enter != '\n' );
}
It works, but not always. It doesn't work when an enter is pressed just before the function is called.

You can use getline to make the program wait for any newline-terminated input:
#include <string>
#include <iostream>
#include <limits>
void wait_once()
{
std::string s;
std::getline(std::cin, s);
}
In general, you cannot simply "clear" the entire input buffer and ensure that this call will always block. If you know that there's previous input that you want to discard, you can add std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); above the getline to gobble up any left-over characters. However, if there was no extra input to begin with, this will cause an additional pause.
If you want full control over the console and the keyboard, you may have to look at a platform-specific solution, for instance, a terminal library like ncurses.
A select call on a Posix system that can tell you if reading from a file descriptor would block, so there you could write the function as follows:
#include <sys/select.h>
void wait_clearall()
{
fd_set p;
FD_ZERO(&p);
FD_SET(0, &p);
timeval t;
t.tv_sec = t.tv_usec = 0;
int sr;
while ((sr = select(1, &p, NULL, NULL, &t)) > 0)
{
char buf[1000];
read(0, buf, 1000);
}
}

On Windows, you can do this:
void WaitForEnter()
{
// if enter is already pressed, wait for
// it to be released
while (GetAsyncKeyState(VK_RETURN) & 0x8000) {}
// wait for enter to be pressed
while (!(GetAsyncKeyState(VK_RETURN) & 0x8000)) {}
}
I don't know the equivalent on Linux.

(the first parameter) The name of the array of type char[] in which the characters read from cin are to be stored.
(the second parameter) The maximum number of characters to be read. When the specified maximum has been read, input stops.
(the third parameter) The character that is to stop the input process. You can specify any character here, and the first occurrence of that character will stop the input process.
cin.getline( name , MAX, ‘\n’ );
Page 175 IVOR HORTON’S BEGINNING VISUAL C++® 2010

Related

Why does scanf_s() isn't working second time I am calling it in order to verify if user provided correct input?

I am writing an application with a menu and I am asking the user to provide an integer representing an option
from the menu
1. Option 1
2. Option 2
3. Option 3
...
This option is stored in a variable called
option
I want to avoid wrong input such as "12a", "1a2", "anyString" and I've read that this can be achieved by storing return value of scanf_s() function.
So I stored it in a variable called
ret
and now I want that every time user provides wrong input to prompt them to enter a new value.
So I wrote something like this:
int ret = scanf_s("%d", &option);
while (!ret)
{
cout << "Provide an integer, not a string! :)\n";
ret = scanf_s("%d", &option);
}
The problem is when it enters the while it is not allowing user to enter a new value and hence the value of ret never changes, making it run endlessly.
How can I achieve what I am looking for?
When scanf_s fails to convert an integer, the offending input stays in the input stream. Calling scanf_s("%d", &option) again will produce the same result until some characters are removed from the input stream. Note also that using scanf_s or scanf directly from stdin is error prone: the newline entered by the user stays in the input stream and will be read by a subsequent call to getchar() or fgets(), potentially causing unexpected behavior.
To avoid these pitfalls, it is recommended to read one line at a time with fgets() and convert it with sscanf() this way:
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdio.h>
int main() {
char buf[80];
int option;
char cc;
for (;;) {
print_menu(); // output the menu options
if (!fgets(buf, sizeof buf, stdin)) {
/* end of file reached: break from the loop */
break;
}
/* parse exactly one integer with optional leading and trailing whitespace */
if (sscanf(buf, "%d %c", &option, &cc) != 1) {
printf("invalid input: %s", buf);
printf("enter one integer exactly\n");
continue;
}
printf("option is %d\n", option);
// handle option
}
return 0;
}

How to detect user input of CTRL-X in C++

I need to end my function once the user inputs CTRL-X or '/'. I don't know how to detect/check for a user input of CTRL-X.
The task given to me states:
'When you edit the file, you will enter data line by line and when done, you will enter ‘/’ or CTRL + X to exit.'
I have written this code so far. I'm a beginner so pardon my code.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string data;
int line_num=1;
ofstream editFile;
editFile.open("edit_test.txt");
while(data!="/"){ //some kind of condition ORed with the condition already present in while loop?
cout<<line_num<<"> "; //I want to display line numbers every time the user presses enter
getline(cin, data);
if(data!="/"){
editFile<<data<<endl;
line_num++;
}
}
editFile.close();
return 0;
}
CTRL+X is the same as character code 24 (since X is the 24th letter of the alphabet). Barring any system interference*, all you need to do is check to see if character code 24 is in the input.
while (getline( std::cin, s ))
{
// Find ^C in s
auto n = s.find( '\x18' );
if (n != s.npos) s = s.substr( 0, n );
// process s normally here
// If ^C was found in s, we're done reading input from user
if (n != s.npos) break;
}
CTRL+X doesn’t tend to have any special system actions attached to it, so you shouldn’t have any problem getting it as input.

fgets() does not return NULL on empty string

I recently tried to use fgets() instead of scanf() to read a string for code security reasons. I used a simple function that I found here to check for errors (no input and too long input). The problem is that whenever i press "ENTER" without actually writing anything, fgets() doesn't return NULL and my program is not able to show the NO_INPUT error.
Here's main.cpp:
#include <stdlib.h>
#include <stdio.h>
#include "utilities.h"
int main() {
int rc;
char str[20];
rc = getLine("Enter string: ", str, sizeof(str));
if(rc == NO_INPUT) {
printf("[ERROR] No input\n\n");
} else if(rc == TOO_LONG) {
printf("[ERROR] Input is too long\n\n");
} else {
printf("This is your input: \"%s\"\n\n", str);
}
system("pause");
}
Here's utilities.h:
#include <stdio.h>
#include <string.h>
#define OK 0
#define NO_INPUT 1
#define TOO_LONG 2
int getLine(const char *msg, char *buff, size_t len) {
if(msg != NULL) {
printf("%s", msg);
fflush(stdout);
}
if(fgets(buff, len, stdin) == NULL /*[+]*/ || strcmp(buff, "\n") == 0) {
//[-] fflush(stdin);
return NO_INPUT;
} else if(buff[strlen(buff)-1] != '\n') {
//[-] fflush(stdin);
return TOO_LONG;
} else {
buff[strlen(buff)-1] = '\0';
//[-] fflush(stdin);
return OK;
}
}
And here's the output:
Enter string:
This is your input: ""
Press any key to continue . . .
I solved my problem by replacing the first if statement in utilities.h with if(fgets(buff, len, stdin) == NULL || strcmp(buff, "\n") == 0). Doing this, my program will check for input errors OR for empty strings and return the NO_INPUT flag.
Thanks to everybody who commented and #Peter for answering. I added the aforementioned if statement in utilities.h and removed every fflush(stdin) occurrence. The code now looks as above.
Your problem that you "fixed" is believing that a end of line should be treated as end of input.
NULL is an indication from fgets() that it encountered an error or the end of input when reading from the file (or stream). A blank line is neither an error nor a marker of end of input. A human (typing on a keyboard) might choose to interpret a newline as end of input, but a computer program does not - after all, there is nothing stopping a user entering more than one line of input.
Practically, fgets() reads a line and indicates the end of that line with a '\n' character. Let's say, we have a file containing
ABC
DE
FGHIJ
(blank lines interspersed in three lines of text, followed by end of the file).
Let's also that buffer is an array of five char, and that we read that file using consecutive statements of the form fgets(buffer, 5, file).
So what will fgets(buffer, 5, file) do on each call. Well, with 1 representing the first call, 2 representing the second call, etc we will see results of
"ABC\n" stored into buffer;
"\n" stored into buffer; (first blank line)
"DE\n" stored into buffer;
"\n" stored into buffer; (second blank line)
"FGHI" stored into buffer;
"J\n" stored into buffer; and
fgets() returns NULL, and nothing is stored into buffer.
The first six calls will all return &buffer[0] - not NULL - since no error is encountered reading from the file. Even though there are two blank lines in the input. The last line, which is longer than the buffer (with the '\n' counted) is read in two parts.
Incidentally, your code is using fflush(stdin). Unfortunately, fflush() only has defined behaviour on OUTPUT streams or files. Using it on stdin (or any input stream) gives undefined behaviour. If it is actually discarding input (which it does with some implementations of the C standard library), you are getting lucky - there are real-world compilers where the resultant behaviour does not discard input.

CMD Prompt C++: Limiting literals entered on screen

I hope the question isn't to ambiguous.
when I ask:
int main()
{
string name = {""};
cout << "Please enter a name: " << endl;
getline(cin, name);
//user enters 12 characters stop displaying next literal keypresses.
enter code here
}
I would like to be able to limit the amount of times the user can enter a char on screen. Example, the screen stops displaying characters after length 12?
If so what would be the library and command line for doing something like this?
Wanting to this as, I have a ascii art drawn on the CMD, and when I cout the statement at x,y anything over 12 characters long inputed draws over the ascii art.
I hope this makes sense :'{ Thank you!
By default the console is in cooked mode (canonical mode, line mode, ...). This means
that the console driver is buffering data before it hands it to your application
characters will be automatically echoed back to the console by the console driver
Normally, this means that your program only ever gets hold of the input after a line ends, i.e. when enter is pressed. Because of the auto-echo, those character are then already on screen.
Both settings can be changed independently, however the mechanism is --unfortunately-- an OS-specific call:
For Window it's SetConsoleMode():
HANDLE h_stdin = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode = 0;
// get chars immediately
GetConsoleMode(hStdin, &mode);
SetConsoleMode(hStdin, mode & ~ENABLE_LINE_INPUT));
// display input echo, set after 12th char.
GetConsoleMode(hStdin, &mode);
SetConsoleMode(hStdin, mode & ~ENABLE_ECHO_INPUT));
As noted by yourself, Windows still provides conio.h including a non-echoing _getch() (with underscore, nowadays). You can always use that and manually echo the characters. _getch() simply wraps the console line mode on/off, echo on/off switch into a function.
Edit: There is meant to be an example on the use of _getch(), here. I'm a little to busy to get it done properly, I refrained from posting potentially buggy code.
Under *nix you will most likely want to use curses/termcap/terminfo. If you want a leaner approach, the low level routines are documented in termios/tty_ioctl:
#include <sys/types.h>
#include <termios.h>
struct termios tcattr;
// enable non-canonical mode, get raw chars as they are generated
tcgetattr(STDIN_FILENO, &tcattr);
tcattr.c_lflag &= ~ICANON;
tcsetattr(STDIN_FILENO, TCSAFLUSH, &tcattr);
// disable echo
tcgetattr(STDIN_FILENO, &tcattr);
tcattr.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSAFLUSH, &tcattr);
You can use scanf("%c",&character) on a loop from 1 to 12 and append them to a pre-allocated buffer.
As in my comments, I mentioned a method I figured out using _getch(); and
displaying each char manually.
simplified version:
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
string name = "";
int main()
{
char temp;
cout << "Enter a string: ";
for (int i = 0; i < 12; i++) { //Replace 12 with character limit you want
temp = _getch();
name += temp;
cout << temp;
}
system("PAUSE");
}
This lets you cout each key-press as its pressed,
while concatenating each character pressed to a string called name.
Then later on in what ever program you use this in, you can display the full name as a single string type.

How to check if std::cin is associated with a terminal or a pipe

How is it possible to check if std::cin buffer is associated with a terminal or a pipe?
Command line which shows that std::cin is associated with a file:
Application.exe < C:\output.txt > C:\result.txt
Command line which shows std::cin associated with a terminal:
Application.exe
I tried to analyze in_avail() for this purpose. But it hasn't helped.
std::cin.rdbuf()->in_avail()
I found an answer to my question.
First of all I have to ask my question in a correct way.
Question I want to ask: How to figure out if std::cin is associated with a terminal or a pipe.
On Windows:
_isatty( _fileno(stdin) );
On POSIX:
isatty( fileno(stdin) );
On Windows:
_isatty call retursn non zero value if stdin is associated with a char device.
On POSIX:
isatty call returns 1 if stdin is referring to a terminal.
isatty man for Linux: http://man7.org/linux/man-pages/man3/isatty.3.html
_isatty man on MSDN: http://msdn.microsoft.com/en-us/library/f4s0ddew.aspx
From the reference of std::istream::peek:
If good() == true, returns the next character as obtained by rdbuf()->sgetc() Otherwise, returns Traits::eof().
This means that it will either return the next available character, or an eof flag.
And if will, if needed, cause the underlying buffers underflow function to be called which fills the buffer if there's anything to read from the operating system.
This will work for file input (and user input):
#include <iostream>
#include <sstream>
int main(int argc, const char * argv[])
{
typedef std::istream::traits_type Traits;
std::stringstream buffer;
Traits::int_type ch;
while((ch = std::cin.get()) != Traits::eof()) {
buffer << Traits::to_char_type(ch);
}
std::cout << buffer.str();
}
Having user input in a (linux) terminal a < ctrl-d > is needed to stop reading.
There is no other way (basically), to detect if cin has (will get) a character available (Although some protocol could handle the user input).