-1

I have written a simple FORTH-like interpreter where I get input a line at a time and process it like this:

std::string line;

while (!std::cin.eof()) {
    std::getline(std::cin, line);
    std::istringstream input(line);

    std::copy(std::istream_iterator<std::string>(input),
        std::istream_iterator<std::string>(),
        std::back_inserter(data.input_));

    // some work
}

And if I input 5 5 + . <enter> it displays like this:

5 5 + .
10 ok

But what I would like it show is:

5 5 + . 10 ok

In other words a newline should be accepted to mean end of line input but it should not be echoed to std::cout.

Having done some reading it seems I will not be able to achieve this in std c++ (is this true?) but I can use the functions in the termios library to set up the console this way on a POSIX system without messing with other console functions such as ctl-c handling etc. I haven't figured out exactly how though. Can anyone show me the way?

Jaldhar
  • 279
  • 1
  • 10
  • 3
    Where is the code that prints that output? – smac89 Oct 05 '20 at 06:19
  • 3
    Please read [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) Actually, this could be the root cause of your problem. Try doing `while (std::getline(std::cin, line))` instead. – Some programmer dude Oct 05 '20 at 06:22
  • Also please take some time to refresh [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). And don't forget how to [edit] your questions or how to create a [mcve]. – Some programmer dude Oct 05 '20 at 06:28
  • I didn't think the output would be relevant because it is the input which is the problem no? But in any case, it would be something like: std::cout << 10 << ' '; std::cout << " ok\n"; – Jaldhar Oct 05 '20 at 07:04
  • By the way there is get_ch() in library for windows. what it does is getting char (Like enter) but not prints to console. if you found simiar function that works on your OS-Terminal. you can use it – Ahmet Yusuf Yatkın Oct 05 '20 at 08:32
  • 1
    It seems some of us misunderstood what you're trying to do. The `5 5 + .` that "it displays" is the *input*, not something that your program output? One of the reasons it's so important with a [mcve] is that we don't have to guess, as well as having the input and output separately labeled. – Some programmer dude Oct 05 '20 at 09:08
  • Considering you're on a POSIX platform, there are many ways to handle terminal input, without causing e.g. newline to echo. The ncurses library is one such solution, but you can also send terminal control codes (*not* VT100 control codes) to enable and disable echo at will. – Some programmer dude Oct 05 '20 at 09:10
  • Assuming that the displayed input is due to termios echoing (rather than your program performing the echo), have you tried clearing the **ECHONL** attribute in the `c_lflag` termios member? Alternatively, disable all echoing by termios and read characters instead of by line, and then your program can gain full control of what is displayed for all typed input, e.g. evaluate the input after typing `=` instead of `.` and `\n`. See https://stackoverflow.com/questions/58205140/backspace-b-not-clearing-text-in-non-canonical-mode-termios. – sawdust Oct 06 '20 at 04:54
  • @sawdust unfortunately that doesn't seem to make a difference. So far only the ANSI escape sequences suggested by Ahmet Yusuf Yatkin have worked. – Jaldhar Oct 06 '20 at 07:39
  • Your results are correct, Turns out that ECHONL is only for enabling (rather than suppressing) newline echoing. However the alternative is still viable: disable all echoing by termios (which it can do), and then have your program echo what it wants. That is a generic POSIX solution that does not depend on any specific terminal emulation. – sawdust Oct 06 '20 at 20:51

1 Answers1

1

You can Use VT100 Escape Sequences for customising console output (VT100 ESC SEQUENCES). in this case;

use \033[1A for go 1 line up, \033[nC (n is int value) to Go n letter right

  • 1
    This is a cosmetic work-around, not an actual fix. – Some programmer dude Oct 05 '20 at 06:24
  • @super you're right. But we don't know which terminal he is using. and maybe will be usefull for him or other future persons – Ahmet Yusuf Yatkın Oct 05 '20 at 06:25
  • Eventually I would want to be terminal independent but for now I am developing and testing on Linux and targetting any POSIXlike OS. – Jaldhar Oct 05 '20 at 07:07
  • @AhmetYusufYatkın While it may not be ideal, this definitely works! I can see how this could cause portability problems in the long run so I am still awaiting the "right" answer. – Jaldhar Oct 05 '20 at 07:14
  • @Jaldhar in my searchs (I were coding console app too) There is no way to really delete Console Outputs if you don't reach console API. You can only do Cosmetic solutions, like this or \b or other things. without Console API. so your right answer may be won't come. – Ahmet Yusuf Yatkın Oct 05 '20 at 07:46