4

I am using VS-Code and Vim, Windows OS, MSYS2 MingW GCC/G++ (V.11.2.0) compiler.

If my code contains a vector there is no output when I run the code. I get no error/warning when compiling or running the code.

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main () {

vector<string> test = {"Why", "Wont", "This", "Run", "??\n"};
cout << test[0] << test[1]<< test[2] << test[3] << test[4];

cout <<"This is a test";
return 0;

}

If I comment out:

//vector<string> test = {"Why", "Wont", "This", "Run", "??\n"};
//cout << test[0] << test[1] << test[2] << test[3] << test[4];

I get the output: "This is a test"

I have tried reinstalling the compiler with no luck.

Dsylexia
  • 49
  • 1
  • What exit code does the program return? – user17732522 Feb 05 '22 at 03:12
  • 2
    Very similar to [this question](https://stackoverflow.com/q/70809283/11082165) from two weeks ago, also using MinGW GCC 11.2. – Brian61354270 Feb 05 '22 at 03:17
  • It worked for me under windows 10, msys2 / gcc-11.2 and vscode: [https://ibb.co/VDGLRVX](https://ibb.co/VDGLRVX) – drescherjm Feb 05 '22 at 03:24
  • 1
    If you put one element in that `vector` would it print? Two, three? Why do you have five? – Slava Feb 05 '22 at 03:25
  • The exit code is zero.. It works fine in CodeBlocks, but cant understand why it wont work in VScode or Vim – Dsylexia Feb 05 '22 at 03:26
  • Did you try changing `cout << "This is a test";` to `cout << "This is a test" < – drescherjm Feb 05 '22 at 03:27
  • ***The exit code is zero.. It works fine in CodeBlocks, but cant understand why it wont work in VScode or Vim*** If you are using the same compiler you used in VSCode its very likely a different terminal. – drescherjm Feb 05 '22 at 03:28
  • Wont print anything if there is a vector https://gyazo.com/d88ebf02d45e70381611434d3a1e5661 https://gyazo.com/587aeb8af716f7480a65a0fd07bf9587 – Dsylexia Feb 05 '22 at 03:29
  • @Dsylexia Please note that when you run a program in any IDE, you are not running on the actual command-line. You're running on a "fake" command window, thus you will be susceptible to any quirks or shortcomings that IDE window has when it comes to displaying output. The correct, at least the most safest way to run the program is to open up an actual terminal window or command-line window, and run the program from there. If you use `std::cout << std::endl;` on the last line, do you then see output? – PaulMcKenzie Feb 05 '22 at 03:33
  • https://gyazo.com/bbdbb997f7e2149b0af21b60fab36bdc Still no output.. – Dsylexia Feb 05 '22 at 03:35
  • @Dsylexia And if you go to the actual command-line and run it, what do you get? Also, if you are a beginner, might as well get used to how C++ programmers run command-line programs, and that is to go to the command-line and run the program. The exception would be for IDE's like Visual Studio, where the command-window is an actual command window, and not something baked into an IDE panel somewhere. – PaulMcKenzie Feb 05 '22 at 03:37
  • 1
    @Dsylexia The screenshots do not show the exit code. Add `; echo $?` after `.\test` and see what number you get. – user17732522 Feb 05 '22 at 03:37
  • 1
    @Dsylexia *I have tried reinstalling the compiler with no luck.* -- Before you did this, you should have confirmed it is a compiler problem, by (and I hate repeating myself, but here goes), running the program on the command line. If the command-line version works, then there is nothing wrong with the compiler. The fault would lie in the IDE that you're using. – PaulMcKenzie Feb 05 '22 at 03:43
  • It says "false" if i add ; echo $? after. VSCode is supposed to give an alert if the exitcode is "non-zero" – Dsylexia Feb 05 '22 at 03:43
  • @PaulMcKenzie There is still no output when running the code on a command-line https://gyazo.com/4d2371530eb64ee8559e2d0cf0341110 – Dsylexia Feb 05 '22 at 03:47
  • 1
    OK, then once you've taken the IDE out of the picture, I would probably conclude that you have a broken compiler installation in some way. – PaulMcKenzie Feb 05 '22 at 03:49
  • @Slava Knew what? I was only showing with one string since you asked. Does not matter what the content of the vector is or how many elements there are, or whether it is an int, string char etc. As long as there is an vector in my program there is zero output when running the program. I am not playing a "troll". This happens to all my programs, cant figure out why ._. – Dsylexia Feb 05 '22 at 16:20
  • Remove the two question marks in the last string. What happens then? – Martin York Feb 06 '22 at 01:41
  • "Knew what?" which string to keep. "Does not matter what the content of the vector is or how many elements there are". Really? Did you try empty vector? Did you try one empty string? Did you try one string one letter long? Did you try vector of `int` or some other types? – Slava Feb 06 '22 at 06:33
  • @MartinYork https://gyazo.com/c09c50ec7946e08a1e86722e982f706e As long as my vector contains some value there is no output. – Dsylexia Feb 06 '22 at 12:45
  • @Slava Yes I have tried, let me show you! :) https://gyazo.com/ebd78c2c7ea653fdc57d5fa94a92ccc8 vector with no content, as I stated in the tittle "C++ code wont run if a vector contains value", this vector does NOT contain any value! as expected we have some output. Some other tests if you are interested https://gyazo.com/8bb329c25eb059c0dd7ae7d5e4b4d046 https://gyazo.com/baba88472e4980d4a741be9283310054 https://gyazo.com/f4653b3410dc963ae61bfcd2ab8a3d03 https://gyazo.com/7ca8ed14a33452b6784b03343ac04b60 https://gyazo.com/0c267e31d5b63f0e90381bc308d901dd - string one letter long – Dsylexia Feb 06 '22 at 12:57
  • The line `if ($?) {./test}` only runs the executable if it compiles. Which suggests that you have a compilation error. – Martin York Feb 07 '22 at 05:30

3 Answers3

4

I spent about 20 hours fighting this one and I lost -- at least on windows. My very smart friend said that there was "something wrong with my runtime" and recommended a fresh install of windows to try fixing it for that environment (I haven't tested that yet because I am lazy)

What did work was installing WSL (Windows Subsystem for Linux) and grabbing Ubuntu. C++ works as expected now, just launch VS Code through the bash terminal you'll get

Note: I had this same issue with a very similar setup. Laptop with VS Code, Windows 10, MSYS2 MingW GCC/G++ (V.11.2.0). If your error is very similar to mine, your code should run when the vector exists but does not have any contents. I could run C++ programs unrelated to vectors, or use vector's size() and empty() functions on empty vectors that were created. Allocating contents to a vector either on initialization or with push_back() was what prevented me from getting output. If you're able to compile other things and you have this issue without seeing extra info from -Wall, the compiler is probably fine but not the runtime. I feel that this is moreso correct because I made test programs, compiled the .exes, and they could be run by another computer, just not my own.

My other attempted fixes included uninstalling and reinstalling MSYS2 and its compilers, VS Code, Microsoft Visual C++ Redistributables, several restarts, and more. Windows itself is the last ditch effort, I'm just waiting on making a backup.

Harris B
  • 71
  • 6
3

Here's what works for me: stackoverflow.com/a/6405064/6871623

Basically add -static-libstdc++ when compiling.

I had the same issue with similar environment, except it's broken after I installed Visual Studio 2022 for C/C++ development which includes Windows 10 SDK tools.

danzel artamadja
  • 102
  • 2
  • 10
  • Had the same issue with basically the same setting but `g++ 12.1.0`. Having `g++ helloworld.cpp -static-libstdc++ -o hello.exe` does the trick. – Antoine Feb 15 '23 at 11:51
-1

Looks like C Trigraph sequence.

The ?? is special sequence from the old C days.

When followed by certain characters will convert to another. You sequence does not seem to be a valid Trigraph sequence but there may be an issue in VS. Trying putting space between the question marks.

Martin York
  • 257,169
  • 86
  • 333
  • 562