PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 15, 2022

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

 August 15, 2022     c++, formatting, output     No comments   

Issue

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

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 << "|";
  size_t x = 0;
  for (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 < v.size() - 1) {
      if ((x + 1) % print_cols == 0) {
         cout << endl << "|";
      }
    }

  }
  size_t remain = print_cols - (x % print_cols);
  for (size_t i = 0; (remain != print_cols) && i < remain; ++i) {
    cout << right << setw(col_width) << " " << " |";
  }
  //dash
  cout << endl << string(print_cols * (col_width + 2) + 1, '-');
  cout << endl;
}

Sample outputs:

-------------------------------------------------
|         1 |         2 |         3 |         4 |
|         5 |         6 |         7 |         8 |
|         9 |           |           |           |
-------------------------------------------------

-------------------------------------
|         1 |         2 |         3 |
|         4 |         5 |         6 |
|         7 |         8 |         9 |
-------------------------------------



Answered By - Boris Hu
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing