Jose Miranda
Jose Miranda

Reputation: 97

setw() not working properly on my code

I'm having problem with my code not sure if it's a bug or there is something wrong with my code.

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

int main()
{
cout << setfill('*') << setw(80) << "*";
cout << setw(21) <<  "Mt.Pleasant Official Billing Statement" << endl;
cout << setfill('*') << setw(80) << "*" << endl;
return 0;
}

Adding manual spaces works but I want to add spaces programmatically but when I tested the application this what it looks like :

enter image description here

Upvotes: 5

Views: 18936

Answers (1)

Marco A.
Marco A.

Reputation: 43662

setw does not move the text but sets the minimum width it should take

To achieve what you have in mind you should experiment with a bigger value since your string is longer than 21 characters, e.g.

cout << setfill('*') << setw(80) << "*" << endl;
cout  << setfill(' ') << setw(56) <<  "Mt.Pleasant Official Billing Statement" << endl;
cout << setfill('*') << setw(80) << "*" << endl;

Output:

********************************************************************************
                  Mt.Pleasant Official Billing Statement
********************************************************************************

Upvotes: 6

Related Questions