How to Create a Gantt Chart in C++: A Step-by-Step Guide
A Gantt chart can turn a tangled project schedule into a timeline you can understand at a glance. Building one in C++ can feel harder than it should, especially when you’re unsure how to map task dates into visible bars.
A small mistake in start days or durations can shift every bar, while a chart that grows too wide becomes difficult to read. You might also wonder whether you need a graphics library before you can show a useful schedule.
You can begin with a simple console chart. In this guide, I’ll walk you through a working C++ program that validates task details, prints a timeline, and gives you a foundation for adding calendar dates or a graphical interface later.
How to create a Gantt chart in C++: step by step
The quickest approach is to store each task’s name, start day, and duration, then print a bar for every day the task is active. This example uses a console, C++17, and a timeline of up to 60 days.
1. Decide what each chart column represents
Start by choosing a time unit. For a short project, one column per day is easy to understand. For a longer project, you could use one column per week or month instead.
This example uses day numbers beginning at 1. A task that starts on day 3 and lasts 4 days occupies days 3, 4, 5, and 6.
2. Create a structure for task details
A task needs a label, a starting day, and a duration. A C++ struct keeps those values together so your chart can process each task consistently.
struct Task {
std::string name;
int startDay;
int duration;
};
You can store several tasks in a std::vector<Task>. The vector makes it straightforward to loop through tasks when drawing the chart.
3. Add and validate the task details
Before drawing the timeline, check that every task has a name, a positive start day, and a duration greater than zero. This example also limits the chart to 60 days and 20 tasks, keeping the console display readable.
For example, a task starting on day 58 with a duration of 4 would end on day 61. It falls outside the chart’s 60-day limit, so the program should reject it.
4. Print the timeline and task bars
The chart prints one cell per day. It uses ### for an active task and three spaces when the task is inactive.
Each task’s final active day is calculated as startDay + duration - 1. The subtraction matters: a task starting on day 3 and lasting 4 days ends on day 6, not day 7.
5. Build and run the complete program
Here is a complete console version. It accepts task names with spaces, checks the schedule values, and displays the chart after all tasks are entered.
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
#include <vector>
struct Task {
std::string name;
int startDay;
int duration;
};
int main() {
int taskCount;
std::cout << "Number of tasks (1-20): ";
if (!(std::cin >> taskCount) || taskCount < 1 || taskCount > 20) {
std::cerr << "Enter a number from 1 to 20.\n";
return 1;
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::vector<Task> tasks;
int lastDay = 0;
for (int i = 0; i < taskCount; ++i) {
Task task;
std::cout << "\nTask " << i + 1 << " name (up to 17 characters): ";
std::getline(std::cin, task.name);
if (task.name.empty() || task.name.size() > 17) {
std::cerr << "Use a name between 1 and 17 characters.\n";
return 1;
}
std::cout << "Start day (1-60): ";
if (!(std::cin >> task.startDay)) {
std::cerr << "Enter a whole number for the start day.\n";
return 1;
}
std::cout << "Duration in days: ";
if (!(std::cin >> task.duration)) {
std::cerr << "Enter a whole number for the duration.\n";
return 1;
}
int endDay = task.startDay + task.duration - 1;
if (task.startDay < 1 || task.duration < 1 || endDay > 60) {
std::cerr << "The task must fit within days 1 to 60.\n";
return 1;
}
lastDay = std::max(lastDay, endDay);
tasks.push_back(task);
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
std::cout << "\n";
std::cout << std::left << std::setw(18) << "Task" << std::right;
for (int day = 1; day <= lastDay; ++day) {
std::cout << std::setw(3) << day;
}
std::cout << "\n";
for (const Task& task : tasks) {
std::cout << std::left << std::setw(18) << task.name << std::right;
for (int day = 1; day <= lastDay; ++day) {
bool active = day >= task.startDay
&& day < task.startDay + task.duration;
std::cout << std::setw(3) << (active ? "###" : " ");
}
std::cout << "\n";
}
return 0;
}
Save the program as gantt.cpp, then compile it with a C++17 compiler:
g++ -std=c++17 gantt.cpp -o gantt
./gantt
The program asks for a task count, then requests a name, start day, and duration for each task. If you enter three tasks, the chart might look like this:
Task 1 2 3 4 5 6 7 8
Planning ### ###
Design ### ### ###
Testing ### ### ###
In the example, Planning runs on days 1 and 2, Design runs on days 3 through 5, and Testing runs on days 5 through 7. The shared day 5 shows that two tasks overlap.
How the chart’s timeline and bars work
A printed Gantt chart is a grid: each row represents a task, while each column represents a time period. A bar appears wherever a task is active.
For instance, a task that begins on day 2 and lasts 3 days should appear in columns 2, 3, and 4. Keeping the day numbers and bar calculations consistent prevents off-by-one errors.
Use a clear calculation for the end day
The final active day is startDay + duration - 1. With a start day of 2 and a duration of 3, the final day is 4.
The loop condition in the program uses day < startDay + duration. This produces the same result while checking each timeline column.
Choose a scale that fits the project
One column per day works well for a project lasting a few weeks. If a project runs for a year, 365 columns may stretch beyond a typical terminal window.
You can address that by representing each column as a week, showing a selected date range, or printing fewer columns at a time. Keep the scale visible so readers know what each column means.
Turn day numbers into real calendar dates
The sample program uses day numbers, which keeps its scheduling logic compact. A real project often needs calendar dates, such as “April 8” or “2026-04-08.”
One practical design is to convert every task’s start date into an offset from the project’s first day. If the project begins on April 1, then April 4 becomes offset 3 when counting from zero.
Represent dates with the C++ standard library
C++20 provides calendar types in <chrono>, including std::chrono::year_month_day. You can use them to represent dates and calculate the number of days between a task and the project start.
If you use a compiler without the needed C++20 calendar support, a date-handling library can provide the conversion. Keep date conversion separate from chart printing, so a change in display does not affect schedule calculations.
Decide how weekends and holidays should appear
A basic daily chart counts every calendar day. That means a five-day task starting on Friday extends through Tuesday, including Saturday and Sunday.
If your schedule uses working days, add a calendar rule that skips weekends and chosen holidays. For example, a five-workday task beginning on Monday would finish on Friday, even though five calendar days have not passed at the same rate.
Add useful scheduling features without making the chart unreadable
A first chart only needs task bars, but a project plan may need milestones, dependencies, and progress. Add these features one at a time so each change remains easy to test.
Mark milestones as single-day events
A milestone can use a duration of one day and a different marker, such as *. A “Design approved” milestone on day 8 could then stand out from longer task bars.
If milestones have separate properties, add a field such as bool isMilestone to the task structure. The drawing loop can select the marker based on that value.
Show progress separately from planned duration
Add a progress percentage between 0 and 100 to each task. Then print completed cells with one character and remaining cells with another.
For a 10-day task at 40% complete, four cells could show completed work while six cells show the remaining schedule. Keep progress values within their allowed range before rendering.
Represent dependencies explicitly
A dependency means one task relies on another. For example, “Testing” may need to start after “Implementation” finishes.
You can store a task identifier for each prerequisite and check the schedule for conflicts. A printed bar alone does not enforce dependencies, so validation should happen before the chart is displayed.
Test and improve the C++ Gantt chart
Test the smallest and largest schedules before expanding the program. A one-task chart checks basic rendering, while a task that ends on day 60 checks the timeline boundary.
- Try a task starting on day 1 with a duration of 1.
- Try two tasks that overlap on the same day.
- Enter a zero duration and confirm the program rejects it.
- Try a task that would end on day 61 and confirm it is rejected.
- Check a task name with spaces, such as “Write test plan.”
If the chart becomes too wide, review the scale before changing the task logic. If the bars look shifted, check the relationship between the start day, duration, and final active day.
For a graphical display, keep the same task model and scheduling calculations. Replace the console-rendering loop with a drawing layer from a graphics library, and let that layer handle colors, labels, and scrolling.
A project planning companion: ONES.com
Writing a C++ renderer is a useful way to learn how task durations become timeline bars. For coordinating a real project, ONES.com offers project management and knowledge management in a unified platform powered by ONES Assistant.

ONES Project is a Jira alternative for managing project work, while ONES Wiki is a Confluence alternative for team knowledge. They are sold separately. ONES.com does not replace the chart-rendering code in this tutorial; it can support the wider planning and collaboration around a project.
Core capabilities
- Scattered work planning → ONES Project workflows → clearer task coordination. Teams can structure work in Jira-compatible workflows and adapt processes with custom workflows and fields.
- Limited visibility into progress → built-in reporting → a more direct view of project status. Reports help teams review progress without relying only on individual updates.
- Unclear sprint commitments → sprint management → more organized iteration planning. Teams can group work into sprints and track what they intend to complete.
- Repeated manual steps → automation → fewer routine actions for the team. Automation can handle recurring workflow tasks, reducing the need to manage every transition by hand.
- Separate project and knowledge needs → ONES Project and ONES Wiki → a coordinated platform choice. Teams can choose the project or knowledge product that fits their needs; the products are sold separately.
- Plugin-heavy workflows → native capabilities and feature parity across deployments → fewer add-ons to manage. Teams can use built-in project functions while choosing a deployment that fits their environment.
- Restricted hosting requirements → cloud and self-hosted deployments → a choice of operating environment. Deployment options include Cloud, On-Premise, Private Cloud, and Air-gapped, with full feature parity between cloud and self-hosted versions.
Application scenarios
A C++ class project: You can build the tutorial’s console chart to understand scheduling logic, then coordinate task ownership and sprint work in ONES Project. The chart remains your C++ program; the platform supports the surrounding project workflow.
A distributed engineering team: A team can manage project work in ONES Project and keep related team knowledge in ONES Wiki if it needs both capabilities. The team can select the deployment option that suits its working environment.
ONES.com offers a free plan for up to 30 seats. Its project management and knowledge management products can support coordination alongside a custom C++ chart, without requiring you to treat the chart as a replacement for team planning.
Common challenges when building a chart in C++
Bars appear one day too long or too short
Problem: The end-day calculation may count the starting day twice or skip the last active day. Solution: Use startDay + duration - 1 for the final day, then test a one-day task.
The chart exceeds the terminal width
Problem: A daily timeline for a long project can require hundreds of columns. Solution: Show weeks or months, restrict the displayed date range, or add horizontal scrolling in a graphical interface.
Task labels disrupt the columns
Problem: A long label can push every timeline column to the right. Solution: Set a maximum label length, shorten displayed names, or calculate a consistent label width before printing.
The chart shows a schedule that cannot be followed
Problem: Bars can overlap even when a task depends on another task finishing first. Solution: Store prerequisites and validate them before rendering, so the chart reflects a feasible sequence.
FAQs
Can I make a Gantt chart in C++ without a graphics library?
Yes. A console chart can display tasks as rows and days as columns, using characters such as ### for active periods. This approach is useful for learning how task dates map to bars and does not require a graphical toolkit. You can add a graphical interface later while keeping the same task structure and scheduling rules.
How do I display calendar dates instead of day numbers?
Choose a project start date, then calculate an offset for every task’s start date. Use that offset to position its bar on the timeline, and print formatted dates in the header. C++20 includes calendar types in <chrono>, though compiler support can vary. Keep date conversion separate from drawing so the chart can use different labels later.
How can I show tasks that overlap?
Give each task its own row and calculate whether it is active for each timeline column. Two tasks that occupy the same day will then show bars in the same column on separate rows. The example program handles this naturally. If overlapping work is not allowed in your project, add a scheduling check to detect it before printing.
What should I add after the basic version works?
Start with a feature that solves a clear scheduling need. Useful additions include calendar dates, milestones, progress markers, working-day rules, or dependency checks. For example, a team managing a release may need a milestone for launch and a dependency that prevents testing from starting before implementation ends. Build and test one addition at a time.
Conclusion
You can create a useful C++ Gantt chart with a task structure, a timeline loop, and clear rules for start days and durations. The console example gives you a working base, while date handling, milestones, progress, and dependencies can extend it as your scheduling needs grow.
When timeline logic feels confusing, test a one-day task and check the end-day calculation. When the chart becomes crowded, change its scale before adding complexity. That gives you a readable schedule and a practical path from a small console exercise to a more capable planning tool.