0

In the line below, setw(3) is ignored when I compile:

cout << setw(3) << sym;

I found out that not using a variable works fine:

cout << setw(3) << "*";

Below are both versions of the full code (the only difference is in line 13) and an image of both of them being compiled and ran.

Not working as intended:

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

char sym = '*';

int main(){

    for (int i = 1; i <= 3; i++){
        for (int j = 1; j <= 3; j++){
            cout << setw(3) << sym;
        }
        cout << endl;
    }
 
    return 0;
}

Working as intended:

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

char sym = '*';

int main(){

    for (int i = 1; i <= 3; i++){
        for (int j = 1; j <= 3; j++){
            cout << setw(3) << "*";
        }
        cout << endl;
    }
 
    return 0;
}

Image of each being compiled and run:

image of each being compiled and run

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83

0 Answers0