Welcome to Dream.In.Code
Getting C++ Help is Easy!

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




Controling the width of stings

 
Reply to this topicStart new topic

Controling the width of stings

gogsholton
5 May, 2008 - 02:42 AM
Post #1

New D.I.C Head
*

Joined: 5 May, 2008
Posts: 1

Having some problems displaying this. The code runs fine but I can't space out each elements of the array equally. The "setw" function only puts a space after the output so the alignment is messed up by the length of the strings in the array. What I need is a way of making the width of each string in the arrays the same. I don't want to put spaces in the string, its a bit crude.

CODE
string speed[5]={"Reasonable","Fast","Lightning","Slow Poke","Fast"};
    string jump[5]= {"Very Good ","Average","Poor      ","OK          ","Awesome"};
    string luck[5]= {"Jammy","Unlucky","Very Jammy","Very Unlucky","No chance"};
    float cost[5]={100,250,400,100,400};
    
    //display horse names and abilities
    
    cout<<setw(15)<<brian<<setw(15)<<elvis_returns<<setw(15)<<vodka_breath<<setw(15)<<naka<<setw(15)<<ate_my_horse<<endl;

    cout<<"Speed:     "<<setw(5);
    for (i=0; i<5; i++)
    {
        cout<<speed[i]<<setw(15);
    }

    cout<<"\nJumping: "<<setw(5);
    for (i=0; i<5; i++)
    {
        cout<<jump[i]<<setw(15);
    }

    cout<<"\nLuck:    "<<setw(5);
    for (i=0; i<5; i++)
    {
        cout<<luck[i]<<setw(15);
    }

    cout<<"\nCost:    "<<setw(5);
    for (i=0; i<5; i++)
    {
        cout<<cost[i]<<setw(15);
    }

User is offlineProfile CardPM
+Quote Post

Cerolobo
RE: Controling The Width Of Stings
5 May, 2008 - 03:35 PM
Post #2

D.I.C Regular
Group Icon

Joined: 5 Apr, 2008
Posts: 440



Thanked: 31 times
My Contributions
http://www.arachnoid.com/cpptutor/student3.html

That should tell you everything you need.

Bascially, you just need to put a setw() in front of <<luck[i], <<jump[i], and <<speed[i].

Edit: Sorry, I didn't notice you were using setw()

This post has been edited by Cerolobo: 5 May, 2008 - 03:37 PM
User is offlineProfile CardPM
+Quote Post

Ambercroft
RE: Controling The Width Of Stings
6 May, 2008 - 06:45 PM
Post #3

D.I.C Head
Group Icon

Joined: 5 Jan, 2007
Posts: 107



Thanked: 3 times
Dream Kudos: 25
My Contributions
Try using cout.width(i);

This should do min number of characters for next output whereas setw does min number of characters ( spaces ) the next value will output.

With out the C++ compiler installed I wasn't able to verify this cool.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 11:48PM

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