Summary:
A command-line productivity tool that allows users to create, track, and manage tasks with priority levels. This project emphasizes the separation of concerns between data storage and user interface logic.
Learning Objectives:
Object-Oriented Design: Implementing a class with private attributes and public interfaces.
STL Containers: Utilizing std::vector for dynamic array management.
Input Validation: Handling stream errors (e.g., non-numeric input) to prevent infinite loops.
Formatting: Using <iomanip> to create clean, tabular console outputs.
The Source Code (main.cpp)
#include <iostream> #include <vector> #include <string> #include <limits> #include <iomanip>
using namespace std;
// --- Task Entity Class --- class Task { private: string title; int priority;
public: Task(string t, int p) : title(t), priority(p) {}
string getTitle() const { return title; } int getPriority() const { return priority; } };
// --- Manager Logic Class --- class ToDoManager { private: vector<Task> tasks;
void clearInput() { cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); }
public: void addTask() { string title; int priority;
cout << "\nEnter Task Description: "; getline(cin >> ws, title);
while (true) { cout << "Enter Priority (1-5): "; if (cin >> priority && priority >= 1 && priority <= 5) break; cout << "Invalid input. Please enter a number between 1 and 5.\n"; clearInput(); } tasks.emplace_back(title, priority); cout << "Task added successfully!\n"; }
void viewTasks() const { if (tasks.empty()) { cout << "\n[!] Your list is currently empty.\n"; return; }
cout << "\n" << setfill('=') << setw(40) << "" << endl; cout << setfill(' ') << left << setw(5) << "ID" << setw(25) << "Task" << "Priority" << endl; cout << setfill('-') << setw(40) << "" << setfill(' ') << endl;
for (size_t i = 0; i < tasks.size(); ++i) { cout << left << setw(5) << i + 1 << setw(25) << tasks[i].getTitle() << tasks[i].getPriority() << endl; } cout << setfill('=') << setw(40) << "" << setfill(' ') << endl; }
void removeTask() { viewTasks(); if (tasks.empty()) return;
int index; cout << "Enter Task ID to remove: "; if (!(cin >> index) || index < 1 || index > (int)tasks.size()) { cout << "Invalid ID. Operation cancelled.\n"; clearInput(); } else { tasks.erase(tasks.begin() + (index - 1)); cout << "Task removed.\n"; } }
void runMenu() { int choice; do { cout << "\n--- TO-DO ARCHITECT V1.0 ---" << endl; cout << "1. Add Task\n2. View Tasks\n3. Remove Task\n4. Exit\nChoice: "; if (!(cin >> choice)) { cout << "Numeric input required.\n"; clearInput(); continue; }
switch (choice) { case 1: addTask(); break; case 2: viewTasks(); break; case 3: removeTask(); break; case 4: cout << "Exiting program...\n"; break; default: cout << "Invalid choice.\n"; } } while (choice != 4); } };
int main() { ToDoManager myApp; myApp.runMenu(); return 0; } |
Execution Trace
--- TO-DO ARCHITECT V1.0 --- 1. Add Task 2. View Tasks 3. Remove Task 4. Exit Choice: 1
Enter Task Description: Finish C++ Lab 4 Enter Priority (1-5): 5 Task added successfully!
--- TO-DO ARCHITECT V1.0 --- 1. Add Task 2. View Tasks 3. Remove Task 4. Exit Choice: 2
======================================== ID Task Priority ---------------------------------------- 1 Finish C++ Lab 4 5 ========================================
--- ERROR HANDLING CASE: Non-numeric Input --- Choice: abc Numeric input required.
--- ERROR HANDLING CASE: Out of Bounds --- Choice: 3 Enter Task ID to remove: 99 Invalid ID. Operation cancelled. |
Academic Learning Corner
The cin.clear() and cin.ignore() Combo: In MinGW, when a user types a character where an int is expected, the stream enters a "fail state." Without clearing this state and flushing the buffer, your program would enter an infinite loop. We use numeric_limits to ensure we wipe the entire buffer.
The ws Manipulator: Notice getline(cin >> ws, title). The ws (whitespace) extractor is vital here; it consumes any leftover newline characters from previous cin >> calls so your program doesn't "skip" the input prompt.
- Vector Erasing: tasks.erase(tasks.begin() + (index - 1)) is the standard way to remove elements by index. Remember that vectors are 0-indexed, while users see 1-based IDs.
eBook ‘14 Beginner Level C++ Projects’ purchase Link: Google Play Store || Google Books
eBook ‘14 Beginner Level C++ Projects’ Promotional Link:
Level Up Your Coding Skills: Master C++ with 14 Hands-On Academic Projects!
For all 2026 published articles list: click here
...till the next post, bye-bye & take care
No comments:
Post a Comment