【问题标题】:C++ iomanip AlignmentC++ iomanip 对齐
【发布时间】:2023-03-04 11:22:01
【问题描述】:

我正在尝试调整我的输出,但由于某种原因我无法做到 我多么想要它,这真的很令人沮丧。标题不会正确对齐。 我不知道我是否正确使用了 setw()。

#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
#include <assert.h>
#include <iomanip>
using std::setw;
using std::right;
using std::left;

//Movie Struct to hold movie data
struct MOVIES
{
    string Title; //Name of movie
    int CriticRating; //Critic rating 1-100
    int AudRating; //Audiences' rating 1-100
};
MOVIES Movies[10] = { { "The Wizard of Oz", 99 , 70 },
                      { "The Third Man"   , 78, 45 },
                      { "Citizen Kane"    , 86, 85 },
                      { "Charlie Chaplin in Modern Times", 56, 95 },
                      { "All About Eve"   , 78, 94 },
                      { "The Cabinet of Dr. Caligari"    , 76, 90 },
                      { "The God Father"  , 99 , 98 },
                      { "E.T. The Extra-Terrestrial"     , 98 , 71 },
                      { "The Beatles: A Hard Day's Night", 87 , 90 },
                      { "Monty Python and the Holy Grail", 100, 100 }
                    };


void PrintMovies(MOVIES* movies, int numMovies)
{
    cout << "Movies" << endl
         << "Critic" << setw(10)
         << "Audience" << setw(10)
         << "Title" << endl;

    for (int i = 0; i < numMovies; i++)
    {
        cout << setw(6);
        cout << movies[i].CriticRating << right;
        cout << setw(6);
        cout << movies[i].AudRating << right;
        cout << setw(6);
        cout << movies[i].Title << left;
        cout << endl;
    };
}

int main()
{
    PrintMovies(Movies, 10);

    return 0;
}

//我需要的样本输出

但是,这就是我得到的

【问题讨论】:

  • 你必须在之前设置你想要对齐的东西。与setw 相同。您需要在标题前添加一些空格,以免与右对齐的数字卡住。

标签: c++ alignment iomanip setw


【解决方案1】:

如果这是你想要的

#include <iomanip>
using std::setw;
using std::right;
using std::left;

//Movie Struct to hold movie data
struct MOVIES
{
    string Title; //Name of movie
    int CriticRating; //Critic rating 1-100
    int AudRating; //Audiences' rating 1-100
};
MOVIES Movies[10] = { { "The Wizard of Oz", 99, 70 },
{ "The Third Man", 78, 45 },
{ "Citizen Kane", 86, 85 },
{ "Charlie Chaplin in Modern Times", 56, 95 },
{ "All About Eve", 78, 94 },
{ "The Cabinet of Dr. Caligari", 76, 90 },
{ "The God Father", 99, 98 },
{ "E.T. The Extra-Terrestrial", 98, 71 },
{ "The Beatles: A Hard Day's Night", 87, 90 },
{ "Monty Python and the Holy Grail", 100, 100 }
};


void PrintMovies(MOVIES* movies, int numMovies)
{
    cout << " Critic " << setw(6) << " Audience " << setw(0) << " Title " << endl;

    for (int i = 0; i < numMovies; i++)
    {
        cout << setw(7) << right << movies[i].CriticRating << setw(10) << right << movies[i].AudRating <<"  "<< setw(30) << left << movies[i].Title << endl;
    }
}

int main()
{
    PrintMovies(Movies, 10);

    return 0;
}

【讨论】:

    【解决方案2】:

    如 cmets 中所说,如果您想使用 std::setw() 设置宽度,则必须在输入“值”之前使用它。

    例如,

    std::cout << "no setw: " << 42 << std::endl;
    std::cout << "setw(10): " << std::setw(10) << 42 << std::endl;
    

    您还必须设置每个 operator&lt;&lt; 之间的宽度。

    如果你这样做:

    std::cout << std::setw(10) << 42 << "bar" << std::endl;
    std:: cout << std::setw(10) << 42 << std::setw(10) << "bar" << std::endl;
    

    你将分别得到输出:

    ________42bar
    ________42_______bar
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-10
      • 2019-08-27
      • 1970-01-01
      • 1970-01-01
      • 2011-07-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多