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:
_________________________________________________________
/ \
/ \
/ \
/ \
/ \
/ \
I don't understand what you expect
setwto do. What it does is: It sets the width of the output. For exampleresults in output
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):
Live Demo
For bonus points you can take a look at
std::setfilltoo. It sets the character that is used to fill the output. For example:results in output
std::setfillis sticky, so you have to reset it when you want the default space fill character again. With a loop your code can be just