What did I do wrong that setw() function not working?

Correct me if i'm wrong. From my understanding of setw() function is to make spaces? So why is that my code not working?

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{

  int fieldLength = 10;
  cout << setw(fieldLength) << "HONNOUJI ACADEMY" << endl;
  cout << "          " << "HONNOUJI ACADEMY" << endl;
}


EDIT: I'd like to make line 10 like the way i did to line 11 how do i do that?
Last edited on
setw sets the "width" of the next output. It is useful when you want things to line up, like a table.

The string has 16 characters which is less than 10 so setw doesn't make a difference in your code. If you want 10 spaces in front of the string you have to pass a value of 26 (number of spaces + length of string) to setw.
setw creates spaces on one line.. if you want the output to be on line 10 then you need to cout 9 endl or '\n' then setw(1) << "honnouji academy"
Topic archived. No new replies allowed.