std::stringsteam seems to break std::cout - c++

#include <iostream>
#include <sstream>
int main() {
std::stringstream ss1;
std::cout << "hello";
}
For some reason, on VSCode on my Windows PC, the std::cout won't output anything when I have an std::stringstream line present but when I remove the line the output works. On my MacBook Pro this code runs normally in VSCode however.

Related

How to display stuff in VS Code debug console without `std::endl`?

When debugging an app using std::cout to print something, nothing appears in the debug console (3rd tab, not external console). One of the exceptions seems to be the use of std::endl:
#include <iostream>
#include <Windows.h>
using namespace std;
int main() {
while(true) {
Sleep(500);
std::cout << "Hello world!" << std::endl; // Works
}
}
#include <iostream>
#include <Windows.h>
using namespace std;
int main() {
while(true) {
Sleep(500);
std::cout << "Hello world!\n"; // Doesn't work
// std::cout << "Hello world!"; // Doesn't work
// std::cout << "Hello world!" << std::flush; // Doesn't work
}
}
In the first example our lines appear over time, while on the second doesn't appear at all (or the console refreshes somewhere around 60 secs). I think this is a bug but I'm not sure, so what is a workaround? Maybe I have to configure something instead? I found this problem while working on something else: C++: cannot see output in VS Code while debugging
Edit
The problem is what to do when u wanna output something without a new line

Command system("pause") not found on Linux

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
using namespace std;
int main() {
string line;
cout << "HW\n";
getline(cin,line);
cout << "Your line is - " << line << "\n";
system("pause");
return 0;
}
I want to do gui to factorio headless server by myself so i need to exec few bash scripts. I think i need function system() to that ?
I think I got problem with lib path. Please don't blame to wrong installed vcpkg. Paths is :
/opt/factorio/bin/x64/vcpkg/installed
/usr/include/c++/9/x86_64-redhat-linux
/usr/include/linux
/usr/include/c++/9/tr1
Command system() not found says Visual Studio.
system("pause"); is meant to be used only on Windows. It runs the Windows command-line "pause" program and waits for that to terminate before it continues execution of your program. That's why it's a bad practice to use it in your code, no matter if you are running your code on Windows or Linux.
Here is a better way you can achieve the same result:
#include <iostream>
using namespace std;
int main() {
do {
cout << '\n' << "Press the Enter key to continue.";
} while (cin.get() != '\n');
return 0;
}
instead of:
#include <iostream>
using namespace std;
int main() {
system("pause");
return 0;
}

missing output in the eclipse console when debugging

I recently started using Eclipse CDT (version 2019-03) with the Cygwin toolchain and have noticed some bizarre behaviour when using the debugger.
Under the debugger the following program behaves as you would expect
#include <iostream>
int main()
{
std::cout << "hello world\n" << std::flush;
}
However the following produces no output
#include <iostream>
int main()
{
std::cout << "* world\n" << std::flush;
}
And for the following the output is world
#include <iostream>
int main()
{
std::cout << "# world\n" << std::flush;
}
This behaviour is completely consistent and reproduceable. Does anyone have any explanation or workarounds?

How to interact synchroniously with a process in Boost.Process 0.5

This is a question about Boost.Process 0.5, not any later or earlier
version, Boost now contains a Boost.Process library with a different syntax and features.
Suppose I have a simple program that ask for a number and return another number, namely:
// ask.x, simple program with IO
#include<iostream>
int main(){
double n;
std::cout << "number?" << std::endl;
std::cin >> n;
std::cout << n + 1 << std::endl;
}
Now I want to interact with this program programmaticaly by means of Boost.Process 0.5 (http://www.highscore.de/boost/process0.5/boost_process/tutorial.html). When I try to use the library I don't get the expected behavior, the number is never sent to the program. (reading the first line is ok). I tried to write a generalization of the example described in http://www.highscore.de/boost/process0.5/boost_process/tutorial.html#boost_process.tutorial.synchronous_i_o but I failed.
MWE, the first half is a lot of necessary boilerplate, also where I think I make the mistake.
#include <boost/process.hpp> // version 0.5
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
#include <string>
#define ALSOSEND // for fully interactive case
using namespace boost;
int main() {
// Boilerplate code, see input only example here https://stackoverflow.com/questions/12329065/how-to-bind-program-termination-with-end-of-stream-in-boost-process-0-5
process::pipe pi = boost::process::create_pipe();
process::pipe po = boost::process::create_pipe();
{
iostreams::file_descriptor_sink sink(
pi.sink,
iostreams::close_handle
);
iostreams::file_descriptor_source source(
po.source,
boost::iostreams::close_handle
);
process::execute(
process::initializers::run_exe("./ask.x"),
process::initializers::bind_stdout(sink)
#ifdef ALSOSEND
, process::initializers::bind_stdin(source)
#endif
);
}
iostreams::file_descriptor_source fdsource(pi.source, iostreams::close_handle);
iostreams::stream<iostreams::file_descriptor_source> is(fdsource);
iostreams::file_descriptor_sink fdsink(po.source, iostreams::close_handle);
iostreams::stream<iostreams::file_descriptor_sink> os(fdsink);
// actual interaction with the process
std::string line;
std::getline(is, line);
assert(line == "number?");
std::cout << "sending: " << "5" << std::endl;
os << "5" << std::endl; // RUN GETS STUCK HERE
std::getline(is, line);
assert(line == "6");
}
Obviously I don't understand the logic of sinks and sources. I also tried using a single pipe for sink and source but it didn't work.
How can I make the program both read and write from and to the executed project?
I cannot find an example which both input and output are interleaved.
EDIT to show the working example with an earlier version of the library
This how it used to be done in Boost.Process GSOC2010 (not 0.5 as
in the question above), note that the actual interaction with the
program is the same as above.
#include <boost/filesystem.hpp> // quasibug in process GSOC2010 needs to include filesystem BEFORE
#include <boost/process.hpp> // version GSOC2010 (not 0.5)
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
#include <string>
using namespace boost;
int main() {
// boiler plate code
std::vector<std::string> args;
process::context ctx;
ctx.process_name = "askprocess";
ctx.streams[process::stdout_id] = boost::process::behavior::pipe();
ctx.streams[process::stdin_id ] = boost::process::behavior::pipe();
process::child c = create_child("./ask", args, ctx);
process::pistream is(c.get_handle(process::stdout_id));
process::postream os(c.get_handle(process::stdin_id ));
// actual interaction with the process
std::string line;
std::getline(is, line);
assert(line == "number?");
std::cout << "sending: " << "5" << std::endl;
os << "5" << std::endl; // RUN GETS STUCK HERE
std::getline(is, line);
assert(line == "6");
}
Seems to be a typo in the following line:
iostreams::file_descriptor_sink fdsink(po.source, iostreams::close_handle);
Must be:
iostreams::file_descriptor_sink fdsink(po.sink, iostreams::close_handle);

C++ std::cout and string printing in wrong order

I have a simple program running on Linux using g++ compiler:
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char **argv){
fstream file;
string s;
file.open("sample/dates.dat", fstream::in);
if(!file.good())
return 0;
getline(file, s);
cout << s << "." << endl;
return 0;
}
Compiled with: g++ -o test test.cpp. When I run this, the fullstop is printed BEFORE the string s, not after. Does anybody know why this is happening? And is it easy to fix?
Thanks.
If there is a carriage return at the end of the string it will move the position of output to the beginning of the console line when printed.
#include <iostream>
int main()
{
std::cout << "some line\r" << "." << std::endl;
// ^^ carriage return
}