Currently coding in C++20, using Ubuntu WSL2.
Using the code shown below, the cursor goes invisible when running the program in WSL2 in Windows Terminal, working as intended.
However, when running the program in WSL2 in vscode's integrated terminal, the cursor is visible throughout the whole program (just in case, I even put terminal.integrated.scrollback to 0).
The function curs_set(0) doesn't return ERR when it runs in either of the terminals. Is this a problem with vscode's integrated terminal? Is there a way to fix this?
Code:
#include <ncurses.h>
int main() {
initscr();
noecho();
cbreak();
if (curs_set(0) == ERR) {
addstr("Not working");
}
mvaddstr(1, 1, "Random sentence.");
refresh();
getch();
mvaddstr(2, 1, "Random sentence number two.");
getch();
endwin();
}
I was able to resolve this issue on my end by calling refresh() once first before using curs_set().
Related
I am using ncurses to develop an application in C++ but both the newwin() and mvwin() functions are not working as expected. Whenever I give either of these function argument values in an attempt to make a new window that is of different size then the initial terminal window, nothing appears to happen. For example, the following code should create a new window with a height of 50, a width of 10, at location (10,10). None of this appears to be happening:
#include <ncurses.h>
#include <iostream>
using namespace std;
int main(){
initscr();
WINDOW * win = newwin(50,10,10,10);
wrefresh(win);
getch();
endwin();
return 0;
}
From the documentation I have read, it appears that a window will be created with default sizing and location if any of the arguments passed to it are invalid values, but to my knowledge all of these are valid values. Does newwin() not work like I think it does? Thank you in advance
The question asks about mvwin, but that does not create windows, nor is there an example in the question of a problematic call.
For newwin, zero parameters are handled specially, not "invalid values". The question shows an apparently valid call (and in a quick check, it runs as expected).
However, OP is likely confused that there is no output. That is because the program creates a window, which is empty and then reads from the standard screen.
This altered program produces output by drawing a box on the new window and then reading from that window (preventing the wrefresh associated with getch from painting over the new window):
#include <ncurses.h>
#include <iostream>
using namespace std;
int main(){
initscr();
WINDOW * win = newwin(50,10,10,10);
box(win,0,0);
wrefresh(win);
wgetch(win);
endwin();
return 0;
}
It sounds like there's some misunderstanding here of what ncurses does.
You mention an "attempt to make a new window that is of different size then the initial terminal window" -- if you mean a terminal emulator running in a windowing system like X/OSX/Windows, that is not what ncurses does. A "window" in ncurses is a rectangle within an existing terminal that can be updated/refreshed independently.
This might be why the comments on the other answer seem to be talking past each other.
I'm having trouble getting a simple C++ script to run in VSCode (I'm new to both). I followed these instructions, and the status bar displays "C++" on the bottom right of the screen, next to the smiley face. I then ran the following script:
#include <iostream>
using namespace std;
main()
{
cout << "Hello World!\n";
return 0;
}
When I run it, the script's path flashes on the output screen and disappears. I expected it to display "Hello World" in the output.
I can run the script from the command window (I'm in Ubuntu) and the output file behaves as expected when executed.
Your program prints message to STDOUT and exits. Add some kind of wait (you can read STDIN for example) if you want to see its output.
PS: Why do you call your program "script"?
I faced with Error opening terminal: unknown. with ncurses and Eclipse Luna.
So installed xterm and add TERM=xterm in Run/Debug Configurations > Environment.
Now, when I run following simple "Hello World" app, some strange characters printed in the Eclipse console:
Code:
#include <stdio.h>
#include <ncurses.h>
int main() {
initscr(); /* Start curses mode */
printw("Hello World !!!"); /* Print Hello World */
refresh(); /* Print it on to the real screen */
getch(); /* Wait for user input */
endwin(); /* End curses mode */
return 1;
}
What are these characters? And how to remove them?
These characters are what initscr() outputs to do its job.
A terminal knows not to show these characters and interpret them in a special way. Since the Eclipse console is not a terminal, it has not a faintest idea.
If you want your program to work in both terminals and non-terminals, you need to check whether your standard output is a terminal, and avoid using ncurses-specific functions if it is not. See man isatty.
If you only need your program to work in terminals, just don't use Eclipse console. See this question and its answer for set-up instructions.
While running a program in Visual Studio C++ editor, when i click on Local Windows Debugger, the command prompt window opens up, where i am asked to input data (depending on my program). The problem is the window doesn't stay very long after the output is shown.
What do i need to do to keep the window up for a longer time, or at least until i close the window myself?
I tried for over half hour to check various options and see if there is anything that can be done to prolong the duration.
Thanks and regards,
Nikhil Kenvetil
Try to use one of the below methods to add some codes at the end of the main() function:
getchar();
char ch;
cin>>ch;
Try this:
#include <iostream>
#include "conio.h"
int main()
{
std::cout << "Hello World\n";
getch();
return 0;
}
you could try using system("PAUSE");
When creating an console application (blank document) how can I get the "Press Any Button To Continue" to automatically show up?
You can add it manually with a
system("pause");
*Be carefull, this is not portable (will work on windows, but might not work elsewhere)
When you run a console program in the IDE using "Start Without Debugging" (Ctrl-F5) you get the behavior you're looking for.
For some reason, when you start the program in the IDE under the debugger ("Start Debugging" or plain-old-F5) you don't get that prompt when the program ends. If you just want to be able to see the last bit of what's in the console window when run under the debugger, you can set a breakpoint on the return from main() (or the closing brace for main()).
There is no builtin function. However you can do a simple loop with kbhit() and getch(), like so:
#include <conio.h>
void main( void )
{
// Display your message here
for(;;)
{
while( !kbhit() );
if (getch() == 0x0D)
break; // Break on ENTER
}
// Continue on here
}
Adapted from http://support.microsoft.com/kb/44895
system("pause") is definitely what you were asking for, but using it is very bad practice. Consider just using cin.get() at the end and press enter.