How is this program running in debug mode? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am confused about something I saw today
In a Linux terminal the next commands were given:
g++ hw.cpp
./a.out
./a.out debug
The second command executes the program and outputs "Hello, World"
The third command seems to run the program in a debug mode and makes the same program output "sending output to terminal" "Hello, World"
I did not see the source code
How can I make my own program only output certain lines in debug mode?

The source is in hw.cpp, which obviously is using some kind of argument parsing to look for 'debug'.
For example:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
if (argc > 1 && strcmp(argv[1], "debug") == 0)
printf("Hello World\n");
return 0;
}
If you would like to do argument parsing more involved then this I suggest you look to getopt

Related

Understand shell script interpreter with custom shell [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
I try to understood how shell script interpreter working.
for example i wrote custom shell with c++ :
#include <iostream>
#include <string>
using namespace std ;
int main()
{
string input;
while (1)
{
cout << "prompt:> ";
cin >> input;
if(input=="exit")
return 0;
else if(input=="test")
cout << "You executed test command\n";
else
cout << "Unknown command.\n";
}
}
now i wrote a script like this :
#!/PATH/TO/COMPILED/SHELL
test
wrong_command1
wrong_command2
exit
Actually this script not working and i want to understand what part of my thinking is wrong .
Note: I executed this script on /bin/bash shell .
can i say ,my c++ code is: interactive shell
How interpreters work on shell scripts ? #!/PATH/TO/COMPILED/SHELL
How can fix code or script to activate interpreting feature ?
No idea what that means
If you compile your program to /tmp/a.out and have an executable file script with:
#!/tmp/a.out
test
wrong_command1
wrong_command2
exit
which you invoke on command line as ./script then the shell running the command line will invoke /tmp/a.out ./script. I.e. looks at the shebang, invokes that command and passes the script as its first argument. The rest is up to that command.
There is no interpreting feature in C++, you have to write it yourself, what you have is a good start except you need to read from the passed file argument, not stdin. Also std::getline might come handy.

The output in my terminal is followed by strange characters [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 months ago.
Improve this question
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
return
}
When running this my terminal outputs the following:
cd "/Users//Desktop/C:C++/" && g++ main2.cpp -o main2 && "/Users//Desktop/C:C++/"main2
The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
3c0630455fab:C:C++ $ cd "/Users//Desktop/C:C++/" && g++ main2.cpp -o main2 && "/Users//Desktop/C:C++/"main2
Hello World!3c0630455fab:C:C++ $
What are these strange characters following the output?
It is showing those weird characters because you never added a new line after "Hello World!". It is printing your string, but since you didn't tell it to go to the next line, your terminal input is starting right after World!.
That is why 3c0630455fab:C:C++ rbrangri$, your terminal name/path, is printed directly after your string!
To fix this, you can append a \n inside of your string, or add a endline like this: << std::endl;

How do I can write a program where I can add information just like commands in Ubuntu (ex: program -u "Hello World") [duplicate]

This question already has answers here:
Pass arguments into C program from command line
(6 answers)
How to read/process command line arguments?
(22 answers)
Closed 5 years ago.
I am interested in writing a program in C or Python and add information to my software just like in command line, maybe I'm not explaining so good but what I want looks like this:
Ex: myProgram -u "Hello World"
Then after pressing enter to show my message on screen
In c, we can receive "arguments" from the operating system in the following manner:
int main(int argc,char **argv){
printf("Quantity of arguments: %i\n",argc);
for(int i=0;i<argc;++i){
printf("Argument %i: %s\n",i,argv[i]);
}
}

How to get a C++ program built in Visual Studio Code to accept user input [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm familiar with the Visual Studio IDE on Windows, and I wanted to give VSCode a shot. For my current situation, I am running VSCode on Ubuntu 64-bit. I installed VSCode, and then installed the C/C++ Extension. Next, I wrote a very simple C++ program that outputs "Hello World", and then asks the user to input their name. The program would then say/output hello to that user. Here's the issue: I am having trouble figuring out how to actually provide the user input to the program. During runtime, I see the cursor blinking in the output panel after the "Hello World" output, but when I press any keys on my keyboard to provide user input, nothing appears, and nothing happens. Any help would be greatly appreciated.
#include<iostream>
#include<string>
using namespace std;
int main() {
string name = "";
cout << "HELLO WORLD" << endl << endl;
cin >> name;
cout << "Hello " << name;
return 0;
}
Better use terminal then the Visual Studio Code Run feature

How to use graphviz neato from within C++ program [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Contination of this question
#include <boost/graph/graphviz.hpp>
#include <boost/graph/grid_graph.hpp>
typedef boost::grid_graph<2> Grid;
int main()
{
boost::array<std::size_t, 2> lengths = { { 3, 5 } };
Grid grid(lengths);
std::ofstream gout;
gout.open("test.dot");
boost::write_graphviz(gout, grid);
}
I run
system('neato -Tpng overlap=false test.dot > test.png');
from a c++ program. It is not working.i.e png file is not created
When I run the same command from a console prompt, it does work as expected.
If redirection doesn't work on your system's shell, use the option:
system("neato -Tpng overlap=false test.dot -o test.png");
Also be aware of your working directory. Make sure your input is in the current working directory, and also check that you are looking for the output (test.png) in that same directory.
Alternatively, spell out the paths
system("neato -Tpng overlap=false /path/to/dir/test.dot -o /path/to/dir/test.png");
CAVEAT: of course, in C++ strings backslashes need to be escaped, if your paths contains them