Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,549 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,576 people online right now. Registration is fast and FREE... Join Now!




right justify

 
Reply to this topicStart new topic

right justify, right justifying part of a line of code

cwmartin
24 Nov, 2007 - 11:59 AM
Post #1

New D.I.C Head
*

Joined: 19 Sep, 2007
Posts: 10


My Contributions
I've tried cout.setf(ios_base::right) but am probably not doing it correctly. I have a line of input (song name and also duration in seconds). I want the name to print on the left of the screen and the durations to print on right.
here is the code for this part:
CODE
void Album::print()
{
    int seconds;
    for(int i = 0; i< numTracks; i++)
    {
        cout << i + 1 << ". " << tracks[i].title;
        //cout.setf(ios_base::right);
        seconds = tracks[i].duration % 60;
        if(seconds < 10 && seconds >= 0)
        {
            cout << cout.setf(ios_base::right);tracks[i].duration/60 << ":0" << seconds << endl;
        }
        else
        {
            cout << tracks[i].duration/60 << ":" << seconds << endl;
        }
    }
}

User is offlineProfile CardPM
+Quote Post

cwmartin
RE: Right Justify
24 Nov, 2007 - 01:02 PM
Post #2

New D.I.C Head
*

Joined: 19 Sep, 2007
Posts: 10


My Contributions
Any help would be greatly appreciated
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Right Justify
24 Nov, 2007 - 04:34 PM
Post #3

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,868



Thanked: 53 times
Dream Kudos: 550
My Contributions
to properly use the justification manipulators you need to set the field width.

CODE
#include <iomanip>
#include <iostream>

using namespace std;

int main() {
    cout << setw(10) << right << "left";
    cout << setw(40) << right << "Right" << endl;
    cout << setw(80) << right << "All the way Right" << endl;
    return 0;
}

User is offlineProfile CardPM
+Quote Post

cwmartin
RE: Right Justify
25 Nov, 2007 - 07:15 AM
Post #4

New D.I.C Head
*

Joined: 19 Sep, 2007
Posts: 10


My Contributions
CODE
void Album::print()
{
    int seconds;
    for(int i = 0; i< numTracks; i++)
    {
        cout << i + 1 << ". " << tracks[i].title;
        cout.setf(ios_base::right);
        seconds = tracks[i].duration % 60;
        if(seconds < 10 && seconds >= 0)
        {
            cout << setw(80) << right <<  tracks[i].duration/60 << ":0" << seconds << endl;
        }
        else
        {
            cout << setw(80) << right << tracks[i].duration/60 << ":" << seconds << endl;
        }
    }
}


// the numbers just seem to go 80 spaces or so after the last letter as opposed to a true right justify.
// Thanks for any guidance you can offer
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Right Justify
25 Nov, 2007 - 08:05 AM
Post #5

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,868



Thanked: 53 times
Dream Kudos: 550
My Contributions
So basically you are saying that you want to tab over a certain number of characters:
CODE
#include <iomanip>
#include <iostream>

using namespace std;

int main() {
    for (int i=0; i<10; i++) {
        cout << setw(40) << "";
        cout << left << "Some String" << endl;
    }
    return 0;
}

User is offlineProfile CardPM
+Quote Post

cwmartin
RE: Right Justify
25 Nov, 2007 - 08:19 AM
Post #6

New D.I.C Head
*

Joined: 19 Sep, 2007
Posts: 10


My Contributions
Thanks for the help but still isn't doing what i am looking for. Here is desired output:
1. <title> <duration>
2. <title> <duration>
and so on.
But My durations are not lining up. All titles and numbers left justify fine, but even though I set column width and told it << right before cout << title, it still seems to be printing the title and then going 80 spaces from the last character in the title before printing duration, causing durations not to line up

Those durations were supposed to be on the right side of the screen, by the by.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Right Justify
25 Nov, 2007 - 08:49 AM
Post #7

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,868



Thanked: 53 times
Dream Kudos: 550
My Contributions
first off you are not putting a width on the title or song number... this will cause problems since they push your collum over...

basically, you will always have this problem as long as you are no defining and enforcing the width for each column. The setw() manipulator sets the minimum width for a field, but it does not change the maximum, you have to do that yourself either by truncating strings that are too long or breaking the data into multiple rows.

Here is an example of formatting a table I posted a while back:
CODE
#include <string>
#include <iostream>
#include <sstream>
#include <iomanip>


using namespace std;

struct SomeData {
    string aString;
    long int anInt;
    double aDouble;
};

string split(string &in, int length);

int main()
{
    ostringstream oss(ostringstream::out);
    //Ok first lest get some data...
    SomeData data[5];
    data[0].aString = "Hello World";
    data[0].anInt = 11;
    data[0].aDouble = 3.14159265;
    data[1].aString = "To whome it may concern;";
    data[1].anInt = 1123;
    data[1].aDouble = 1.41421;
    data[2].aString = "Laugh and the world laughs with you...";
    data[2].anInt = 112358;
    data[2].aDouble = 0.707107;
    data[3].aString = "It was the best of times it was the worst of times...";
    data[3].anInt = 1123581321;
    data[3].aDouble = 1.61803399;
    data[4].aString = "When, in the course of human events, it becomes necessary for one poeple to dissolve the political bounds...";
    data[4].anInt = 1231853211;
    data[4].aDouble = 1.94161104;

    cout << setw(1) << "|" << setw(19) << "The String"
         << setw(1) << "|" << setw(19) << "The Integer"
         << setw(1) << "|" << setw(19) << "The Double"
         << setw(1) << "|" << endl;
    cout << setfill('-') << setw(61) << "" << setfill(' ') << endl;
    int counter = 0;
    string rows[3];
    while (counter < 5) {
        //Convert each of the data portions to strings for manipulation
        oss << data[counter].aString;
        rows[0] = oss.str();
        oss.str("");
        oss << data[counter].anInt;
        rows[1] = oss.str();
        oss.str("");
        oss << data[counter].aDouble;
        rows[2] = oss.str();
        oss.str("");

        //Print out the data formatted to the table
        while (rows[0] != "" || rows[1] != "" || rows[2] != "")
        {
            cout << setw(1) << "|" << setw(19) << left << split(rows[0], 20) << right
                 << setw(1) << "|" << setw(19) << split(rows[1], 20)
                 << setw(1) << "|" << setw(19) << split(rows[2], 20)
                 << setw(1) << "|" << endl;
        }
        cout << setfill('-') << setw(61) << "" << setfill(' ') << endl;
        counter++;
    }

    return 0;
}


//Split a string into two parts...
// return a part of at most "length"
// and remove from the original string.
string split(string &in, int length) {
    string retString = "";
    string temp;
    if (in.length() > length) {
        temp = in.substr(length - 1);
        in.resize(length - 1);
        retString = in;
        in = temp;
    } else
    {
        retString = in;
        in = "";
    }
    return retString;
}


In this example the long strings are broken up onto multiple rows to keep the table structure. Note that I am controlling the width of each field I print.

The program also uses ostringstream to convert numbers into strings. This is convenient to tell how many character a numeric field will take.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 09:40PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month