VSCode creating string in c++ doesn't work - c++

Trying to figure out why creating an instance of a string results in my program being shut down...
My simple program is:
#include <iostream>
#include <string>
int main(){
std::cout << "HELLO" << std::endl;
std::string str("str");
return 0;
}
My problem is that this program prints nothing. However:
#include <iostream>
#include <string>
int main(){
std::cout << "HELLO" << std::endl;
// std::string str("str");
return 0;
}
works perfectly fine and prints "HELLO".
I'm compiling with mingw this way: g++ main.cpp -o main.exe (doesn't show any errors)
I've tried using the string in any way (like printing it). In general I'm trying to create a string instance to do std::cin>> into it.
Help will be much appreciated! Thank you

Works for me:
$ cat test.cpp
#include <iostream>
#include <string>
int main(){
std::cout << "HELLO" << std::endl;
std::string str("str");
return 0;
}
$ g++ test.cpp
$ ./a.out
HELLO
$
The problem is elsewhere in your system, the code is OK.

Related

mingw compiled executable does not output characters in powershell/cmd

I tried to use the following code to output files in current directory
#include <filesystem>
#include <iostream>
using namespace std::filesystem;
int main() {
for (directory_iterator next("."), end; next != end; ++next) {
std::cout << next->path() << std::endl;
}
return 0;
}
the compile command is a simple g++ demo.cpp -o demo.exe, the path to g++ is C:\msys64\ucrt64\bin\g++.exe.
It worked properly when i ran it in bash (msys2),
$ ./demo.exe
".\\.clangd"
".\\.vscode"
".\\demo.cpp"
".\\demo.exe"
but when i did the same thing in powershell or cmd, it output nothing.
PS C:\Users\cnjawi> .\demo.exe
PS C:\Users\cnjawi>
The following code could run just fine in powershell and cmd.
#include <filesystem>
#include <iostream>
using namespace std::filesystem;
int main() {
std::cout << "test\n";
return 0;
}
However, if i add the original code, the issue occurs, even the "test" couldn't be output.
#include <filesystem>
#include <iostream>
using namespace std::filesystem;
int main() {
std::cout << "test\n";
for (directory_iterator next("."), end; next != end; ++next) {
std::cout << next->path() << std::endl;
}
return 0;
}
Thanks to HolyBlackCat's comment. When i double-clicked this program, Windows prompted The procedure entry point ~ could not be located, which gave the key info -- the program doesn't actually work in powershell due to some missing DLLs, rather than working but not outputting.
However, a collapsed program does not issue warnings in powershell, which made it appear to have worked "normally".
The simplest way to solve it is using the static library. For this instance, use g++ -c demo.cpp to generate demo.o and then g++ -static demo.o /ucrt64/lib/libstdc++fs.a -o demo.exe(in bash).

How to change the default main.cpp generated by CLion

Every time I create a new project in CLion the default main.cpp is written like this:
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
But I want it to look like this:
/**/
#include <iostream>
using namespace std;
int main(){
return 0;
}
I searched everywhere but can't undestand how to do it.
Thank you in advance.

std::ifstream::open inconsistent failure

I was working on a project involving fstream when I ran into this inconsistency. My project is failing to open a .txt file and in attempting to debug it I created test.cpp which, as far as I can tell, is functionally identical to my main.cpp, however they output differently when compiled and run.
main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cmath>
double get_pe(double price, double rent);
int main()
{
std::ifstream ifile;
std::string text;
std::vector<int> price;
std::vector<int> rent;
// ------------------------------ Problem 1 ------------------------------ \\
ifile.open("test.txt", std::ios::in);
if(ifile.is_open())
{
std::cout << "Works\n";
ifile.close();
}
else
{
std::cout << "Fails\n";
return 1;
}
return 0;
test.cpp
#include <iostream>
#include <fstream>
int main()
{
std::ifstream ifile;
ifile.open("test.txt", std::ios::in);
if(ifile.is_open())
{
std::cout << "Works\n";
ifile.close();
}
else
{
std::cout << "Fails\n";
return 1;
}
return 0;
}
Console Output
$ g++ main.cpp | g++ test.cpp -o t.out
$ ./a.out
Fails
$ ./t.out
Works
$ ls
a.out housingPriceAndRent.txt main.cpp streetAdresses.txt test.cpp test.txt t.out
$ g++ --version
g++ (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
I'm really interested in what's causing this, but I haven't the slightest clue.
Your issue is here:
// ------------------------------ Problem 1 ------------------------------ \\
Looks pretty benign right? Just a comment? But the problem is that escape character at the end. That's your way of saying "ignore this linebreak and treat the next line as if it was on this one."
So the next line:
ifile.open("test.txt", std::ios::in);
is actually part of that comment!! You never run that ifile.open()--so of course ifile.is_open() will be false!
I would expect any IDE worth its salt to have colored this this properly so you could quickly see that that line was a comment (that's how I caught it). Notice how for me, line 18 looks green like a comment:

g++ compiles c++ script with #include-statements to .exe-file which fails to run

I have the following script in C++:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector <string> markus = { "M", "A", "R", "K", "U", "S" };
for (int i = 0; i < markus.size();i++) {
cout << markus[i];
}
return 0;
}
I have successfully installed g++. When I try compiling this code with the command g++ -o test test.cpp, I get no errors. However, when I try running the created test.exe-file, with the command test, I get an error saying "could not find starting point". First, there is a long sequence, then the following message; "The starting point for the procedure could not be found in the library for dynamical links", and the the absolute path for test.exe.
If I remove #include <vector>, and try the following code;
#include <iostream>
using namespace std;
int main() {
cout << "Hello world";
return 0;
}
, it works perfect.
I would be glad for the help.

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
}