0

I'm using the g++ compiler and used the https://code.visualstudio.com/docs/languages/cpp guide to install everything. Never had any sort of problem.

I can initialise a vector from the standard library, but as soon as I attempt to initialise it with values, or add to it, or print its size after adding to it, I get a blank line in the console. There is no compilation error and i've tried compiling with the -std flag as 'c++11' and 'c++17'. The odd thing is that even if I put a cout statement before I add to the array, then it won't even output anything - it's like it just halts the whole program.

I am using vscode, and I've read of vaguely similar issues but none of the problems are identical and none of the solutions have worked. My code is below:

Imports:

#include <iostream>
#include <limits>
#include <vector>
#include <string>

Main function:

int main()
{
    std::cout << "BEFORE";
    std::vector<int> data;

    std::cout << data.size(); // Sometimes outputs 0 if the vector is not modified but prints nothing if it is(even after the statement)

    data.push_back(20);
    std::cout << data.size();

    std::cout << "AFTER";
}

Output: Just a blank line

UPDATE: Flushing the buffer and updating mingw haven't changed anything.

  • 2
    there are some buggy versions of mingw that don't seem to flush the console buffer on exit, you should update to a more recent version of mingw if that's the case or add a flush to the end of your program – Alan Birtles Aug 30 '22 at 11:04
  • 3
    Another possibility is that your mingw dlls are not on your path so your program is simply failing to execute at all – Alan Birtles Aug 30 '22 at 11:06
  • Whatever this problem is, it seems unlikely to be related to vectors specifically. – john Aug 30 '22 at 11:18
  • Does adding a newline `"\n"` at the end of your lines make any difference? – Stephen M. Webb Aug 30 '22 at 12:21
  • Even the _"BEFORE"_ and _"AFTER"_ are not displayed. It's not a `std::vector` issue but more likely to be related to `std::cout` instead. Maybe try to flush the buffer. – Fareanor Aug 30 '22 at 12:23
  • @AlanBirtles I've fully updated my mingw, still nothing. I've tried clearing and flushing cout. Still giving me the same output, so bizarre. – Benjamin McGregor Aug 30 '22 at 12:43
  • @AlanBirtles how would I go about checking and fixing mingw dlls on the path? – Benjamin McGregor Aug 30 '22 at 12:44
  • Put vscode aside for a moment. Open the mingw console. Run the compiler from there. Run the resulting program from there. Does it work? If not, the problem is in your mingw install and we should not bother with vscode until it is resolved. If it does work, the problem is in vscode and/or the way it integrates with gcc. – n. m. could be an AI Aug 30 '22 at 13:01
  • @n.1.8e9-where's-my-sharem. I believe you have solved my problem. When using the mingw64 console outside of Vscode, the output is correct. It seems that mingw64 console within Vscode does not work. Thank you. – Benjamin McGregor Aug 30 '22 at 13:15

1 Answers1

0

Thanks to @n. 1.8e9-where's-my-share m. I was able to find an answer. The problem was to do with Vscode not being able to display the vector. I ran the compiled .exe using the mingw64 console (outside of vsc, not the internal one) and it produced the correct output.