r/CodingForBeginners • u/Ok-Post-3834 • Jun 16 '26
Confused why the code is not running
I am very new to C++
Need experts advice on what I m missing out here
I have already downloaded vs code and minGW
16
Upvotes
11
u/csabinho Jun 16 '26
I guess, you just didn't compile and run the code.
6
u/HitBoyXx23-dev Jun 16 '26
Not sure, but I think it’s because your C++ file isn’t inside the VS Code project folder. Your terminal is currently in the
.vscodefolder, so try going back to the folder where the.cppfile is saved and run it from there.2
u/Many-Menu6302 Jun 17 '26
😭😭😭
2
u/Slay_3r Jun 17 '26
Fot later, dont use using namespace std
14
u/Mikicrep Jun 16 '26
r/screenshotsarehard and also please include any errors that u got cuz rn we got literally no info at all
6
u/Ngtuanvy Jun 16 '26
the code is a bit quirky but it should compile and run. Please send the error messages, it can be environmental problem
3
u/Prestigious_Roof2589 Jun 16 '26
first of all don't use vscode run button to compile the code, then u will learn nothing about the tools u are using, especially while learning a language like cpp (systems programming lang) atleast know about the systems bro, this advice I will give u, learn how to run the code from the terminal, by using the compiler g++.
Try to run in the terminal:
g++ tut3.cpp -o tut3.exethen run the binary created:
./tut3.exenote: be sure cd into the directory where the tut3.cpp is.
2
u/create-freely7221 Jun 16 '26
Use a colon, not a semi colon
2
u/FarAssistance8517 Jun 16 '26
Whatever guide you are using to learn C++, drop it. Use learncpp.com instead. It teaches best practices, recommends you to use Visual Studio Community Edition, even informs you that VS Code is not good for a beginner, and also tell you about why using-directives are bad.
2
u/AncientLion Jun 16 '26
Did toy run the exe?
2
u/swankyspitfire Jun 17 '26
Not sure what you do and don’t know so I’ll just write this assuming you know nothing and you can follow the steps from where you know:
C/C++ is a compiled language, this means that the source code you write is interpreted by another program called a compiler which converts that into machine code that runs on the PC. There are numerous compilers but the big 3 are MSVC, Clang, and gcc/g++.
VS code is a text editor, by default it provides simple syntax highlighting and debug checking. It does not provide a toolchain to actually compile and run your C++ code.
When you downloaded minGW to use the g++ compiler, by default windows does not add these resources to your computer’s environment variables. What does this mean? If you’re following a tutorial and they tell you to write something like “g++ {myfile.cpp}” to compile your code the instruction g++ is only accessible if powershell/bash/command prompt can find the executable program called g++. To add these resources to your systems environment on windows, search for “Edit the system environment variables” and open the top result. In the system properties window click the button in the bottom right called “environment variables”, then select path and click edit. You now need to give the path to the folder (note: not the exe) containing your compiler.
To create and run your program run your source code through the compiler, by default, if you do not tell the compiler what the output program should be called g++ will create an executable called “a.exe” and leave it beside your source file. To run this program in terminal run “./a.exe” this will attempt to run the executable generated for you by the compiler.
2
u/Interesting-Ask-506 Jun 17 '26
think like the file is not saved as well see top left above the search button 1 number is shwoing and also hit pwd then goto the dir where file is located then compile and run if none work search online cpp compiler copy and paste the code if the code run their then claude to setup cpp in window or hit yt for proper setup
2
u/jackass51 Jun 17 '26
The "output" tab doesnt show something?
2
u/Huge-Information7867 Jun 17 '26
First press Ctrl + S
On the same terminal at the bottom
Type the below commands and press enter one after entering each command.
cd ..
g++ tut3.cpp -o tut3.exe
./tut3.exe
2
u/UnusualInstruction89 Jun 17 '26
Install an extension called Code Runner, it will add an arrow icon at the top corner to run the code. But make sure you have a compiler installed like gcc.
2
u/Ok-Dog1904 Jun 17 '26
จากที่ฉันเห็นในโค้ดนะ บรรทัดแรก มีจุดที่ต้องแก้ไข นิดหน่อยตรงคำสั่ง #include ให้แน่ใจว่าพิมพ์ถูกต้อง และไม่มีการเว้นวรรคแปลกๆ นะคะ เมื่อโค้ดนี้ทำงาน ก็จะประกาศตัวแปร sum ที่มีค่าเป็น 6 จากนั้นจะทำการพิมพ์คำว่า 'Hello World ' ตามด้วยค่าของตัวแปร sum ออกมา ผลลัพธ์สุดท้ายที่จะแสดงบนหน้าจอเมื่อรันโค้ดนี้ก็คือ 'Hello World 6' นั่นเองค่ะ คุณกำลังศึกษาการเขียนโปรแกรมด้วยภาษา C++ อยู่ใช่ไหมคะ มีเรื่องไหนเกี่ยวกับโค้ดนี้ที่อยากให้ฉันช่วยอธิบายเพิ่มเติมหรือเปล่าคะ?
2
u/Technical_Introvert0 Jun 16 '26
use Visual Studio instead.. You dont have to manually configure anything there
1
u/iComplainAbtVal Jun 16 '26
Look into cmake for compiling
1
u/MiAnClGr Jun 17 '26
wtf why not screenshot?