1

I know this is an odd question, but is there any way of reading a previous input from the console?

Something like:

The fox is brown // line 1
The duck is yellow // line 2
Here where the control is right now_ // but I want to read line 2

P.S : I'm using windows

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
  • Write a console interface class that reads from the console and stores the lines for future use. It should provide a simple interface for getting the current line (blocking on input), and getting previous lines. – Peter Sep 30 '16 at 19:16

3 Answers3

5

If, by reading previous input, you mean from within the C++ program the answer is yes. The standard input is a stream which maintains a read buffer.

Quick and dirty to unwind the stream and read the same line twice

#include <iostream>
#include <string>

using namespace std;

int main()
{
    cout << "Enter a line of text and you will see it echo twice\n";

    string mystring;
    getline(cin, mystring);
    cout << mystring;

    cout << "\n";

    // reverse the input stream by the lengtht of the read string (+1 for the newline)
    for (int i = 0; i <= mystring.length(); i++)
    {
        cin.unget();
    }

    string mystring2;
    getline(cin, mystring2);
    cout << mystring2;

    cout << '\n';
}
dkackman
  • 15,179
  • 13
  • 69
  • 123
  • 1
    You just basically blatted most of my answer, so here is one caveat: that stream buffer doesn't go back forever and you have effectively zero control over when it will clean up. By the time you go looking, some or all of line 2 may no longer be available. – user4581301 Sep 30 '16 at 19:07
  • Thanks, but could you briefly explain what the std::cin.unget() does exactly – Nicholas Theophilus Sep 30 '16 at 19:11
  • 2
    [`unget` puts the last character back in the stream](http://en.cppreference.com/w/cpp/io/basic_istream/unget). Other note: Trying to use `seekg` to do the same thing is... Well I can't find anything explicit in [the standard working draft copy](http://open-std.org/JTC1/SC22/WG21/docs/papers/2016/n4606.pdf) that's freely available, but it's looking implementation defined. Don't count on `seekg` working. – user4581301 Sep 30 '16 at 19:15
  • So, can I do something like `string c = cin.unget()` Not really what I'm trying to do but just checking, cause what I want to do exactly is check the character at a specific location in the stream to check for a newline character would that be possible – Nicholas Theophilus Sep 30 '16 at 19:32
  • 1
    @NicholasTheophilus No. Unget does not return the ungotten character. If you read on the documentation for basic_stream you'll see a bunch of different ways to evaluate and manipulate the input stream. If the previous state of the input stream is important to your app, I'd come up with a different approach, like caching the input yourself. Mucking with cin, cout, and cerr is best avoided, though possible. – dkackman Sep 30 '16 at 19:37
  • Thanks a whole lot for everything you guys – Nicholas Theophilus Sep 30 '16 at 19:41
  • 1
    @zinouteyar instead of a for loop you would do a while loop and unget until you found whatever delimiter it is you are using to separate input – dkackman Apr 18 '19 at 13:52
1

It can be done with some assumptions, but brings some limitations which you may find too strict. So it is better to find a way how not to do that.

Using ncurses library you can get full control over terminal and read/write symbols to any position on the screen. But if you do so, you will be responsible for controlling it, this includes scrolling text. It will also not possible to scroll terminal up unless you implement it yourself. Beside that you will need to keep in mind screen size and process it's changes. Be aware that your program can also be launched under terminal that does not support that mode.

So, don't mess with it if you can and store user input inside your program instead of storing it on the screen.

Alexey Guseynov
  • 5,116
  • 1
  • 19
  • 29
0

This seems like it's pretty close.

history | tail -n 2

Dark Matter
  • 325
  • 3
  • 9