Demo: Using VS Code with GitHub and GitHub Copilot to Write a C++ Program

This tutorial will guide you through setting up VS Code, GitHub, and GitHub Copilot to write a C++ program that reads a list of unique characters and identifies words in a text file (words.txt) that use only those characters. Along the way, we will leverage GitHub Copilot to write much of the code.


Prerequisites

  1. Install VS Code (Download).
  2. Install Git (Download).
  3. Create a GitHub account and install GitHub Copilot (Copilot Setup).
  4. Install the C++ Extension for VS Code.
  5. Download the text file named words.txt with a list of English words is available in your working directory.

Step 1: Set Up Your Environment

  1. Create a new GitHub project with a Readme.md file:
    WordFind
  2. Clone a GitHub Repository:
    • Open a terminal and clone the new GitHub repository:
      git clone https://github.com/your-username/WordFind.git
    • Replace your-username with your GitHub username.
  3. Open VS Code:
    • Navigate to the repository folder and open it in VS Code:
      cd demo-cpp-project code .
  4. Enable GitHub Copilot:
    • Install and enable the GitHub Copilot extension in VS Code.
    • Sign in to GitHub if prompted.

Step 2: Create the C++ File

  1. Create a New File:
    • In VS Code, create a new file named main.cpp.
  2. Write the Program Header:
    • Start typing the program’s objective. GitHub Copilot will suggest comments and headers:
      // This program reads a list of unique characters from the user. // It then finds all words in a file (words.txt) that use only those characters.

Step 3: Write the Code Using GitHub Copilot

  1. Set Up the Program Structure:
    • Type #include and let Copilot suggest necessary headers:
      #include <iostream>
      #include <fstream>
      #include <string>
      #include <vector>
      #include <set>
      Accept Copilot’s suggestions.
  2. Write the Main Function:
    • Start typing int main() and allow Copilot to suggest boilerplate code:
    • int main() { std::set<char> ....
    • Use Copilot’s inline suggestions for logic like:
      • Reading the input characters.
      • Reading words from the file.
      • Checking if the word is valid based on the characters.
      • Printing the matching words.

Step 4: Test the Program

  1. Compile and Run:
    • Use g++ to compile:
      g++ -o main main.cpp ./main
    • Input characters like aeiou and observe the matching words.

Step 5: Commit and Push to GitHub

  1. Commit Changes:
    • In the terminal:
      git add main.cpp words.txt git commit -m "Add C++ program to filter words by characters"
  2. Push to GitHub:
    • Push the changes:
      git push origin main

Key Points

  • Leveraging GitHub Copilot:
    • Copilot can auto-complete loops, conditionals, and even complex logic.
    • Use comments to guide Copilot, like:
      // Read the file and check each word against allowedChars
    • Let Copilot suggest code snippets based on context.
  • Iterative Development:
    • Use Copilot’s suggestions as a starting point and refine the logic as needed.

By following this tutorial, you can experience how GitHub Copilot assists in writing efficient, boilerplate-free code while focusing on your program’s logic.

Scroll to Top