12 Replies - 366 Views - Last Post: 09 July 2012 - 02:50 PM Rate Topic: -----

#1 jello92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-July 12

Columns to adjust according.

Posted 08 July 2012 - 03:32 PM

i have four columns and i need them to adjust accordingly to one another.
here is my code. i do not know how to make so that the shorter ones adjust to the longer ones. help!
but simple please! i am just beginning. the problem is that the "stuff" is the same in the four columns. How do i do something without changing the orginial "stuff"/

[
cout << object_one << setw(10) << number_one << "\n";
cout << object_two << setw(10) << number_two << "\n";


cout << fixed;
cout << setprecision(2);
cout << object_one.substr(0,object_one.length()-1) << "-to-" << object_two.substr(0,object_two.length()-1) << " ratio " << setw(10) << ratio_one << "\n";
cout << object_two.substr(0,object_two.length()-1) << "-to-" << object_one.substr(0,object_one.length()-1) << " ratio " << setw(10) << ratio_two << "\n";
]

This post has been edited by jimblumberg: 08 July 2012 - 03:38 PM
Reason for edit:: Added missing Code Tags, Please learn to use them.


Is This A Good Question/Topic? 0
  • +

Replies To: Columns to adjust according.

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: Columns to adjust according.

Posted 08 July 2012 - 04:20 PM

Hi, you can use a variable in setw().

 int colwidth = 12;

 cout << setw(colwidth) << "stuff" << endl;


Was This Post Helpful? 0
  • +
  • -

#3 jello92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-July 12

Re: Columns to adjust according.

Posted 08 July 2012 - 04:27 PM

but that isnt adjusting accordingly to the other columns? its only adjusting to setw(12)? or am i doing something wrong?
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3100
  • View blog
  • Posts: 10,889
  • Joined: 08-August 08

Re: Columns to adjust according.

Posted 08 July 2012 - 04:45 PM

Quote

... need them to adjust accordingly to one another...

What does that mean?
Was This Post Helpful? 0
  • +
  • -

#5 jello92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-July 12

Re: Columns to adjust according.

Posted 08 July 2012 - 05:36 PM

the columns should come out like this
stuff 14
stuff2 8
stuff-to-stuff2 ratio 2
stuff2-to-stuff ratio 4

the first two columns should adjust to the longer two.
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: Columns to adjust according.

Posted 08 July 2012 - 06:20 PM

Do you mean something like :

stuff ................14
stuff2 ................8
stuff-to-stuff2 ratio .2
stuff2-to-stuff ratio .4


This post has been edited by #define: 08 July 2012 - 06:21 PM

Was This Post Helpful? 0
  • +
  • -

#7 jello92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-July 12

Re: Columns to adjust according.

Posted 08 July 2012 - 06:40 PM

like this!

[
............... stuff ................14

...............stuff2 ................8
stuff-to-stuff2 ratio                .2

stuff2-to-stuff ratio                .4
]

Was This Post Helpful? 0
  • +
  • -

#8 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: Columns to adjust according.

Posted 08 July 2012 - 07:19 PM

If you find the length of the longest string you could use that. Is this what you are trying to do?


#include <iostream>
#include <iomanip>
#include <string>
using namespace std;


int main()
{
  string str("stuff2-to-stuff ratio");
  int colwid = str.length();

  cout << setw(colwid) << "stuff" << setw(10) << 14 << endl;
  cout << setw(colwid) << "stuff2" << setw(10) << 8 << endl;
  cout << setw(colwid) << "stuff-to-stuff2 ratio" << setw(10) << 2 << endl;
  cout << setw(colwid) << "stuff2-to-stuff ratio" << setw(10) << 4 << endl;

  // cin.get();
  return(0);
}



A function could be made to print a row.
Was This Post Helpful? 0
  • +
  • -

#9 jello92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-July 12

Re: Columns to adjust according.

Posted 08 July 2012 - 10:25 PM

its kinds of like that. The thing is that we do not know the length of the longest "stuff".
the user will input whatever objects they want and then the code has to adjust the columns accordingly to whatever is begin put in.
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,472
  • Joined: 05-May 12

Re: Columns to adjust according.

Posted 08 July 2012 - 11:23 PM

You'll have to buffer your output. Instead of writing directly to cout with your columnar data, you should call into a helper class that keeps track of the columns widths and caches the rows. Then when you are ready to output, call Flush() on the helper class and it'll iterate over rows that it stored.

I had some pseudo code written up, but my previous post attempt choked when DIC went tits up.
Was This Post Helpful? 0
  • +
  • -

#11 jello92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-July 12

Re: Columns to adjust according.

Posted 08 July 2012 - 11:44 PM

oi! i have no idea what a helper class is! i am totally confused. i have unfortunately started programming. but i really appreciate your help!
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,472
  • Joined: 05-May 12

Re: Columns to adjust according.

Posted 09 July 2012 - 12:00 AM

Forget that I used the adjective "helper", then. You'll need a class that keeps track of the column widths and caches the rows.
Was This Post Helpful? 0
  • +
  • -

#13 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: Columns to adjust according.

Posted 09 July 2012 - 02:50 PM

Usually a list of items would be read from the user and stored in arrays or other containers. Then the maximum length could be calculated and used to print the table.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1