Is it possible to cout without overwriting current text in the input? - c++

Okay, so let's say I have a program that couts a line while the user may be typing in information.
For this example, let's say we're using the code
cout << "THIS CODE IS BEING COUTED" << endl;
Let's say for our example, the user is in the process of typing up an input and as it stands they have only entered "hello", but has not yet pressed enter.
As it stands, when the line is executed, the user will see
"helloTHIS CODE IS BEING COUTED"
and they will be given a new line to input information.
What I want to do is instead of cout'ing, I would like to get the text in the current input, erase it from the input, cout the line that needs to be cout'ed, and then re-enter the info into the input.
Does this make sense or is this a bunch of jumbled nonsense?
Thanks for reading.
Edit: Clarification: I want it so if I have a string entered into my input and I cout, that the cout will be displayed above my input instead of inserting it past my input. I also want my input to be unaffected so the user can continue typing or delete what was already entered.

If you are getting the input character by character then when you need to output your text you could move the output position to the start of the line by printing carriage return, '\r'. Then your output will overwrite the current input, after which print a line feed and reprint what has been input so far
cout << "\r" << output << "\n" << currentinput;
If the output is shorter than the input then you will only partially overwrite the input, in which case you could pad the output with spaces up to the length of the current input

You may ask user in one, main thread with cin >> data there and read data from stdin in other thread by fread or something like that what works with FILE* handle.
After the data you want was typed by user you may clear screen (for example by clrscr() in conio.h or by any other better way) and cout what you want.

Related

Default value for input in C++ [duplicate]

My app reads user input using std::cin stream.
In one place I would like to provide default input and let the user to accept it as it is (by pressing enter) or modify it before continuing (by removing old characters with backspace and adding new text).
I'm aware that characters can be placed directly into cin.rdbuf, but that's not exactly what I want to achieve.
I would like to put characters into console window in the place where console's cursor is when waiting for user input and do no read them before user will accept them. User should be also able to remove them and write their own text.
Can something like this be achieve using cin or do I have to simulate this by reading single characters and repainting content of the console?
No, something like that cannot be done with std::cin. Its read buffer is read directly from standard input. Standard input is a "cooked" character stream. All the editing is handled entirely by your operating system's terminal console, and pressing Enter results in your application's std::cin reading the entered text.
The traditional way this is done is to simply indicate the default input value in the prompt itself, and use the default value in the event of empty input, something like:
std::string buffer;
std::cout << "What color is the sky [blue]? ";
std::getline(std::cin, buffer);
if (buffer.size() == 0)
buffer="blue";

Providing default value of cin input

My app reads user input using std::cin stream.
In one place I would like to provide default input and let the user to accept it as it is (by pressing enter) or modify it before continuing (by removing old characters with backspace and adding new text).
I'm aware that characters can be placed directly into cin.rdbuf, but that's not exactly what I want to achieve.
I would like to put characters into console window in the place where console's cursor is when waiting for user input and do no read them before user will accept them. User should be also able to remove them and write their own text.
Can something like this be achieve using cin or do I have to simulate this by reading single characters and repainting content of the console?
No, something like that cannot be done with std::cin. Its read buffer is read directly from standard input. Standard input is a "cooked" character stream. All the editing is handled entirely by your operating system's terminal console, and pressing Enter results in your application's std::cin reading the entered text.
The traditional way this is done is to simply indicate the default input value in the prompt itself, and use the default value in the event of empty input, something like:
std::string buffer;
std::cout << "What color is the sky [blue]? ";
std::getline(std::cin, buffer);
if (buffer.size() == 0)
buffer="blue";

Read from past console entries

Let's say my program or another program did:
cout << "Hi" << endl;
And I want to read the last line entered into the console.
Something like:
string s=lastline();
s should have "Hi" as its contents.
Or to take it one step further, get the entire contents of the console put into something (vector, array, string, string stream, etc).
How would I go about doing this?

C++ - Skipping code after first run-through?

