|
| 1 | +--- |
| 2 | +title: Use AI to create a C++ console application in Visual Studio |
| 3 | +description: "Learn how to use GitHub Copilot to create a C++ app using Microsoft C++ in Visual Studio." |
| 4 | +ms.date: 10/24/2025 |
| 5 | +ms.topic: "tutorial" |
| 6 | +ms.custom: |
| 7 | + - ai-assisted |
| 8 | + - copilot-scenario-highlight |
| 9 | +--- |
| 10 | + |
| 11 | +# Use AI to create a C++ console application in Visual Studio |
| 12 | + |
| 13 | +This tutorial shows you how to use GitHub Copilot to quickly create a C++ console application in Visual Studio. You create a console version of Conway's Game of Life, a classic cellular automaton. |
| 14 | + |
| 15 | +Conway's Game of Life was devised by mathematician John Conway. It consists of a grid of cells that can be either alive or dead. The game evolves automatically based on simple rules and produces complex, evolving patterns that demonstrate how intricate behavior can emerge from basic mathematical rules. |
| 16 | + |
| 17 | +GitHub Copilot is an AI-powered coding assistant that helps you write code faster, reduce errors, and explore new solutions. Some benefits of using Copilot when coding in C++: |
| 18 | +- Generate entire C++ functions or classes as you type. |
| 19 | +- Suggest code completions based on plain-language comments or prompts. |
| 20 | +- Get help with complex algorithms, data structures, and standard library usage. |
| 21 | +- Learn new APIs and modern C++ patterns through in-context examples. |
| 22 | +- Receive context-aware suggestions based on your comments or code. |
| 23 | +- Debug errors in your code. |
| 24 | +- Simplify and refactor existing code. |
| 25 | + |
| 26 | +## Prerequisites |
| 27 | + |
| 28 | +- Visual Studio 2022 or later with the **Desktop development with C++** workload installed. |
| 29 | +- GitHub Copilot. For more information, see [Get started with GitHub Copilot](/visualstudio/ide/visual-studio-github-copilot-get-started). |
| 30 | + |
| 31 | +To verify you have the C++ workload installed: |
| 32 | +1. Open Visual Studio Installer |
| 33 | +1. Select **Modify** next to your Visual Studio installation |
| 34 | +1. Ensure **Desktop development with C++** is checked: |
| 35 | + |
| 36 | + :::image type="content" source="media/desktop-development-cpp-workload.png" alt-text="Screenshot of the Visual Studio Installer with the Workloads tab selected. Desktop development with c++ is selected."::: |
| 37 | + |
| 38 | +1. If it isn't installed, select it and choose **Modify**. |
| 39 | + |
| 40 | +For more information about installing Copilot, see [Manage GitHub Copilot installation and state](/visualstudio/ide/visual-studio-github-copilot-install-and-states). |
| 41 | + |
| 42 | +## Create a project |
| 43 | + |
| 44 | +Visual Studio uses *projects* to organize the code for an app, and *solutions* to organize your projects. A project contains all the options, configurations, and rules used to build your apps. It manages the relationship between all the project's files and any external files. To create your app, first, create a new project and solution. |
| 45 | + |
| 46 | +1. Open Visual Studio and select **Create a new project**. |
| 47 | +1. Search for "Console App" and select the **Console App** template for C++. |
| 48 | + |
| 49 | + :::image type="complex" source="media/vs2019-choose-console-app.png" alt-text="Screenshot of the Create a new project dialog."::: |
| 50 | + The Create a new project dialog is shown with the Console App template selected. The template says: Run code in a windows terminal. Prints hello world by default. Has the tags c++, Windows, and Console. |
| 51 | + :::image-end::: |
| 52 | + |
| 53 | +1. Select **Next**. |
| 54 | +1. Set the project name to **Life** and choose the location for the project. |
| 55 | +1. Select **Create**. |
| 56 | +1. Once the project opens, find the `Life.cpp` file in Solution Explorer. |
| 57 | +1. Open `Life.cpp` and delete the default "Hello, World!" code to start with a clean slate. |
| 58 | + |
| 59 | +## Use Copilot to create an app |
| 60 | + |
| 61 | +You prompt Copilot by describing the functionality you want. In this section, you'll learn how to prompt Copilot to generate an implementation of Conway's Game of Life. |
| 62 | + |
| 63 | +1. Open the Copilot chat window by selecting the Copilot icon in the toolbar: |
| 64 | + |
| 65 | + :::image type="content" source="media/github-copilot-open-chat.png" alt-text="Screenshot of the GitHub icon dropdown. Open Chat Window is selected."::: |
| 66 | + |
| 67 | +1. In the chat window, enter the following prompt: |
| 68 | + ```copilot-prompt |
| 69 | + Create a C++ console application that implements Conway's Game of Life. The program should: |
| 70 | + - Use a 40x20 grid displayed with asterisks (*) for live cells and spaces for dead cells |
| 71 | + - Start with a random initial pattern |
| 72 | + - Display each generation for 500ms before showing the next |
| 73 | + - Allow the user to press any key to exit the program |
| 74 | + - Include proper headers and use standard C++ practices |
| 75 | + - Clear the console between generations to provide an animation effect |
| 76 | + ``` |
| 77 | +1. Copilot generates C++ code for Conway's Game of Life. |
| 78 | +1. Copy the generated code and paste it into your empty `Life.cpp` file. |
| 79 | +1. Build the project by pressing **F6** or selecting **Build > Build Solution**. |
| 80 | +1. Run the program by pressing **F5** or **Ctrl+F5**. |
| 81 | +
|
| 82 | +> [!NOTE] |
| 83 | +> The exact code generated by Copilot may vary slightly from run to run and model to model, but the core functionality should be consistent. If the generated code doesn't compile immediately, you can ask Copilot to fix any compilation errors. Copilot is powered by AI, so surprises and mistakes are possible. For more information, see [Copilot FAQs](https://aka.ms/copilot-general-use-faqs). |
| 84 | +
|
| 85 | +### Example of typical generated code structure |
| 86 | +
|
| 87 | +Your generated code will likely include these key components: |
| 88 | +
|
| 89 | +- Headers for console manipulation, random number generation, and timing |
| 90 | +- A 2D array or vector to represent the game grid |
| 91 | +- Functions to initialize the grid, apply Conway's rules, and display the current state |
| 92 | +- A main loop that continues until a key is pressed |
| 93 | +
|
| 94 | +Here's an example of the code that Copilot generated using the previous prompt: |
| 95 | +
|
| 96 | +```cpp |
| 97 | +// Code generated by GitHub Copilot |
| 98 | +#include <iostream> |
| 99 | +#include <chrono> |
| 100 | +#include <thread> |
| 101 | +#include <conio.h> // for _kbhit and _getch |
| 102 | +
|
| 103 | +using namespace std; |
| 104 | +
|
| 105 | +int step = 0; |
| 106 | +const int rows = 20; |
| 107 | +const int cols = 40; |
| 108 | +
|
| 109 | +void printGrid(int grid[rows][cols]) |
| 110 | +{ |
| 111 | + cout << "Step: " << step << endl; |
| 112 | + for (int i = 0; i < rows; ++i) |
| 113 | + { |
| 114 | + for (int j = 0; j < cols; ++j) |
| 115 | + { |
| 116 | + cout << (grid[i][j] ? '*' : ' '); |
| 117 | + } |
| 118 | + cout << endl; |
| 119 | + } |
| 120 | +} |
| 121 | +
|
| 122 | +int countNeighbors(int grid[rows][cols], int x, int y) |
| 123 | +{ |
| 124 | + int count = 0; |
| 125 | + for (int i = -1; i <= 1; ++i) |
| 126 | + { |
| 127 | + for (int j = -1; j <= 1; ++j) |
| 128 | + { |
| 129 | + if (i == 0 && j == 0) |
| 130 | + { |
| 131 | + continue; |
| 132 | + } |
| 133 | +
|
| 134 | + int ni = x + i; |
| 135 | + int nj = y + j; |
| 136 | + if (ni >= 0 && ni < rows && nj >= 0 && nj < cols) |
| 137 | + { |
| 138 | + count += grid[ni][nj]; |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + return count; |
| 143 | +} |
| 144 | +
|
| 145 | +void updateGrid(int grid[rows][cols]) |
| 146 | +{ |
| 147 | + int newGrid[rows][cols] = { 0 }; |
| 148 | + for (int i = 0; i < rows; ++i) |
| 149 | + { |
| 150 | + for (int j = 0; j < cols; ++j) |
| 151 | + { |
| 152 | + int neighbors = countNeighbors(grid, i, j); |
| 153 | + if (grid[i][j] == 1) |
| 154 | + { |
| 155 | + newGrid[i][j] = (neighbors < 2 || neighbors > 3) ? 0 : 1; |
| 156 | + } |
| 157 | + else |
| 158 | + { |
| 159 | + newGrid[i][j] = (neighbors == 3) ? 1 : 0; |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + // Copy newGrid back to grid |
| 164 | + for (int i = 0; i < rows; ++i) |
| 165 | + { |
| 166 | + for (int j = 0; j < cols; ++j) |
| 167 | + { |
| 168 | + grid[i][j] = newGrid[i][j]; |
| 169 | + } |
| 170 | + } |
| 171 | +} |
| 172 | +
|
| 173 | +int main() |
| 174 | +{ |
| 175 | + int grid[rows][cols] = { 0 }; |
| 176 | +
|
| 177 | + // Initial configuration (a simple glider) |
| 178 | + grid[1][2] = 1; |
| 179 | + grid[2][3] = 1; |
| 180 | + grid[3][1] = 1; |
| 181 | + grid[3][2] = 1; |
| 182 | + grid[3][3] = 1; |
| 183 | +
|
| 184 | + while (true) |
| 185 | + { |
| 186 | + if (_kbhit()) // Check if a key has been pressed |
| 187 | + { |
| 188 | + _getch(); // Consume the key |
| 189 | + break; // Exit the loop |
| 190 | + } |
| 191 | + printGrid(grid); |
| 192 | + updateGrid(grid); |
| 193 | + std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 194 | + cout << "\033[H\033[J"; // Clear the screen |
| 195 | + step++; |
| 196 | + } |
| 197 | +
|
| 198 | + return 0; |
| 199 | +} |
| 200 | +``` |
| 201 | +When you run the application, you should see an animated display of Conway's Game of Life with patterns evolving over time. To exit the program, press a key. |
| 202 | + |
| 203 | +:::image type="content" source="./media/life-exe.png" alt-text="Screenshot of Conway Life running in a command window, displaying the evolving grid of cells."::: |
| 204 | + |
| 205 | +In the preceding code example, the code generates a warning: `Return value ignored: '_getch'`. You can ask Copilot to fix it. Select the code editor and press **Alt+/** (Windows) to open the Copilot chat, then enter: |
| 206 | + |
| 207 | +:::image type="content" source="./media/github-copilot-fix-warning.png" alt-text="Screenshot of the Copilot chat window. The text: Fix warning C6031 is in the chat window."::: |
| 208 | + |
| 209 | +Copilot suggests a fix to handle the return value properly. To accept the changes, select **Tab**: |
| 210 | + |
| 211 | +:::image type="content" source="./media/github-copilot-fix-warning-accept.png" alt-text="Screenshot of the Copilot proposed changes. Tab to accept. Alt+Del to discard."::: |
| 212 | + |
| 213 | +Congratulations! You successfully used GitHub Copilot to create a fully functional Conway's Game of Life console application in C++. You learned how to: |
| 214 | + |
| 215 | +- Craft an effective prompt to generate C++ code |
| 216 | +- Use Copilot's chat interface to create an entire application from scratch |
| 217 | +- Fix compilation warnings with AI assistance |
| 218 | + |
| 219 | +## Improve your prompting skills |
| 220 | + |
| 221 | +For better results with Copilot, see these prompting resources: |
| 222 | + |
| 223 | +- [GitHub Copilot documentation](https://docs.github.com/en/copilot) - Official best practices and tips |
| 224 | +- [OpenAI's GPT best practices](https://platform.openai.com/docs/guides/prompt-engineering) - General AI prompting techniques |
| 225 | +- [Copilot prompting guide](https://github.blog/2023-06-20-how-to-write-better-prompts-for-github-copilot/) - Specific guidance for code generation |
| 226 | + |
| 227 | +## Troubleshooting |
| 228 | + |
| 229 | +### Missing console app template |
| 230 | + |
| 231 | +The **New Project** dialog should show a **Console App** template that has **C++**, **Windows**, and **Console** tags. If you don't see it, it might be filtered out of the list, or it might not be installed. First, check the filter dropdowns at the top of the list of templates. Set them to **C++**, **Windows**, and **Console**. The C++ **Console App** template should appear; otherwise, the **Desktop development with C++** workload isn't installed. |
| 232 | + |
| 233 | +To install **Desktop development with C++**, you can run the installer right from the **Create a new project** dialog. Choose the **Install more tools and features** link at the bottom of the **Create a new project** dialog, beneath the list of templates. If the **User Account Control** dialog requests permissions, choose **Yes**. In the installer, make sure the **Desktop development with C++** workload is checked. Then choose **Modify** to update your Visual Studio installation. |
| 234 | + |
| 235 | +### Copilot not responding |
| 236 | + |
| 237 | +- Ensure you have an active GitHub Copilot subscription. |
| 238 | +- Try signing out and back into your GitHub account in Visual Studio |
| 239 | + |
| 240 | +### Generated code doesn't compile |
| 241 | + |
| 242 | +- Ask Copilot to fix specific compilation errors by pasting the error message into Copilot chat. |
| 243 | +- Try refining your prompt to be more specific about what you want the app to do. |
| 244 | + |
| 245 | +## Next steps |
| 246 | + |
| 247 | +- [GitHub Copilot in Visual Studio](/visualstudio/ide/visual-studio-github-copilot-install-and-states) |
| 248 | +- [GitHub Copilot documentation](https://docs.github.com/en/copilot) - Dive deeper into AI-assisted development |
| 249 | +- [Awesome ChatGPT Prompts](https://github.com/f/awesome-chatgpt-prompts) - Community-driven prompting examples for inspiration |
0 commit comments