0

好的,在这个程序中,我需要根据用户输入制作一个表格。问题是我无法弄清楚如何让表头与显示的信息正确对齐。如果用户输入 Michael 作为球员 1 和 Michael Jordan 作为球员 2 输入,表格标题不会对齐。任何关于允许标题与显示的输入正确对齐的建议,无论字符长度如何,将不胜感激,谢谢。

这是我的代码:

#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;


//struct of Basketball Player info
struct BasketballPlayerInfo
{
    string name; //player name

    int playerNum, //player number
        pointsScored; //points scored

};

int main()
{
    int index, //loop count
        total = 0; //hold total points
    const int numPlayers = 5; //nuymber of players
    BasketballPlayerInfo players[numPlayers]; //Array of players

    //ask user for Basketball Player Info
    cout << "Enter the name, number, and points scored for each of the 5 players.\n";

    for (index = 0; index < numPlayers; index++)
    {
        //collect player name
        cout << " " << endl;
        cout << "Enter the name of player # " << (index + 1);
        cout << ": ";

        //input validation
        if(!(getline(cin, players[index].name)))
        {
            cout << "Player Name must be alphabetical characters only!\n";
            cout << "Program terminating please start over." << endl;
            system("pause");
            exit(0);
        }

        //getline(cin, players[index].name);

        //collect players number
        cout << "Enter the number of player # " << (index + 1);
        cout << ": ";

        //input validation
        if(!(cin >> players[index].playerNum))
        {
            cout << "Player Name must be numeric characters only!\n";
            cout << "Program terminating please start over." << endl;
            system("pause");
            exit(0);
        }
        //collect points scored
        cout << "Enter points scored for player # " << (index + 1);
        cout << ": ";

        //input validation
        if(!(cin >> players[index].pointsScored))
        {
            cout << "Player Name must be numeric characters only!\n";
            cout << "Program terminating please start over." << endl;
            system("pause");
            exit(0);
        }

        cin.ignore();
    }

    //display
    cout << "\n";
    cout << "Here is the information for each player: \n";
    cout << fixed << showpoint << setprecision(2);
    cout << "\n";
    cout << "          \tName\tNumber\tPoints\n";
    cout << "------------------------------------------------" << endl;

    for(index = 0; index < numPlayers; index++)
    {
        cout << "Player # " << (index + 1);
        cout << ": \t" << players[index].name << "\t" << players[index].playerNum << "\t" << players[index].pointsScored << endl;
        cout << "------------------------------------------------" << endl;

    }

    //display total points scored by all players
    for(index = 0; index < numPlayers; index++)
    {
        //hold total
        total += players[index].pointsScored;
    }

    cout << "Total Points scored are: " << total << endl;

 system("pause");
return 0;

}
4

2 回答 2

2

您可以使用setw.io 下的 io 操纵器#include <iomanip>

cout << setw(20) << "Column1"
     << setw(20) << "Column2"
     << setw(8) << "Column3";

或者你可以使用Boost 库

// 使用 Boost.Format

cout << format("%-20s %-20s %-8s\n")  % "Column1" % "Column2" % "Column3";
于 2012-04-09T01:01:45.583 回答
2

看一下setwsetfill函数。您可以使用它们为列分配最小宽度,这将使输出格式比制表符更清晰。

于 2012-04-09T01:01:49.667 回答