Reputation: 235
I'm trying to print this LIST but it's driving me insane.
So this is the output that I wanted to get :
Veranstaltungen:
1: Freitag 08:00 - 09:30 SU Informatik III Kevin Kaufmann H3
2: Freitag 10:00 - 11:30 Ueb Informatik III Kevin Kaufmann D 114
3: Freitag 12:15 - 13:45 Ueb Informatik III Kevin Kaufmann D 114
4: Montag 10:00 - 11:30 SU Mathe III Nathan Neuling B 301
5: Mittwoch 10:00 - 11:30 Ueb Mathe III Nathan Neuling D 209
6: Donnerstag 16:00 - 17:30 Englisch Sabine Sauber D 419
7: Dienstag 16:00 - 17:30 SU Digitale Systeme Willi Witzig H5
8: Dienstag 17:45 - 19:15 Ueb Digitale Systeme Willi Witzig D 114
This is the print function in my code :
I tried first of all this simple alternative here:
void CEvents::print()
{
cout << "Veranstaltungen:" << endl ;
for (int i = 0 ; i < counter;i++)
{
CWeekday Day = Events[i]->WeekDay;
cout<<i+1<<": "<<Events[i]->getDay(Day) << " ";
Events[i]->Block->print();
cout<<" "<<Events[i]->Name<<" "<<Events[i]->Teacher->Name<<" "<<Events[i]->Room->Name<< endl;
}
}
I got from this code this output :
Veranstaltungen:
1: Freitag 08:00 - 09:30 SU Informatik III Kevin Kaufmann H3
2: Freitag 10:00 - 11:30 Ueb Informatik III Kevin Kaufmann D 114
3: Freitag 12:15 - 13:45 Ueb Informatik III Kevin Kaufmann D 114
4: Montag 10:00 - 11:30 SU Mathe III Nathan Neuling B 301
5: Mittwoch 10:00 - 11:30 Ueb Mathe III Nathan Neuling D 209
6: Donnerstag 16:00 - 17:30 Englisch Sabine Sauber D 419
7: Dienstag 16:00 - 17:30 SU Digitale Systeme Willi Witzig H5
8: Dienstag 17:45 - 19:15 Ueb Digitale Systeme Willi Witzig D 114
Of course this one won't work correctly because they all don't have the same lengths but atleast the first part here is correct .
Then I tried to switch to printf() instead of cout , got a little closer to the result but still not the same :
extern "C" void CEvents::print()
{
cout << "Veranstaltungen:" << endl ;
string a= " ";
for (int i = 0 ; i < counter;i++)
{
CWeekday Day = Events[i]->WeekDay;
printf("%d: %10s %5s",i+1,Events[i]->getDay(Day).c_str(),a.c_str());
Events[i]->Block->print();
printf("%30s %20s %10s\n",Events[i]->Name.c_str(),Events[i]->Teacher->Name.c_str(),Events[i]->Room->Name.c_str());
}
}
For this code I got this output :
1: Freitag 08:00 - 09:30 SU Informatik III Kevin Kaufmann H3
2: Freitag 10:00 - 11:30 Ueb Informatik III Kevin Kaufmann D 114
3: Freitag 12:15 - 13:45 Ueb Informatik III Kevin Kaufmann D 114
4: Montag 10:00 - 11:30 SU Mathe III Nathan Neuling B 301
5: Mittwoch 10:00 - 11:30 Ueb Mathe III Nathan Neuling D 209
6: Donnerstag 16:00 - 17:30 Englisch Sabine Sauber D 419
7: Dienstag 16:00 - 17:30 SU Digitale Systeme Willi Witzig H5
8: Dienstag 17:45 - 19:15 Ueb Digitale Systeme Willi Witzig D 114
Now second part is correct(the time) and the other parts are not. How do I make this format correctly ?
Upvotes: 3
Views: 104
Reputation: 38181
As a side note here is fmt example (note fmt
is part of C++20 and currently is not supported by any compiler, so it has to be used as external library):
#include <iostream>
#include <string>
#include <vector>
#include <fmt/core.h>
int main()
{
std::vector<std::array<std::string, 5>> data =
{
{ "Freitag", "08:00 - 09:30", "SU Informatik III", "Kevin Kaufmann", "H3" },
{ "Freitag", "10:00 - 11:30", "Ueb Informatik III", "Kevin Kaufmann", "D 114" },
{ "Freitag", "12:15 - 13:45", "Ueb Informatik III", "Kevin Kaufmann", "D 114" },
{ "Montag", "10:00 - 11:30", "SU Mathe III", "Nathan Neuling", "B 301" },
{ "Mittwoch", "10:00 - 11:30", "Ueb Mathe III", "Nathan Neuling", "D 209" },
{ "Donnerstag", "16:00 - 17:30", "Englisch", "Sabine Sauber", "D 419" },
{ "Dienstag", "16:00 - 17:30", "SU Digitale Systeme", "Willi Witzig", "H5" },
{ "Dienstag", "17:45 - 19:15", "Ueb Digitale Systeme", "Willi Witzig", "D 114" },
};
std::cout << "Veranstaltungen:\n";
size_t i = 0;
for (const auto&x : data)
{
fmt::print("{:2}: {:12} {:16} {:22} {:16} {}\n",
++i, x[0], x[1], x[2], x[3], x[4]);
}
}
Upvotes: 1
Reputation: 36578
Using std::left
and std::setw
should get you the formatting you want.
I've had to improvise a little as you didn't provide a full example but you should be able to adapt this code into yours:
#include <vector>
#include <string>
#include <iostream>
#include <array>
#include <iomanip>
int main()
{
std::vector<std::array<std::string, 5>> data =
{
{ "Freitag", "08:00 - 09:30", "SU Informatik III", "Kevin Kaufmann", "H3" },
{ "Freitag", "10:00 - 11:30", "Ueb Informatik III", "Kevin Kaufmann", "D 114" },
{ "Freitag", "12:15 - 13:45", "Ueb Informatik III", "Kevin Kaufmann", "D 114" },
{ "Montag", "10:00 - 11:30", "SU Mathe III", "Nathan Neuling", "B 301" },
{ "Mittwoch", "10:00 - 11:30", "Ueb Mathe III", "Nathan Neuling", "D 209" },
{ "Donnerstag", "16:00 - 17:30", "Englisch", "Sabine Sauber", "D 419" },
{ "Dienstag", "16:00 - 17:30", "SU Digitale Systeme", "Willi Witzig", "H5" },
{ "Dienstag", "17:45 - 19:15", "Ueb Digitale Systeme", "Willi Witzig", "D 114" },
};
std::cout << "Veranstaltungen:\n";
for (size_t i = 0; i < data.size(); i++)
{
std::cout << (i+1) << ": " <<
std::left <<
std::setw(12) << data[i][0] <<
std::setw(16) << data[i][1] <<
std::setw(22) << data[i][2] <<
std::setw(16) << data[i][3] <<
data[i][4] << "\n";
}
}
Upvotes: 3
Reputation: 71009
To fix your problems you need to left justify the text for some fields:
printf("%-30s %-20s %-10s\n",Events[i]->Name.c_str(),Events[i]->Teacher->Name.c_str(),Events[i]->Room->Name.c_str();
Note the minus in the number before s. Additionally I think you will have to make some of the fields wider than this constants
Upvotes: 2