C ++ iomanip对齐 [英] C++ iomanip Alignment

查看:205
本文介绍了C ++ iomanip对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调整输出,但是由于某种原因,我无法将其转换为我想要的
,这确实令人沮丧。标题不正确。
我不知道我是否正确使用了setw()。

I'm trying to align my output but for some reason I can't get it to how I want it, and it's really frustrating. The title won't align right. I don't know if I'm using setw() properly.

#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;
}

//我需要的样本输出

//Sample Output of what I need

不过,我得到的却是

However this is what I'm getting instead

推荐答案

如果这是您想要的

#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;
}

这篇关于C ++ iomanip对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