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"?
Related
I'm facing a weird issue with std::cout, ANSI color codes and PowerShell (integrated terminal in VS Code).
I have written a sample program that asks the user for string input. In the prompt I make use of ANSI color codes, so the default value (in case the user doesn't input anything) is displayed gray instead of white.
Running the executable works as intended the first time, but when running it a second time there's a weird spacing that disappears after the user presses Enter.
This issue persists until the code is compiled again.
I have noticed that when I leave out the ANSI color codes, this spacing also disappears. I'd still like to use colors though.
Please see below for sample code and images showing the output.
What causes this issue? Is there a different way of using colored std::cout output in C++?
Thanks a lot for your time.
Sample code
#include <iostream>
#include <string>
int main() {
std::string input;
std::cout << "Input: \x1b[37;2m(default value)\x1b[0m ";
std::getline(std::cin, input);
if(input.empty()) {
input = "default value";
}
std::cout << input << std::endl;
return 0;
}
Running the executable for the first time
Running the executable thereafter
Additional information
OS: Windows 10
Terminal: VS Code Integrated PowerShell
C++ compiler: g++ 8.1.0
Alright so I have been trying to fix this for a while, but with NO SUCCESS, I've come here to search for help instead. I'd appreciate it!
So basically, when I debug with gdb it should give me a console window saying "Hello World" (cause that is what I wrote in my code) right? Well you guessed, there is no console window for me.
This is the code i've written:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
This is supposed to give me an "Hello World" output in the console window but it is not, cause the console window is not even there when I debug. I've double checked almost everything and I can't find ANY SOLUTION at all.
Thanks for listening as this is very frustrating for me.
If you are using VSCode in launch.json file with a configuration of debug execution you can use
"externalConsole": true
which allows you to see your output not in the embedded terminal.
If I run the code::blocks default console c++ "hello world" app (see below), I only see this in the console that opens :
Process returned 0 (0x0) execution time : 0.011 s
Press any key to continue.
I don't see "hello world". What could be wrong ?
If I run the ./helloworld.exe with cygwin, I do see "hello world". But I don't see it with cmd or powershell.
The app :
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
There is a related discussion you might want to check out here:
http://www.tomsguide.com/forum/244674-49-basic-program
It could be a matter of the program executing too quickly, per the forum I referenced above, but that seems like an odd reason. You might try what they recommend (pause, etc.), but you may also have an issue with your compiler or how your properties are set up.
For cmd, check this out:
https://www.thecrazyprogrammer.com/2015/09/how-to-run-c-and-cpp-program-in-cmd.html
For the command line/Windows prompt, check this out:
https://msdn.microsoft.com/en-us/library/ms235639.aspx
Thanks to #George.
Solution was to remove the -mwindows compiler flag.
But to make it work, I needed to delete the .exe before building again.
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.
I'm trying to execute a very simple program that runs "ls" command
Im working under Mac OS 10.7, with XCode and C++
This is the code:
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello world" << endl;
execl("/bin/ls","ls",NULL);
return 0;
}
It crashes after following output
Hello world
No memory available to program now: unsafe to call malloc
I tried to google it but no luck, any ideas on what I might be doing wrong?
This is just "my opinion"
From man page:
The exec family of functions replaces the current process image with a
new process image.
It could be that it tries to replace the debugger process and so it crashes (the app is run from Xcode..). If you execute the app from command line it works...
Seems to work fine:
http://ideone.com/8AoZ3
But seems like on your platform some sort of weird recursion is taking place. Can you change your call to:
execl("/bin/ls","/bin/ls",0);
I know this may not be exactly what you want to do, but the following SO question is using execv to execute echo:
how-to-create-a-process-on-mac-os-using-fork-and-exec