how to run c++ on cmd

To run C++ code on the command prompt (CMD), you need to follow these steps:

  1. Open the Command Prompt: Press the Windows key, type "cmd," and select the Command Prompt from the search results. This will open the CMD window.

  2. Navigate to the Directory: Use the cd command to navigate to the directory where your C++ file is located. For example, if your file is in the "C:\myfolder" directory, you would type cd C:\myfolder and press Enter.

  3. Compile the C++ File: Use the g++ command, which is the GNU Compiler Collection for C++, to compile your C++ file. Type g++ filename.cpp -o outputfilename and press Enter. Replace "filename.cpp" with the name of your C++ file and "outputfilename" with the desired name of your compiled file. The -o flag specifies the output file name.

  4. Run the Compiled File: Once the compilation is successful, you can run the compiled file by typing its name and pressing Enter. For example, if your compiled file is named "outputfilename.exe," type outputfilename.exe and press Enter.

If your C++ code doesn't have any errors, it will be compiled successfully and the output will be displayed in the CMD window.

Note: To run C++ code on CMD, you need to have a C++ compiler installed on your system. One popular option is the GNU Compiler Collection (GCC), which includes the g++ compiler. Make sure you have installed GCC or any other compatible C++ compiler before following the above steps.