I am trying to print the values within a structure as a table. However, one of the names gets printed out into the wrong 'field'.
Here is what I have:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct SPlayer {
string name;
string team;
int goalsScored;
int yellowcards;
int redcards;
};
void PrintHeader() {
cout << "name\t\tteam\tgoals\tyellow cards\tred cards\n";
cout << "--------------------------------------------------------------------" << endl;
}
void PrintTable(SPlayer table) {
cout << table.name << "\t\t" << table.team << "\t" << table.goalsScored << "\t" << table.yellowcards << "\t\t" << table.redcards << endl;
}
int main()
{
SPlayer mitchell = { "Mitchell" , "Red" , 4 , 1 , 0 };
SPlayer smith = { "Smith" , "Blue" , 8 , 0 , 0 };
SPlayer white = { "White" , "Green" , 0 , 2 , 4 };
SPlayer doe = { "Doe" , "Yellow" , 2 , 1 , 0 };
vector<SPlayer> players;
players.push_back(mitchell);
players.push_back(smith);
players.push_back(white);
players.push_back(doe);
PrintHeader();
for (int i = 0; i < players.size(); i++) {
PrintTable(players.at(i));
}
}
When you run the code, it looks like this:
Does anyone know how to fix this?



PrintTableis a weird name for a function that prints a singleSPlayer. A single player is not a table.Mitchellis long enough that tabbing out does exactly what you're seeing. Try very short names as well. And longer names will also be bad. It's very verbose, but I've taken to putting my table column widths into an enum and then using those.