Samir
Samir

Reputation: 235

C++ : Output of a formatted table

I'm trying to make a table for my output and I kinda got stuck in a part where I didn't know how to handle it. This is my program below :

#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
using namespace std;

int main()
{
    vector<vector<string>> myVector;

    for (int i = 0; i < 7; i++)
    {
        vector<string> temp;
        for (int j = 0; j < 6; j++)
        {
            temp.push_back("");
        }
        myVector.push_back(temp);
    }

    myVector[1][1] = "test1";
    myVector[6][3] = "test2";

   for (int i = 0; i < myVector.size(); i++)
    {
        for (int j = 0; j < myVector[i].size(); j++)
        {
            if (myVector[i][j] == "")
            {
                cout << setfill(' ') << setw(20) << "|";
            }
            else
            {
                cout << myVector[i][j] << setfill(' ') << setw(20 - myVector[i][j].length()) << "|";
            }
        }

        cout << "\n|------------------|-------------------|-------------------|-------------------|-------------------|-------------------|" << endl;
    }

    return 0;
}

What I'm basically trying to do is to create some sort of timetable where I can put data in it . I managed to do a part of it .

This is what I had as an output

                   |                   |                   |                   |                   |                   |
|------------------|-------------------|-------------------|-------------------|-------------------|-------------------|
                   |test1              |                   |                   |                   |                   |
|------------------|-------------------|-------------------|-------------------|-------------------|-------------------|
                   |                   |                   |                   |                   |                   |
|------------------|-------------------|-------------------|-------------------|-------------------|-------------------|
                   |                   |                   |                   |                   |                   |
|------------------|-------------------|-------------------|-------------------|-------------------|-------------------|
                   |                   |                   |                   |                   |                   |
|------------------|-------------------|-------------------|-------------------|-------------------|-------------------|
                   |                   |                   |                   |                   |                   |
|------------------|-------------------|-------------------|-------------------|-------------------|-------------------|
                   |                   |                   |test2              |                   |                   |
|------------------|-------------------|-------------------|-------------------|-------------------|-------------------|

And as you can see when I assigned myVector[6][3] = "test2" the data went to that cell . The issue that I'm facing right now is when widening the table . For example if I want to assign another vector variable "myVector1" and write myVector1[6][3]="test3" I want to him to go to the same cell too but in the second line . So the expected output would be :

                   |                   |                   |                   |                   |                   |
                   |                   |                   |                   |                   |                   |
|------------------|-------------------|-------------------|-------------------|-------------------|-------------------|
                   |test1              |                   |                   |                   |                   |
                   |                   |                   |                   |                   |                   |
|------------------|-------------------|-------------------|-------------------|-------------------|-------------------|
                   |                   |                   |                   |                   |                   |
                   |                   |                   |                   |                   |                   |
|------------------|-------------------|-------------------|-------------------|-------------------|-------------------|
                   |                   |                   |                   |                   |                   |
                   |                   |                   |                   |                   |                   |
|------------------|-------------------|-------------------|-------------------|-------------------|-------------------|
                   |                   |                   |                   |                   |                   |
                   |                   |                   |                   |                   |                   |
|------------------|-------------------|-------------------|-------------------|-------------------|-------------------|
                   |                   |                   |                   |                   |                   |
                   |                   |                   |                   |                   |                   |
|------------------|-------------------|-------------------|-------------------|-------------------|-------------------|
                   |                   |                   |test2              |                   |                   |
                   |                   |                   |test3              |                   |                   |
|------------------|-------------------|-------------------|-------------------|-------------------|-------------------|

I tried many alternatives to reach this output but failed at the end .

Upvotes: 1

Views: 417

Answers (1)

Jarod42
Jarod42

Reputation: 218333

You might do:

    std::vector<std::vector<std::string>> myVector(7, std::vector<std::string>(6));
    std::vector<std::vector<std::string>> myVector1(7, std::vector<std::string>(6));

    myVector[1][1] = "test1";
    myVector[6][3] = "test2";
    myVector1[6][3] = "test3";

   for (std::size_t  i = 0; i != myVector.size(); i++)
    {
        for (std::size_t j = 0; j != myVector[i].size(); j++)
        {
            std::cout << myVector[i][j] << std::setfill(' ') << std::setw(20 - myVector[i][j].length()) << "|";
        }
        std::cout << "\n";

        for (std::size_t j = 0; j != myVector1[i].size(); j++)
        {
            std::cout << myVector1[i][j] << std::setfill(' ') << std::setw(20 - myVector1[i][j].length()) << "|";
        }
        std::cout << "\n";

        std::cout << "|------------------|-------------------|-------------------|-------------------|-------------------|-------------------|" << std::endl;
    }

Demo

I would even suggest to create sub function for your inner loop:

auto print_line = [](const auto& line){
    for (const auto& word : line)
    {
        std::cout << word << std::setfill(' ') << std::setw(20 - word.length()) << "|";
    }
    std::cout << "\n";
};

for (std::size_t  i = 0; i != myVector.size(); i++)
{
    print_line(myVector[i]);
    print_line(myVector1[i]);
    std::cout << "|------------------|-------------------|-------------------|-------------------|-------------------|-------------------|" << std::endl;
}

Demo

Upvotes: 2

Related Questions