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

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




Creating a simple table

 
Reply to this topicStart new topic

Creating a simple table, with setw()

overclocked
post 12 Mar, 2008 - 01:09 PM
Post #1


New D.I.C Head

*
Joined: 24 Oct, 2007
Posts: 27


My Contributions


I am having problems creating a simple table. I am using setw() but the dividers for the table are not staying still when you cout the name and age. What am I doing wrong? I also found a way to sort the ages from greatest to smallest but I don't know how to attach the names to the ages but thats a different prob. Thanx!

cpp
      cout << "|" << name1 << setw(20)<<"   |   " <<setw(5)<<age1 << setw(5)<<"   |"<<endl;
cout << "|" << name2 << setw(20)<<" | " <<setw(5)<<age2 << setw(5)<<" |"<<endl;
cout << "|" << name3 << setw(20)<<" | " <<setw(5)<<age3 << setw(5)<<" |"<<endl;
cout << "|" << name4 << setw(20)<<" | " <<setw(5)<<age4 << setw(5)<<" |"<<endl;
cout << "|" << name5 << setw(20)<<" | " <<setw(5)<<age5 << setw(5)<<" |"<<endl;


I should look like this.
(Of course with different names and ages.)

| William Penn | 23 |
| William Penn | 23 |
| William Penn | 23 |
| William Penn | 23 |
| William Penn | 23 |


Instead this is what I get....
(Without the dots, had to put them in to show how it skips spaces)

| William Penn ............| 23 |
| William Penn |...........23 |
| William Penn | 23............|
| William Penn |....... 23 .....|
| William Penn........| 23 |

This post has been edited by overclocked: 12 Mar, 2008 - 01:14 PM
User is offlineProfile CardPM

Go to the top of the page

Pontus
post 12 Mar, 2008 - 01:13 PM
Post #2


Dreaming Coder / Coding Dreamer

Group Icon
Joined: 28 Dec, 2006
Posts: 519



Thanked 2 times

Dream Kudos: 275
My Contributions


Well, this
CODE

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
string name1="Wiliam shaun";
string name2="Wiliam shaun";
string name3="Wiliam shaun";
string name4="Wiliam shaun";
string name5="Wiliam shaun";
int age1=50;
int age2=50;
int age3=50;
int age4=50;
int age5=50;
cout << "|" << name1 << setw(20)<<"   |   " <<setw(5)<<age1 << setw(5)<<"   |"<<endl;  
cout << "|" << name2 << setw(20)<<"   |   " <<setw(5)<<age2 << setw(5)<<"   |"<<endl;  
cout << "|" << name3 << setw(20)<<"   |   " <<setw(5)<<age3 << setw(5)<<"   |"<<endl;  
cout << "|" << name4 << setw(20)<<"   |   " <<setw(5)<<age4 << setw(5)<<"   |"<<endl;  
cout << "|" << name5 << setw(20)<<"   |   " <<setw(5)<<age5 << setw(5)<<"   |"<<endl;
cin.get();
return 0;
}

runs perfectly when i tested it.
Wich compiler are you using?

This post has been edited by manhaeve5: 12 Mar, 2008 - 01:20 PM
User is offlineProfile CardPM

Go to the top of the page

overclocked
post 12 Mar, 2008 - 01:15 PM
Post #3


New D.I.C Head

*
Joined: 24 Oct, 2007
Posts: 27


My Contributions


QUOTE(manhaeve5 @ 12 Mar, 2008 - 02:13 PM) *

I dont see the difference, could u be more precise?



Sorry the forum took out my spaces so I added periods spaces to show what it looks like.
User is offlineProfile CardPM

Go to the top of the page

overclocked
post 12 Mar, 2008 - 02:49 PM
Post #4


New D.I.C Head

*
Joined: 24 Oct, 2007
Posts: 27


My Contributions


I'm using Borland.
User is offlineProfile CardPM

Go to the top of the page

jeronimo0d0a
post 12 Mar, 2008 - 03:51 PM
Post #5


D.I.C Head

**
Joined: 3 Mar, 2008
Posts: 141


My Contributions


It runs fine on my Borland.
User is offlineProfile CardPM

Go to the top of the page

overclocked
post 12 Mar, 2008 - 04:01 PM
Post #6


New D.I.C Head

*
Joined: 24 Oct, 2007
Posts: 27


My Contributions


Ok I just compiled it over again and it does work. I don't know why it didn't the first time. Thank you.
User is offlineProfile CardPM

Go to the top of the page

NickDMax
post 12 Mar, 2008 - 04:48 PM
Post #7


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,857



Thanked 47 times

Dream Kudos: 550
My Contributions


it seems to me that your setw(20) and names are reversed...
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 10:54PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month