Reputation: 1
Can anyone help me on how to make a hierarchy chart type of leaderboard in C++ like in the example given?
I've tried using vector, but it seems like the results is not neat/clean. Can anyone help me how to add the lines like this:
Rank1
|
+----------------------+-----------------------+
Rank2 Rank3
| |
+----------+----------+ +--------+--------+
Rank4 Rank5 Rank6 Rank7
and so on until Rank31.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// Function to print a row of winners
void printRow(const vector<string>& row, int totalWidth) {
int elements = row.size();
int spaceBetween = (totalWidth - elements) / (elements + 1);
for (int i = 0; i < elements; ++i) {
for (int j = 0; j < spaceBetween; ++j)
cout << " ";
cout << row[i];
}
cout << endl;
}
// Function to print the winners' chart
void printWinnersChart() {
vector<vector<string>> winnersChart = {
{"Bob"},
{"Alice", "Charlie"},
{"David", "Eve", "Frank", "Grace"},
{"Henry", "Isabella", "Jack", "Kate", "Liam", "Mia", "Noah", "Olivia"},
{"Penelope", "Quinn", "Ryan", "Sophia", "Thomas", "Uma", "Victoria", "William", "Xavier", "Yara", "Zoe", "Adam", "Bella", "Caleb", "Diana", "Ethan"}
};
int totalWidth = 80;
for (const auto& row : winnersChart) {
printRow(row, totalWidth);
}
}
int main() {
cout << "Figure 2: Sample of the winners' chart" << endl;
printWinnersChart();
//wait for the user to press enter to exit terminal
cin.get();
return 0;
}
Upvotes: 0
Views: 96