cin sometimes compiling and never working.

The Codeler 40 Reputation points
2024-12-20T04:51:05.96+00:00

I'm trying to figure out how to use vs code, but stupid thing is mad at me.

When I put in this code:

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello world!";
    int x;
    cin >> x;
    cout << x;
}
```Nothing happens, it doesn't even get to printing hello world.

When I try this though:


```cpp
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};

    for (const string& word : msg)
    {
        cout << word << " ";
    }
    cout << endl;
}

then it prints the first message, but isn't receiving any data.

What am I doing wrong in vs code (technically Visual Studio Code) and how can I fix it?

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,815 questions
0 comments No comments
{count} votes

Accepted answer
  1. Minxin Yu 12,341 Reputation points Microsoft Vendor
    2024-12-20T06:53:36.3233333+00:00

    Hi, @The Codeler

    Make sure you flush the stream.

    Add std::endl

    std::cout << "Hello world!" << std::endl;
    

    or use

    std::cout << "Hello world!";
    std::cout.flush();
    

    Also check that the running program is up to date.

    Try to use external console: "console": "externalTerminal",

    .vscode

    ├──launch.json

    {
        "configurations": [
            {
                "name": "C/C++: cl.exe build and debug active file",
                "type": "cppvsdbg",
                "request": "launch",
                "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "args": [],
                "stopAtEntry": false,
                "cwd": "${fileDirname}",
                "environment": [],
                "console": "externalTerminal",
                "preLaunchTask": "C/C++: cl.exe build active file"
            }
        ],
        "version": "2.0.0"
    }
    

    ├──tasks.json

    {
        "tasks": [
            {
                "type": "cppbuild",
                "label": "C/C++: cl.exe build active file",
                "command": "cl.exe",
                "args": [
                    "/Zi",
                    "/EHsc",
                    "/nologo",
                    "/Fe${fileDirname}\\${fileBasenameNoExtension}.exe",
                    "${file}"
                ],
                "options": {
                    "cwd": "${fileDirname}"
                },
                "problemMatcher": [
                    "$msCompile"
                ],
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "detail": "Task generated by Debugger."
            }
        ],
        "version": "2.0.0"
    }
    

    In addition, the original code works well in Visual Studio.

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.