I have a do-while loop, shown below
do
{
dimensions = NULL;
printf("=======================================\nDo you want to multiply in 2 dimensions, or 3?\n\n");
scanf("%c", &dimensions);
... //do stuff
printf("\nEnter r to repeat, return to terminate\n");
scanf("%c", &key);
scanf("%c", &key);
}
while(key == 'r');
On the first run, it executes fine. The problem however is when it runs through the code again after the user enters 'r' and hits return. It'll take you to the first printf("==== etc., but won't allow the user to do anything, it'll go straight back to the second printf("\nEnter...
I stepped through the code to see what was going on, and on a second run through the program just skips the scanf( and all following code for absolutely no reason. Initially I thought it was because 'dimensions' wasn't being set to a value that doesn't run the following methods - but I have, and even if that were the case, the program would run the methods instead of skipping them without user input.
Am I missing something? Is scanf( not enough to stop the program once it's already been used?
Your problem is that when your program gets input from the console with scanf, it reads the data from the keyboard into the input buffer, then values are taken out of the buffer and placed into the locations you provide to scanf. The issue is that when scanf reads a character, it also reads the \n into the buffer, then upon being called again, it reads the second character that was placed into the buffer (without asking you for more input - because why would it? It already HAS things in the buffer).
So there are two solutions: one - use fflush on stdin like so: fflush(stdin). Second - write a while loop that clears out characters one by one from the input buffer: while (getchar() != '\n' );
EDIT: For more reading, see How to clear input buffer in C?
Think it through: "the user enters 'r' and hits return", then the program reads the 'r' and repeats. What's left in the input buffer? There were two keys pressed, and the code only read the first one.
That's also the reason that the code needs two calls to scanf. The first clears the extra character out of the input buffer and the second reads the new character.
What is happening now
To make the buffer flush you need to enter
r<enter>
Hitting <enter> flushes the buffer. So the input buffer now contains two characters.
r\n
So the first scanf will read r
The second scanf will read \n
So by the time you reach the while key has a value of \n. The test fails and the loop is not repeated. So you should remove the second scanf() reading into key.
So you remove the second scanf. Now what happens?
User types
r<enter>
This leaves the input buffer with:
r\n
The scanf() read r into key. The loop repeats correctly. But when we get back to the scanf(). The input buffer still has \n character in the buffer. So the scanf() immediately reads this value and the loop exists as it should have.
how you should fix it.
Ask for Y/N answer and validate that the input is correct.
std::string line;
std::getline(std::cin, line);
while (line != "Y" && line != "N")
{
std::cout << "Hey cluts enter a correct value. Y/N\n";
std::getline(std::cin, line);
}

Possible to discard return character when using std::cin >>?

When using the >> operator in c++ to capture user input, is it possible to prevent the console from printing the newline that is generated when the user presses the return key?
You cannot prevent newline character, because when you use cin, you are communicating with system core, which is not under control by users. console will return, when you enter \n or EOF or other exception situation.
So the better way is to use getchar() to capture the '\n', and do not leave it in buffer.
It is possible to prevent this newline behavior by inputting two EOFs instead of Carriage Return from the keyboard. After entering your string at the console prompt, hit
CTRL-D, CTRL-D
Note, this is a platform specific answer. This works on my Mac, but on Windows OS the EOF sequence may be CTRL-Z, RETURN. I would appreciate an answer edit <-- HERE.
Alternately, you can ditch the >> operator and use something like std::getline and specify an exact string termination delimiter. For example:
std::string myString;
std::getline(std::cin, myString, ';');
std::cout << myString;
This will read from standard input to myString, and put the string terminating NULL character where it finds the first semicolon ';'. Then you'll only have to hit CTRL-D (input EOF) once.
You can enter the values or input by pressing space every time. But at the end you must press enter key.
Let's say: you want to enter "5,4,3,2,1"
You can do: 5 [enter] 4 [enter] 3[enter] 2[enter] 1[enter]
Also: 5[space]4[space]3[space]2[space]1[enter]
But if you want to print the output near input, you can simply print the input first and than you can print the what you want.
Example:
Input: 3 Output: input+1
So you will do:
cout<<input;
cout<<" "<<input+1<<endl;
Good luck :)