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

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;

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.

How to supress display of \n after input in c++? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
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?
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

How is this program running in debug mode? [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 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

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

ifstream returning true for files that don't exist [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 7 years ago.
Improve this question
What I am trying to do?
I'm creating a c++ program where I am checking as of the file exists for the execution of command in CWD (Current working directory) if not then bin and if not there then sbin folder.
What I am doing?
I am checking the existence of application using ifstream. If the object is created I consider it as valid. otherwise i don't. Here is a code sample of what I have done
string pathforCWD = argv[1];
ifstream checkFileCWD(pathforCWD.c_str());
if (!checkFileCWD) {
cout << "\nCommand not Found in working directory\n";
cout<<endl<<"";
string pathforBin = "/bin/",argv[1];
ifstream checkFileBin(pathforBin.c_str());
if(!checkFileBin)
{
cout<<"\nCommand also not found in Bin\n";
string pathforSbin = "/sbin/",argv[1];
ifstream checkFileSbin(pathforSbin.c_str());
if(!checkFileBin)
{
cout<<"\nFile also not found in sBin\n";
}
else
{
cout<<"\nCommand found in SBin directory.. Executing it."<<endl;
int response = system((orignalCommand).c_str());
programExecution(response);
}
}
else
{
cout<<"\nCommand found in Bin directory.. Executing it."<<endl;
int response = system((orignalCommand).c_str());
programExecution(response);
}
} else {
cout<<"\nCommand found in working directory.. Executing it."<<endl;
int response = system(("./" + orignalCommand).c_str());
programExecution(response);
}
What Issue I am facing?
If I pass an invalid program name argument to the program which does exist. The current working directory ifstram object is not created which means that file does not exist there. How ever for the /bin/. It says that the object exists each time. even if it doesn't. What I am doing wrong?
The following definition doesn't do what you think it does:
string pathforBin = "/bin/",argv[1];
Hint: you're declaring two names here, pathforBin and argv.