cplusplus
cplusplus

 

 

Introduction

Flashcards are a powerful tool for learning and retaining information. This app allows students to create digital flashcards where they can store a question and its corresponding answer. With the help of this flashcard app, users can efficiently review topics and test their knowledge, improving memory and understanding.

Objective

The goal of this flashcard app is to allow users to create a set of flashcards, view them, and test their knowledge by reviewing them. Users can add, view, and delete flashcards, helping to reinforce their memory on specific topics. This program will be implemented using the C++ programming language.

C++ Program Code

#include 
#include 
#include 
using namespace std;

// Flashcard class to store question and answer
class Flashcard {
public:
    string question;
    string answer;
    
    Flashcard(string q, string a) : question(q), answer(a) {}
};

// FlashcardApp class to manage the flashcards
class FlashcardApp {
private:
    vector flashcards;
    
public:
    void addFlashcard(string question, string answer) {
        flashcards.push_back(Flashcard(question, answer));
    }

    void viewFlashcards() {
        if(flashcards.empty()) {
            cout << "No flashcards available.\n";
            return;
        }
        for(int i = 0; i < flashcards.size(); i++) {
            cout << "Flashcard " << i+1 << ":\n";
            cout << "Question: " << flashcards[i].question << "\n";
            cout << "Answer: " << flashcards[i].answer << "\n\n";
        }
    }

    void testFlashcards() {
        if(flashcards.empty()) {
            cout << "No flashcards to test.\n";
            return;
        }
        int correct = 0;
        string userAnswer;
        for(int i = 0; i < flashcards.size(); i++) {
            cout << "Flashcard " << i+1 << " - Question: " << flashcards[i].question << "\n";
            cout << "Your Answer: ";
            getline(cin, userAnswer);
            if(userAnswer == flashcards[i].answer) {
                correct++;
                cout << "Correct!\n\n";
            } else {
                cout << "Wrong! The correct answer is: " << flashcards[i].answer << "\n\n";
            }
        }
        cout << "You got " << correct << " out of " << flashcards.size() << " correct!\n";
    }

    void deleteFlashcard(int index) {
        if(index < 0 || index >= flashcards.size()) {
            cout << "Invalid index.\n";
            return;
        }
        flashcards.erase(flashcards.begin() + index);
        cout << "Flashcard " << index+1 << " has been deleted.\n";
    }
};

// Function to display the menu and handle user input
void displayMenu(FlashcardApp &app) {
    int choice;
    do {
        cout << "Menu:\n";
        cout << "1. Add Flashcard\n";
        cout << "2. View Flashcards\n";
        cout << "3. Test Flashcards\n";
        cout << "4. Delete Flashcard\n";
        cout << "5. Exit\n";
        cout << "Enter your choice: "; cin >> choice;
        cin.ignore();  // Ignore the newline character left in the buffer

        switch(choice) {
            case 1: {
                string question, answer;
                cout << "Enter the question: ";
                getline(cin, question);
                cout << "Enter the answer: ";
                getline(cin, answer);
                app.addFlashcard(question, answer);
                break;
            }
            case 2:
                app.viewFlashcards();
                break;
            case 3:
                app.testFlashcards();
                break;
            case 4: {
                int index;
                cout << "Enter the flashcard number to delete: "; cin >> index;
                cin.ignore();  // Ignore the newline character
                app.deleteFlashcard(index - 1);
                break;
            }
            case 5:
                cout << "Exiting the app. Goodbye!\n";
                break;
            default:
                cout << "Invalid choice. Please try again.\n";
        }
    } while(choice != 5);
}

int main() {
    FlashcardApp app;
    displayMenu(app);
    return 0;
}

Explanation of the Program Structure

The program is structured into multiple classes:

  • Flashcard class: This class is used to store the question and answer for a single flashcard. It has a constructor that initializes the question and answer.
  • FlashcardApp class: This class manages a collection of flashcards using a vector. It provides methods to add, view, test, and delete flashcards.
  • Main function: The main function initializes an instance of the FlashcardApp class and displays a menu to interact with the user. It provides options to manage flashcards.

The user can add, view, test their knowledge, and delete flashcards using the menu. The flashcard test involves answering questions and comparing user input to the stored answers.

How to Run the Program

To run this program, follow these steps:

  1. Ensure you have a C++ compiler installed (e.g., GCC or MSVC).
  2. Save the provided code in a file with a .cpp extension (e.g., flashcard_app.cpp).
  3. Open your terminal or command prompt.
  4. Navigate to the directory where your file is saved.
  5. Compile the program with the following command:
    g++ flashcard_app.cpp -o flashcard_app
  6. Run the compiled program:
    ./flashcard_app

After running the program, follow the on-screen prompts to interact with the app.

© 2024 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)