Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setw() not working properly on my code

Tags:

c++

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

like image 776
Jose Miranda Avatar asked Sep 21 '14 06:09

Jose Miranda


1 Answers

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
********************************************************************************
like image 63
Marco A. Avatar answered Sep 25 '22 04:09

Marco A.