C++ program won't print anything with vectors?

Viewed 408

I compiled my program and run it but nothing happened when I tried to narrow the problem down, it turned out anytime I defined a vector nothing would be sent in the output. In this program, nothing is printed, even the std:cout at the start, and I don't understand how to fix this problem. A simple "hello world" works though. Also, I'm on Windows 10 and use VSCode.

    #include <iostream>
    #include <vector>


    int main( void )
    {
        std::cout << "hello\n";
        std::vector<int> g1; 
    
        for (int i = 1; i <= 5; i++) 
            g1.push_back(i); 
    
        std::cout << "Output of begin and end: "; 
        for (auto i = g1.begin(); i != g1.end(); ++i) 
            std::cout << *i << " "; 
    
        std::cout << "\nOutput of cbegin and cend: "; 
        for (auto i = g1.cbegin(); i != g1.cend(); ++i) 
            std::cout << *i << " "; 
    
        std::cout << "\nOutput of rbegin and rend: "; 
        for (auto ir = g1.rbegin(); ir != g1.rend(); ++ir) 
            std::cout << *ir << " "; 
    
        std::cout << "\nOutput of crbegin and crend : "; 
        for (auto ir = g1.crbegin(); ir != g1.crend(); ++ir) 
            std::cout << *ir << " "; 
    
        return 0; 
    } 

In terminal (Windows Powershell) I write this command

g++ test.cpp -o test
.\test

Thank you for answering

2 Answers

With the same idea as what was advised by Harry. Try to flush systematically after a cout (not doing it may not guaranty the print when you expect it). using << std::endl will end the line and flush the ostream (recomanded after each cout). And if you don't want to end the line use simply << std::flush

Found answer: turns out you need to put a dll file libstdc++-6.dll into the .exe dir and solved my problem

Related