How to use setw

Hello, I have been trying to use setw with no success, have been reading the forums and they all seem to be saying something similar to this, however no matter the value I enter for setw, nothing is shifting.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <iomanip>
using namespace std;

int main()

{

	cout <<                  "Noahs Horse Yard\n"; 
	cout <<    "____________________________________________________\n";
	
	cout  << "Horse type"; 
	cout << fixed << setw(20) << " Minimum Optimum weight";







	system("PAUSE");
	return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <iomanip>

int main()
{
    std::cout << "***|" << "Minimum Optimum weight" << "|***\n" ;
    std::cout << "***|" << std::setw(30) << "Minimum Optimum weight" << "|***\n" ;
    std::cout << "***|" << std::left << std::setw(30) << "Minimum Optimum weight" << "|***\n" ;

    const double number = 1234.5678 ;
    std::cout << std::fixed << std::showpos << std::setprecision(3) << '\n' ;
    std::cout << "***|" << number << "|***\n" ;
    std::cout << "***|" << std::right << std::setw(15) << number << "|***\n" ;
    std::cout << "***|" << std::left << std::setw(15) << number << "|***\n" ;
    std::cout << "***|" << std::internal << std::setw(15) << number << "|***\n" ;
    std::cout << "***|" << std::setfill('#') << std::right << std::setw(15) << number << "|***\n" ;
}

***|Minimum Optimum weight|***
***|        Minimum Optimum weight|***
***|Minimum Optimum weight        |***

***|+1234.568|***
***|      +1234.568|***
***|+1234.568      |***
***|+      1234.568|***
***|######+1234.568|***

http://coliru.stacked-crooked.com/a/56f98404c7a8d1e0
setw() simply sets the next stream's width with default positioning of the characters, additionally you'd need std::left(), std::right() to tell the compiler with what alignment you want the stream's characters to line up:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <iomanip>
using namespace std;

int main()

{

	cout << setw(45) <<                  "Noahs Horse Yard\n";
	//stream width set to 45, default aligned

	cout <<    "____________________________________________________\n";
	//no format

	cout  << "Horse type \n";
	//no format

	cout << setw(45) << std::right << "Minimum Optimum Weight \n";
	//stream width set to 45, aligned right

	cout << setw(45) << std::left << "Minimum Optimum Weight \n";
	//stream width set to 45, aligned left

//	system("PAUSE");
	return 0;
}
Last edited on
It would appear that you are trying to centre text in a field. Here's a manipulator to centre text and numbers:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <iomanip>
#include <string>

namespace manip
{
    namespace detail
    {
        template < typename CHAR, typename TRAITS = std::char_traits<CHAR>,
                   typename ALLOC = std::allocator<CHAR> >
        struct centre
        {
            template < typename STR_OR_CSTR >
            centre( const STR_OR_CSTR& str, std::size_t width ) : str(str), width(width) {}

            std::basic_string<CHAR,TRAITS,ALLOC> str ;
            std::size_t width ;

            template < typename OSTREAM >
            friend OSTREAM& operator<< ( OSTREAM& stm, const centre& cntr )
            {
                std::size_t padl = 0 ;
                std::size_t padr = 0 ;
                if( cntr.width > cntr.str.size() )
                {
                    padl = ( cntr.width - cntr.str.size() ) / 2 ;
                    padr = padl + ( cntr.width - cntr.str.size() ) % 2 ;
                }

                for( std::size_t i = 0 ; i < padl ; ++i ) stm << stm.widen(' ') ;
                stm << cntr.str ;
                for( std::size_t i = 0 ; i < padr ; ++i ) stm << stm.widen(' ') ;

                return stm ;
            }
        };
    }

    template < typename CHAR > // centre c-style strings
    detail::centre<CHAR> centre( const CHAR* cstr, std::size_t width )
    { return { cstr, width } ; }

    template < typename CHAR, typename TRAITS, typename ALLOC > // centre C++ strings
    detail::centre<CHAR,TRAITS,ALLOC> centre( const std::basic_string<CHAR,TRAITS,ALLOC>& str, std::size_t width )
    { return { str, width } ; }

    template < typename NUM_TYPE > // centre numbers (output as narrow characters)
    typename std::enable_if< std::is_arithmetic<NUM_TYPE>::value,
                             detail::centre<char> >::type
    centre( const NUM_TYPE& number, std::size_t width )
    { return { std::to_string(number), width } ; }

    template < typename NUM_TYPE > // centre numbers (output as wide characters)
    typename std::enable_if< std::is_arithmetic<NUM_TYPE>::value,
                             detail::centre<wchar_t> >::type
    wcentre( const NUM_TYPE& number, std::size_t width )
    { return { std::to_wstring(number), width } ; }
}

int main()
{
    using namespace std::literals ;

    std::cout << '|' << manip::centre( "abcde", 20 ) << "|\n"
              << '|' << manip::centre( "fghijk"s, 20 ) << "|\n"
              << '|' << manip::centre( "lmnopqr"s, 20 ) << "|\n"
              << '|' << manip::centre( -123.45, 20 ) << "|\n" ;

    std::wcout << L'|' << manip::centre( L"jklmno", 20 ) << L"|\n"
               << L'|' << manip::centre( L"pqrstuv"s, 20 ) << L"|\n"
               << L'|' << manip::centre( L"lmnopqr"s, 20 ) << L"|\n"
               << L'|' << manip::wcentre( -123.45, 20 ) << "|\n" ;
}

http://coliru.stacked-crooked.com/a/9179201f3501b271
http://rextester.com/HSPCU85191
z
I still do not understand, all of those codes are totally different, and only confuse me more.
I have added right and left to the cout and experimented with both, however that has not changed anything, and the text still does not move.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <iomanip>
#include<conio.h>
using namespace std;

int main()

{

	cout <<                  "Noahs Horse Yard\n"; 
	cout <<    "____________________________________________________\n";
	
	cout  <<  setw(10) << right << "Horse type"; 
	cout  << " Minimum Optimum weight";







	system("PAUSE");
	return 0;
}
All that I am trying to do is move the text in a specific direction to make columns. I do not understand any complex codes yet
UPDATE::
Here is what I have ended up getting, the code will set the width properly for the first 2 outputs in the column, however it still does not set the width for the 3rd term while the code appears to be the exact same as the other 2.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <iomanip>
using namespace std;

int main()

{

	cout <<                  "Noahs Horse Yard\n"; 
	cout <<    "_____________________________________________________________________\n";
	
	cout.setf(ios::left);
	cout.width(20);     cout << "Horse type";
	cout.width(20);     cout << " Minimum Optimum weight";
	cout.width(20);  	cout << "Maximum Optimum Weight\n";

	cout << "________________________________________________________________________\n";








	system("PAUSE");
	return 0;
}
Hello TheArk,

On line 10 the space between >> and " means nothing. If your idea is to center the text over the line you will need to add spaces between the first " and the first letter or you could use setw.

On line 13 your text has 10 characters and the setw is 10. You will not see anything from the setw because it is the same size as the text. change the setw to 15 run the program and see the difference. If lines 13 and 14 are column headings then I would use the setw before each string.

I once set up an Excel spread sheet with 120 columns each having a width of 1. I entered the type that I needed and used the columns to count the spaces I needed for the setw value.

I am not sure how you want the final output to look, so for now I can only guess. Maybe something like this:

1
2
3
4
5
std::cout << std::right << std::setw(34) << "Noahs Horse Yard\n";
std::cout << "____________________________________________________\n";

std::cout << std::right << std::setw(13) << "Horse type";
std::cout << std::right << std::setw(32) << " Minimum Optimum weight";


BTW "conio.h" is not needed in this program you have posted. Or maybe you have it for something that is not here.

Hope hat helps,

Andy
@handy andy, here is the updated code. The first output is aligning, however the other 2 are not adjusting at all.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <iomanip>
using namespace std;

int main()

{

	cout << "Noahs Horse Yard\n";
	cout << "_____________________________________________________________________\n";

	cout << left << setw(15) << "Horse type";
	cout << left << setw(20) << " Minimum Optimum weight";
	cout << left << setw (15)<< "Maximum Optimum Weight\n";

	cout << "________________________________________________________________________\n";








	system("PAUSE");
	return 0;
}
I only want to create a column for the Horse type, Minimum optimum weight, and maximum optimum weight.
Hello TheArk,

The value of the "setw" in line 14 will have to large enough to cover the size of "Maximum Optimum Weight" plus any space you will need to separate the column headings. So, the 15 will have to start at 22 for the length of the string plus the number of spaces you will need.

In lines 12 - 13 the setw manipulates what follows the setw. setw does not put space between items printed to the screen. So, if you need space between something you will have to take the length of the string and add any spce to thet length.

The 15 in the setw on line 14 would be closer to 30.

Hope that helps,

Andy
Topic archived. No new replies allowed.