0

This is the code which gives my desired output using spaces, but I have to perform the task using setw which isn't working as I want(2nd code attached). Help please!

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout <<     "           _________________________________________________________" <<endl ;
cout <<     "          /                                                         \\ " <<endl;
cout <<     "         /                                                           \\ " <<endl;
cout <<     "        /                                                             \\ " <<endl;
cout <<     "       /                                                               \\ " <<endl;
cout <<     "      /                                                                 \\ " <<endl;
cout <<     "     /                                                                   \\ " <<endl;


return 0;
}

Desired code which doesn't works:

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout << setw(15) <<    "_________________________________________________________" <<endl ;
cout <<  setw(14)<<   "/                                                          \\ " <<endl;
cout <<  setw(13)<<  "/                                                            \\ " <<endl;
cout <<  setw(12)<< "/                                                              \\ " <<endl;
cout <<  setw(11)<<"/                                                                \\ " <<endl;
cout << setw(10)<<"/                                                                  \\ " <<endl;
cout << setw(9)<<"/                                                                    \\ " <<endl;


return 0;
}

The desired output:

           _________________________________________________________
          /                                                         \
         /                                                           \
        /                                                             \
       /                                                               \
      /                                                                 \
     /                                                                   \

2 Answers2

3

I don't understand what you expect setw to do. What it does is: It sets the width of the output. For example

std::cout << std::setw(5) << 12;

results in output

   12
^ ^ 3 spaces
^   ^ total width = 5

This results in the desired output with more spaces in the front (because I was too lazy to count the exact amount, I'll leave that to you):

#include<iostream>
#include<iomanip>
int main()
{
    std::cout << std::setw(70) <<    "_________________________________________________________" << std::endl ;
    std::cout << std::setw(71) << "/                                                          \\ " << std::endl;
    std::cout << std::setw(72) << "/                                                            \\ " << std::endl;
    std::cout << std::setw(73) << "/                                                              \\ " << std::endl;
    std::cout << std::setw(74) << "/                                                                \\ " <<std::endl;
    std::cout << std::setw(75) << "/                                                                  \\ " <<std::endl;
    std::cout << std::setw(76) << "/                                                                    \\ " <<std::endl;

}

Live Demo

For bonus points you can take a look at std::setfill too. It sets the character that is used to fill the output. For example:

std::cout << std::setfill('x') << std::setw(5) << 12 << std::setfill(' ');

results in output

xxx12

std::setfill is sticky, so you have to reset it when you want the default space fill character again. With a loop your code can be just

std::cout << std::setw(15) << "_" << std::setw(40) << std::setfill('_') << "_" <<endl ;
std::cout << std::setfill(' ');
for (int i=0;i<6;++i){
    std::cout <<  setw(14-i) << "/" << std::setw(43+i*2) << "\\ " <<endl;
}
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
0

You probably understood how setw works now, so here is a minimal working code based on your question. Simply add a dedicated character (whitespace or else) as a target for each setw:

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
 cout << setw(14) << ' '  << setw(59) << setfill('_') << '_' << setfill(' ') << endl; 
 cout << setw(14) << '/' << setw(60) << '\\' << endl;                                                 
 cout << setw(13) << '/' << setw(62) << '\\' << endl;                                                 
 cout << setw(12) << '/' << setw(64) << '\\' << endl;                                                 
 cout << setw(11) << '/' << setw(66) << '\\' << endl;                                                 
 cout << setw(10) << '/' << setw(68) << '\\' << endl;                                                 
 cout << setw(9)  << '/' << setw(70) << '\\' << endl;                                                 

 return 0;
}

I checked, this code produces exactly your desired output.

Ralf Ulrich
  • 1,575
  • 9
  • 25