'How can I print the empty spaces with " | | " until the line ends [closed]

I am working with vectors and I wanna know how I can print the empty spaces in between until the line ends.

void print_vector(const std::vector < int > & v, int print_cols, int col_width) {
  //dash
  cout << string(print_cols * (col_width + 2) + 1, '-');
  cout << endl;
  //printing the vector in formated output
  cout << "|";
  for (size_t x = 0; x < v.size(); x++) {

    cout << right << setw(col_width) << v[x] << " |";
    //prints new line if it reaches limit of numbers per line
    if ((x + 1) % print_cols == 0) cout << endl << "|";

  }

  //dash
  cout << endl << string(print_cols * (col_width + 2) + 1, '-');
  cout << endl;
}

this is my current output: my output so far and sorry I can't embed images yet it wont let me. But this is the output that I want output needed



Solution 1:[1]

You can add an extra loop after the one you have and before the bottom row of dashes:

  • The loop would print a given number of blank columns.
  • The number of blank columns can be computed as print_cols - v.size() % print_cols.
  • The loop should not be executed if the number of blank columns is equal to the total number of columns. That would print a full row of empty columns. That case happens when the vector you want to print has a number of elements that is an exact multiple of the number of columns.
  • Every iteration of the loop should print col_width + 1 blanks and a '|' (or if you prefer it, to make it more consistent with your other code, col_width blanks plus a " |").

Yet you should fix another issue with your code:

  • The check for reaching the end of the row (enabling the print of a new line and a '|') should be instead a check for starting a row.
    ? If you do it at the end, for the case when the last element of the vector goes in the last column, you will unnecessarily add a new row.
    ? Doing it at the beginning, since you know you have at least one more number to show, you can print "\n|" and then the number.
    ? The check x % print_cols == 0 will tell you if x is the index of the first element of a row.

[Demo]

#include <iomanip>  // setw
#include <iostream>  // cout
#include <numeric>  // iota
#include <string>
#include <vector>

void print_vector(const std::vector<int>& v, int print_cols, int col_width) {
    // dash
    std::cout << std::string(print_cols * (col_width + 2) + 1, '-');

    // printing the vector in formated output
    for (size_t x = 0; x < v.size(); x++) {
        // prints new line if it is the first element of the line
        if (x % print_cols == 0) {
            std::cout << "\n|";
        }
        std::cout << std::right << std::setw(col_width) << v[x] << " |";
    }
    // prints last empty columns
    if (int number_of_blank_columns = print_cols - v.size() % print_cols;
        number_of_blank_columns != print_cols) {

        for (int x = 0; x < number_of_blank_columns; x++) {
            std::cout << std::string(col_width + 1, ' ') << "|";
        }
    }

    // dash
    std::cout << "\n" << std::string(print_cols * (col_width + 2) + 1, '-') << "\n";
}

int main() {
    {
        std::vector<int> v(8);
        std::iota(std::begin(v), std::end(v), 100);
        print_vector(v, 5, 4);
    }
    std::cout << "\n";
    {
        std::vector<int> v(10);
        std::iota(std::begin(v), std::end(v), 100);
        print_vector(v, 5, 4);
    }
}

// Outputs:
//
//   -------------------------------
//   | 100 | 101 | 102 | 103 | 104 |
//   | 105 | 106 | 107 |     |     |
//   -------------------------------
//
//   -------------------------------
//   | 100 | 101 | 102 | 103 | 104 |
//   | 105 | 106 | 107 | 108 | 109 |
//   -------------------------------

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1