3 Replies - 1004 Views - Last Post: 13 April 2016 - 12:18 AM Rate Topic: -----

#1 de_rock   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-April 16

setw() function not working

Posted 12 April 2016 - 09:15 PM

can please somebody explain why setw() function not working below...please tnx...

#include <iostream>
#include <iomanip>

using namespace std;

void main()
{

cout<< " Please Choose your 7 beverage from below option:\n\n";
cout<< " 1.Green milk" <<setw(7) <<"$4.0\n";
cout<< " 2.Coconur Water"<<setw(6) <<"$3.0\n";
cout<< " 3.Alkaline Water"<<setw(4) <<"$2.0\n";
cout<< " 4.Mineral Water" <<setw(6) <<"$2.0\n";

system("pause")
}

Is This A Good Question/Topic? 0
  • +

Replies To: setw() function not working

#2 jimblumberg   User is offline

  • member icon

Reputation: 5521
  • View blog
  • Posts: 17,111
  • Joined: 25-December 09

Re: setw() function not working

Posted 12 April 2016 - 09:24 PM

How do you know it's not working? What is your output (please post the output into a post inside code tags to preserve the spaces.

Please use code tags when posting code.


Jim
Was This Post Helpful? 0
  • +
  • -

#3 de_rock   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-April 16

Re: setw() function not working

Posted 12 April 2016 - 11:29 PM

output is exactly same as in below, as some how opt.1,2,4 have some space before price but not opt3. i used to setw to get space between price and item....please advise..:) and of course there is ; after system pause...tnx...


<code>

1.Green milk $4.0
2.Coconur Water $3.0
3.Alkaline Water$2.0
4.Mineral Water $2.0


</code>
Was This Post Helpful? 0
  • +
  • -

#4 horace   User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: setw() function not working

Posted 13 April 2016 - 12:18 AM

rememeber that the string length of "$2.0\n" is 5 characters
in the case of
cout<< " 3.Alkaline Water"<<setw(4) <<"$2.0\n";

the lenegth of "$2.0\n" is longer than setw(4)

if you set the fill to some visible character and make the setw() larger you will see it is working as expected, e.g.
cout<< " Please Choose your 7 beverage from below option:\n\n";
cout<< " 1.Green milk" <<setfill('x' )<<setw(9) <<"$4.0\n";
cout<< " 2.Coconur Water"<<setw(8) <<"$3.0\n";
cout<< " 3.Alkaline Water"<<setw(6) <<"$2.0\n";
cout<< " 4.Mineral Water" <<setw(8) <<"$2.0\n";


gives
 Please Choose your 7 beverage from below option:

 1.Green milkxxxx$4.0
 2.Coconur Waterxxx$3.0
 3.Alkaline Waterx$2.0
 4.Mineral Waterxxx$2.0


This post has been edited by horace: 13 April 2016 - 12:19 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1