Sure! Below is a simple console-based To-Do List application written in Pascal. This program allows users to add, view, and delete tasks from a to-do list. We'll use an array to store the tasks with a predefined maximum capacity. Here's a step-by-step guide along with the complete source code.
Program Overview
-
Data Structure:
- Tasks Array: We'll use a dynamic array of strings to store the tasks. This allows the list to grow as needed.
- Task Count: A variable to keep track of the current number of tasks.
-
Functionalities:
- Add Task: Prompt the user to enter a new task and add it to the array.
- View Tasks: Display all current tasks with their corresponding indices.
- Delete Task: Allow the user to delete a task by entering its index.
- Exit: Terminate the application.
-
User Interface:
- A simple text-based menu to navigate between different options.
Source Code
program ToDoListApp;
uses
SysUtils;
const
MAX_TASKS = 100;
type
TStringArray = array[1..MAX_TASKS] of string;
var
Tasks: TStringArray;
TaskCount: integer;
// Procedure to add a task
procedure AddTask;
var
newTask: string;
begin
if TaskCount >= MAX_TASKS then
begin
writeln('Task list is full. Cannot add more tasks.');
exit;
end;
write('Enter the task: ');
readln(newTask);
Inc(TaskCount);
Tasks[TaskCount] := newTask;
writeln('Task added successfully.');
end;
// Procedure to view all tasks
procedure ViewTasks;
var
i: integer;
begin
if TaskCount = 0 then
begin
writeln('No tasks to display.');
exit;
end;
writeln('Your To-Do List:');
writeln('-----------------------');
for i := 1 to TaskCount do
writeln(i, '. ', Tasks[i]);
writeln('-----------------------');
end;
// Procedure to delete a task
procedure DeleteTask;
var
index, i: integer;
begin
if TaskCount = 0 then
begin
writeln('No tasks to delete.');
exit;
end;
ViewTasks;
write('Enter the index of the task to delete: ');
readln(index);
if (index < 1) or (index > TaskCount) then
begin
writeln('Invalid task index.');
exit;
end;
// Shift tasks to fill the deleted spot
for i := index to TaskCount - 1 do
Tasks[i] := Tasks[i + 1];
Dec(TaskCount);
writeln('Task deleted successfully.');
end;
// Procedure to display the menu
procedure DisplayMenu;
begin
writeln;
writeln('===== To-Do List Application =====');
writeln('1. Add Task');
writeln('2. View Tasks');
writeln('3. Delete Task');
writeln('4. Exit');
write('Choose an option (1-4): ');
end;
// Main program block
var
choice: integer;
begin
TaskCount := 0;
repeat
DisplayMenu;
readln(choice);
writeln;
case choice of
1: AddTask;
2: ViewTasks;
3: DeleteTask;
4: writeln('Exiting the application. Goodbye!');
else
writeln('Invalid option. Please choose between 1 and 4.');
end;
until choice = 4;
end.
Explanation of the Code
-
Variables and Constants:
MAX_TASKS: Sets the maximum number of tasks the application can handle (set to 100).
Tasks: An array of strings to store the task descriptions.
TaskCount: Keeps track of the number of current tasks.
-
Procedures:
- AddTask: Checks if the task list is full. If not, it prompts the user to enter a new task and adds it to the
Tasks array.
- ViewTasks: Displays all the tasks with their indices. If there are no tasks, it notifies the user.
- DeleteTask: Shows the current tasks, prompts the user to enter the index of the task to delete, validates the input, and removes the task by shifting the subsequent tasks up.
- DisplayMenu: Shows the main menu options to the user.
-
Main Program Loop:
- The program runs in a
repeat...until loop, continuously displaying the menu and executing user choices until the user decides to exit by selecting option 4.
Sample Output
===== To-Do List Application =====
1. Add Task
2. View Tasks
3. Delete Task
4. Exit
Choose an option (1-4): 1
Enter the task: Buy groceries
Task added successfully.
===== To-Do List Application =====
1. Add Task
2. View Tasks
3. Delete Task
4. Exit
Choose an option (1-4): 1
Enter the task: Call Alice
Task added successfully.
===== To-Do List Application =====
1. Add Task
2. View Tasks
3. Delete Task
4. Exit
Choose an option (1-4): 2
Your To-Do List:
-----------------------
1. Buy groceries
2. Call Alice
-----------------------
===== To-Do List Application =====
1. Add Task
2. View Tasks
3. Delete Task
4. Exit
Choose an option (1-4): 3
Your To-Do List:
-----------------------
1. Buy groceries
2. Call Alice
-----------------------
Enter the index of the task to delete: 1
Task deleted successfully.
===== To-Do List Application =====
1. Add Task
2. View Tasks
3. Delete Task
4. Exit
Choose an option (1-4): 2
Your To-Do List:
-----------------------
1. Call Alice
-----------------------
===== To-Do List Application =====
1. Add Task
2. View Tasks
3. Delete Task
4. Exit
Choose an option (1-4): 4
Exiting the application. Goodbye!
Notes and Enhancements
-
Error Handling: The program includes basic error handling for scenarios like adding tasks beyond the maximum limit and deleting tasks with invalid indices.
-
Dynamic Array: For more flexibility, especially when dealing with a large or unknown number of tasks, you might consider using dynamic arrays or linked lists. However, this example uses a fixed-size array for simplicity.
-
Persistence: Currently, the tasks are stored in memory. To make the to-do list persistent across sessions, you can implement file I/O to save and load tasks from a file.
-
User Experience: You can enhance the user interface by adding more features such as marking tasks as completed, editing tasks, sorting tasks, etc.
Feel free to modify and expand this application based on your requirements!