C++ iomanip Alignment

随声附和 提交于 2019-12-11 11:44:53

问题


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


回答1:


If this is what you want

#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:


As said in the comments if you want to use std::setw() to set the width, you will have to use it before entering your "value".

For example,

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

Also you will have to set the width between each operator<<.

If you do:

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

you will respectively get the outputs :

________42bar
________42_______bar


来源:https://stackoverflow.com/questions/35424332/c-iomanip-alignment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!