how to write a code in c language to make smart planner
Question
how to write a code in c language to make smart planner
Solution
Sure, let's break down the steps to write a code for a smart planner:
-
Define the Requirements:
- The planner should allow users to add, view, edit, and delete tasks.
- Each task should have a title, description, due date, and priority.
- The planner should store tasks in a way that they can be retrieved and modified.
-
Set Up the Environment:
- Ensure you have a C compiler installed (like GCC).
- Create a new file for your code, e.g.,
smart_planner.c.
-
Include Necessary Headers:
- Include standard libraries for input/output and string manipulation.
-
Define Data Structures:
- Create a structure to represent a task.
-
Implement Functions:
- Write functions to add, view, edit, and delete tasks.
- Write a function to display the menu and handle user input.
-
Main Function:
- Implement the main function to initialize the planner and handle the main loop.
Here is a basic example to get you started:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TASKS 100
#define TITLE_LEN 50
#define DESC_LEN 100
typedef struct {
char title[TITLE_LEN];
char description[DESC_LEN];
char dueDate[11]; // Format: YYYY-MM-DD
int priority;
} Task;
Task tasks[MAX_TASKS];
int taskCount = 0;
void addTask() {
if (taskCount >= MAX_TASKS) {
printf("Task list is full!\n");
return;
}
Task newTask;
printf("Enter title: ");
fgets(newTask.title, TITLE_LEN, stdin);
newTask.title[strcspn(newTask.title, "\n")] = 0; // Remove newline character
printf("Enter description: ");
fgets(newTask.description, DESC_LEN, stdin);
newTask.description[strcspn(newTask.description, "\n")] = 0;
printf("Enter due date (YYYY-MM-DD): ");
fgets(newTask.dueDate, 11, stdin);
newTask.dueDate[strcspn(newTask.dueDate, "\n")] = 0;
printf("Enter priority (1-5): ");
scanf("%d", &newTask.priority);
getchar(); // Consume newline character left by scanf
tasks[taskCount++] = newTask;
printf("Task added successfully!\n");
}
void viewTasks() {
if (taskCount == 0) {
printf("No tasks to display.\n");
return;
}
for (int i = 0; i < taskCount; i++) {
printf("Task %d:\n", i + 1);
printf("Title: %s\n", tasks[i].title);
printf("Description: %s\n", tasks[i].description);
printf("Due Date: %s\n", tasks[i].dueDate);
printf("Priority: %d\n", tasks[i].priority);
printf("\n");
}
}
void editTask() {
int taskNumber;
printf("Enter task number to edit: ");
scanf("%d", &taskNumber);
getchar(); // Consume newline character left by scanf
if (taskNumber < 1 || taskNumber > taskCount) {
printf("Invalid task number.\n");
return;
}
Task *task = &tasks[taskNumber - 1];
printf("Editing Task %d:\n", taskNumber);
printf("Enter new title (leave blank to keep current): ");
char newTitle[TITLE_LEN];
fgets(newTitle, TITLE_LEN, stdin);
if (newTitle[0] != '\n') {
newTitle[strcspn(newTitle, "\n")] = 0;
strcpy(task->title, newTitle);
}
printf("Enter new description (leave blank to keep current): ");
char newDescription[DESC_LEN];
fgets(newDescription, DESC_LEN, stdin);
if (newDescription[0] != '\n') {
newDescription[strcspn(newDescription, "\n")] = 0;
strcpy(task->description, newDescription);
}
printf("Enter new due date (leave blank to keep current): ");
char newDueDate[11];
fgets(newDueDate, 11, stdin);
if (newDueDate[0] != '\n') {
newDueDate[strcspn(newDueDate, "\n")] = 0;
strcpy(task->dueDate, newDueDate);
}
printf("Enter new priority (leave blank to keep current): ");
char newPriority[3];
fgets(newPriority, 3, stdin);
if (newPriority[0] != '\n') {
task->priority = atoi(newPriority);
}
printf("Task updated successfully!\n");
}
void deleteTask() {
int taskNumber;
printf("Enter task number to delete: ");
scanf("%d", &taskNumber);
getchar(); // Consume newline character left by scanf
if (taskNumber < 1 || taskNumber > taskCount) {
printf("Invalid task number.\n");
return;
}
for (int i = taskNumber - 1; i < taskCount - 1; i++) {
tasks[i] = tasks[i + 1];
}
taskCount--;
printf("Task deleted successfully!\n");
}
void displayMenu() {
printf("Smart Planner Menu:\n");
printf("1. Add Task\n");
printf("2. View Tasks\n");
printf("3. Edit Task\n");
printf("4. Delete Task\n");
printf("5. Exit\n");
printf("Enter your choice: ");
}
int main() {
int choice;
while (1) {
displayMenu();
scanf("%d", &choice);
getchar(); // Consume newline character left by scanf
switch (choice) {
case 1:
addTask();
break;
case 2:
viewTasks();
break;
case 3:
editTask();
break;
case 4:
deleteTask();
break;
case 5:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
This code provides a basic structure for a smart planner. You can expand it by adding more features, such as saving tasks to a file, sorting tasks by priority or due date, and improving the user interface.
Similar Questions
write a code to make a smart planner
smart planner using array,function,switch case,loop in c programming language
make a ppt on smart planner using array,loops,function,switch case
Explain the structure of C program. Explain type conversion in C. Write a C program to find prime numbers between a range(using function). Explain any 5 string handling functions available in C. Write a C program to print the address of a variable along with its value.
Context: make a ppt on smart planner using array,loops,function,switch case with an example and the whole ppt should be in english
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.