Asithandile Ludonga
Asithandile Ludonga

Reputation: 13

How can I remove the whitespace at the end of each line

void printSudoku9x9(int grid[9][9]) {
    for (int y = 0; y < 9; y++){
        for (int x = 0; x < 9; x++)
            cout << grid[y][x] <<' ';
        cout << endl;
    }
}

Upvotes: 0

Views: 210

Answers (2)

Damir Tenishev
Damir Tenishev

Reputation: 3402

The usual way is to use the '\b' escape-character which moves the output cursor back to one character:

void printSudoku9x9(int grid[9][9]) {
    for (int y = 0; y < 9; y++){
        for (int x = 0; x < 9; x++)
            cout << grid[y][x] <<' ';
        cout << '\b' << endl;
    }
}

Upvotes: 0

selbie
selbie

Reputation: 104599

Instead of printing a space after each character and dealing with the end, print a space before each character printed except for when (x==0).

void printSudoku9x9(int grid[9][9]) {
    for (int y = 0; y < 9; y++){
        for (int x = 0; x < 9; x++) {
            char leading = (x == 0) ? "" : " ";
            cout << leading << grid[y][x];
        }
        cout << endl;
    }
}

Upvotes: 2

Related Questions