copy a .cpp to visual studio, but can't get it to run

I'm doing an online course of cpp, and I'm using the Visual Studio 2019 IDE. The course gives text files of the lessons to 'play with", with the .cpp extension. If I click on the text file it opens a window in Visual Studio (VS), tab heading is (for example) Program 19_9.cpp; There are 3 sub tabs: "Miscellaneous files" contains the copied code, (global scope) which is empty, and main() which is empty.

I am not able to run/debug it.

If I copy it to a ConsoleApplication tab from another program that I wrote (overwriting my old program), it runs.

I know that problem is that that the new text code is not being opened in the right way, but is there a simple way to get the code to open in an executable window?

Thanks

Jason
Just opening up a .cpp file with VS 2019 won't do anything, as you've found out, you need to create a new solution/project and include the file.

https://www.learncpp.com/cpp-tutorial/writing-your-first-program/
Thanks for the (great) direction, Furry, I read it thoroughly and learned from it.

I think that I'm asking a related question; is there a way to create a project on the already coped .cpp text, rather than creating a new project then recopying the text into it?
The short answer, @jsurow, is no.

There are ways to import projects, but there has to be a project to import (from some other format, like CMake or 'make' files).

There are reasons, too many to list, but the basic point is that configuring a project is a big deal. Many of the single source CPP file examples are built as console projects in visual studio.

That comes to an end if your example is for Windows, particularly with GUI code. The project must be built to link in the Windows libraries, and reference one of the SDK's you have installed.

Now, what I often do is hold a few console application projects in a few directories, which assume one CPP file source. They are pre-configured for console builds, usually, and in 32 or 64 bit flavors. When I have code to try from this site, or some other basic example, I merely paste the code into the otherwise empty CPP main file of that project (though I do keep a bunch of typical includes, like stl containers).

I chose that so I don't create a project for every little sample, and those samples are rarely worth keeping, so they get replaced with the next one.
Niccolo wrote:
The short answer, @jsurow, is no.
Not true. I import already created source files into projects all the time. And have zero problems with configuration details.

If you want to import an already created .cpp/.h file(s) to a project it is insanely easy.

1. Create an empty project/solution. No files added. The default name is Project1. The number changes if you have more than one default named project. The configuration defaults to a console app.

I repeat. CREATE AN EMPTY PROJECT/SOLUTION.

I always change the project and solution names to something descriptive of my program. For example, I might name the project MyGame1 and the solution name to MyGames. I can now have multiple similar projects under the umbrella of one solution.

2. Copy your .cpp/.h file(s) to the project/solution root directory.

3. From the project menu Add Existing Item. From the file/folder dialog select the file(s) you want to add to the project. The added file(s) should now show up in the Solution Explorer file tree. Visual Studio now will be able to compile and link the file(s).

Personally every project I start I begin with an empty project, adding existing and new files as I need them. The empty project defaults set the configuration settings among other things to NOT have precompiled headers, something I despise.

Precompiled headers can cause compile/link problems in my experience, even with a complete project/solution rebuild. I have yet to create a project that using precompiled headers has made a significant time saving when editing/compiling/reediting phase.

If you want to create a project/solution for a Windows GUI app you can and should use the Windows Desktop Wizard. You can still create an empty project, with the ability to change key configuration details as needed.
Topic archived. No new replies allowed.