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
- Install VS Code (Download).
- Install Git (Download).
- Create a GitHub account and install GitHub Copilot (Copilot Setup).
- Install the C++ Extension for VS Code.
- Download the text file named
words.txtwith a list of English words is available in your working directory.
Step 1: Set Up Your Environment
- Create a new GitHub project with a Readme.md file:
WordFind - Clone a GitHub Repository:
- Open a terminal and clone the new GitHub repository:
git clone https://github.com/your-username/WordFind.git - Replace
your-usernamewith your GitHub username.
- Open a terminal and clone the new GitHub repository:
- Open VS Code:
- Navigate to the repository folder and open it in VS Code:
cd demo-cpp-project code .
- Navigate to the repository folder and open it in VS Code:
- 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
- Create a New File:
- In VS Code, create a new file named
main.cpp.
- In VS Code, create a new file named
- 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.
- Start typing the program’s objective. GitHub Copilot will suggest comments and headers:
Step 3: Write the Code Using GitHub Copilot
- Set Up the Program Structure:
- Type
#includeand let Copilot suggest necessary headers:#include <iostream>#include <fstream>#include <string>#include <vector>#include <set>
Accept Copilot’s suggestions.
- Type
- 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.
- Start typing
Step 4: Test the Program
- Compile and Run:
- Use
g++to compile:g++ -o main main.cpp ./main - Input characters like
aeiouand observe the matching words.
- Use
Step 5: Commit and Push to GitHub
- Commit Changes:
- In the terminal:
git add main.cpp words.txt git commit -m "Add C++ program to filter words by characters"
- In the terminal:
- Push to GitHub:
- Push the changes:
git push origin main
- Push the changes:
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.
