0

Here is a program. Its only purpose is to delete the line printed, after some user input.

main.cpp

#include <iostream>
#define ESC_PREV_LINE "\e[F"
#define ESC_CLEAR_TO_END "\e[K"
int main(){
    int i;
    std::cerr << "Some input to clear: " << std::flush;
    std:: cin >> i;
    std::cerr << ESC_PREV_LINE ESC_CLEAR_TO_END << std::flush;
}

Inside Xcode, if the user were to input 2, this is the output:

Some input to clear: 2
[F[KProgram ended with exit code: 0

If compiled in terminal, using: g++ -std=c++20 main.cpp -o test, the output is cleared after user input, right before the program exists, and the shell looks like the program never ran.

How can I include this functionality within Xcode? I am writing a program that requires it, but developing inside Xcode, and it would be highly inconvenient to have to swap to the terminal just to test my program output.

user18348324
  • 244
  • 10
  • First note that `'\e'` is a GCC-specific extension to the language, it's not standard C++. Then note that by default the `gcc` and `g++` driver programs are *aliases* for the Clang compiler. Thirdly, not all terminals handle all possible control sequences. – Some programmer dude Dec 29 '22 at 04:57
  • OT: Why do you output to `std::cerr`? That should be reserved for actual errors, not general output. – Some programmer dude Dec 29 '22 at 04:58
  • @Someprogrammerdude this is based off someone else's code, and they explained you output to `std::cerr` when you intend the line to take input. – user18348324 Dec 29 '22 at 05:11
  • @Someprogrammerdude do you know where I could find a table of the actual standard escape sequences? I believe the correct ones may be along the lines of `\33[2K` and `\033[A`, but I would like some reference with all of them. – user18348324 Dec 29 '22 at 05:12
  • Whomever told you to use `std::cerr` for generic output is wrong. And considering that it should be unbuffered you don't even need those `std::flush` calls. – Some programmer dude Dec 29 '22 at 05:14
  • 1
    The standard escape sequences are, of course, listed in the C++ standard... ;) There are drafts that are freely available to download and read. Or you can use a reputable reference such as [this one](https://en.cppreference.com/w/cpp/language/escape). – Some programmer dude Dec 29 '22 at 05:15
  • https://en.wikipedia.org/wiki/ANSI_escape_code – Eljay Dec 29 '22 at 05:24
  • @Someprogrammerdude I had the discussion at the following link: https://stackoverflow.com/questions/74946807/how-to-manipulate-stdout/74946893?noredirect=1#comment132259366_74946893 if you would like to correct it. What they said made sense to me, but then again, I'm an incredibly naive and impressionable newbie :3 – user18348324 Dec 29 '22 at 05:25
  • What you're saying makes more sense @Someprogrammerdude as they couldn't really explain `std::flush` when I asked about it. Do you know where the best resource to learn about that as well is? I don't really understand what you mean by unbuffered. – user18348324 Dec 29 '22 at 05:26
  • @Eljay I had looked at wiki and it wasn't quite enlightening. – user18348324 Dec 29 '22 at 05:28
  • @Someprogrammerdude I had looked at that, but I was hoping more for a list of actual ones I could write in code like `\033[A` or `\33[2K`, and the way they described it wasn't too enlightening. – user18348324 Dec 29 '22 at 05:29
  • If you mean the "ANSI" escape codes, not the plain standard C++ string or character escapes, then most of then are indeed called ANSI or VT100 escape codes or control sequences. There are plenty of tutorials and references for them all over the Internet. – Some programmer dude Dec 29 '22 at 05:30
  • @Someprogrammerdude I mean the stuff that allows you to do special stuff with the terminal, not stuff like `\n` or `\0`. I think that's what you're referring to. Thank you. – user18348324 Dec 29 '22 at 05:31
  • If you don't learn C++ through school or classes, then the best resource are [a few good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). While some of them can be expensive, it's an investment that will pay off by learning good habits and good C++. A good book is really worth its weight in gold. :) – Some programmer dude Dec 29 '22 at 05:32
  • I'm at UCLA right now @Someprogrammerdude but I've only taken 4 lower-divs, just for reference. I only took my first CS class ever this last Spring, which was my first introduction to C++ and programming in general. – user18348324 Dec 29 '22 at 05:32
  • One last question, @Someprogrammerdude are VT100 codes like `\033[A` standard C++? I know you mentioned `\e` is a GCC extension, which it seems like clang supports. – user18348324 Dec 29 '22 at 05:37
  • 1
    Then you should be getting a good education. Which should include some good course literature. If possible, getting some more books don't hurt though. :) Also, please don't rush, let it all sink in, and repeat if something is unclear, and ask the teachers, assistants, and your class-mates. Class-mates are a good resource for some [rubber duck debugging](https://en.wikipedia.org/wiki/Rubber_duck_debugging). All in all, it will all work out fine in the end. :) – Some programmer dude Dec 29 '22 at 05:37
  • [VT100](https://en.wikipedia.org/wiki/VT100) is an ***old*** "dumb" terminal, used in the late 1970's. It was so popular that later terminal programs, up until these days, emulate many of its control sequences. It's not a part of standard C or C++, as those languages strive to be generic and to work in any environment. – Some programmer dude Dec 29 '22 at 05:40
  • So does C++ have any standard for manipulating input on the screen @Someprogrammerdude ? That was my question that led to this in the other thread I linked. One person mentioned ncurses, another escape sequences, and the final answer doesn't work and I suspect the person didn't think it through. – user18348324 Dec 29 '22 at 05:43
  • No, all you can do is read and write plain text. For anything else you need to use things like the VT100/ANSI escape sequences that are terminal-specific, or use libraries (like `ncurses`) to handle terminal-specific functions. – Some programmer dude Dec 29 '22 at 05:46
  • By terminal-specific functions, do you mean that `ncurses` is also terminal-specific? – user18348324 Dec 29 '22 at 06:09

0 Answers0