I have a simple display() method that essentially looks like this

#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include "Pieces.h"


using namespace std;

void Pieces::BeginInstruct(int& i){

    cout<<endl<<endl;
    cout<<i++<<":"<<setw(5)<<"Open the program."<<endl
           <<i++<<":"<<setw(5)<< "Click \"File\" and then\"New\"."   
           <<endl;

    }

Theres obviously more but what I am concerned with is that setw() is not doing anything...Im trying to get the list of instructions to align yet the output shows

1:Open the program
2:Click "File" and then "New"

Lol Man i thought it was easy but it is giving me problems as well. I have used this function before and i know it works but it was in windows, i am using linux, are you linux as well?

I don't think its the operating system...But its not working for me either

Im using windows right now, and yeah same here, Ive used it many times before...I actually created a test program that simply just prints out some strings and integers and used setw() and it worked fine, but in this function that Ive posted above it does not seem to work, yet most everything about the two are the same...I dont know.

yeh i have some results here..

#include <iostream>

#include <iomanip>
using namespace std;


int main()
{
   char x[] = {"Opening the file"};
 
  


  
  cout<<(x)<<setw(40)<<x;

 
  
  //cout<<"\nSecond Message";


 return 0;
}

The way its done is the number of spaces subtract it with setw spaces , so opening file = 12 - 40 = difference is 38 spaces so i expect space of 38 characters, Check if it works for you

What do you think setw() does given how you used it? What is displayed and what did you expect it to display?

I was just testing your code and did a modification to it. And now it is possible to have empty spaces you needed.

#include <iostream>

#include <iomanip>
using namespace std;


int main()
{
   char x[] = {"Opening the file"};
   int i =0;
  



cout<<i++<<":"<<setw(20)<<"Open the program."<<endl<<i++<<":"<<setw(30)<< "Click \"File\" and then\"New\"."<<endl;


 return 0;
}

Output:
1: Open the program.
0: Click "File" and then"New".

Open the program is 17 characters subtract that from 20 gives 3 spaces and for the second case its 27 characters subtract 40 from it and you get 3 spaces from there as well. Hope this helps.

hmm yes I see it now, I guess I'll have to switch some things around, thanks that helps alot!

cout << i++ << ":" << setw(5) << "Open the program." << endl << i++ << ":" << setw(5) << "Click \"File\" and then\"New\"." << endl;

Default alignment is right (probably locale-based, but I've never used any RTL locale, so don't know), using char(32) for fill. If the output is longer than 5 characters, setw(5) isn't going to do anything. It applies only to what is immediately after it. Try instead:

cout << left;
cout << i++ << setw(5) << ":" << "Open the program." << endl;
cout << i++ << setw(5) << ":" << "Click \"File\" and then\"New\"." << endl;

You'll have to cout << right or reset iosflags if you later want to setw() and have output right-aligned.

@Software guy: An arbitrarily sized setw() on every line is a horrible way to go about formatting output.

Right right, I see how setw() works now, I feel as though there should be an easier way to align a table though, since there could possibly be unknown string lengths given after a call to setw()...

Right right, I see how setw() works now, I feel as though there should be an easier way to align a table though, since there could possibly be unknown string lengths given after a call to setw()...

Iterate through your strings, find the longest length, store it, and use that for setw(). Or, truncate output to a set number of characters.

Another problem is when I get to multiple digit numbers, then when I reuse a display method down the road, the alignment is one space further. That was actually the main problem I have been trying to resolve.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.