7 cc Hg
7 cc Hg

Reputation: 21

How can I create a table?

I have an assignment to write a .txt file using inputs from the codes i made from c++, i wanted to make a table and the easiest solution is using ncurses. I was taught to use fstream but I don't know how to use fstream to take the output from ncurses

Upvotes: 0

Views: 512

Answers (1)

Botje
Botje

Reputation: 31054

You do not need ncurses to make a table. As a hint, take a look at the std::setw and std::right iostream manipulators. They define the minimum amount of space a field should take when printed and the alignment within that space, respectively.

As a simple example, let's print a table:

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

using std::cout;
using std::endl;
using std::right;
using std::setw;
using std::string;
using std::vector;

int main() {
    vector<vector<string>> data = {
        { "Homer", "Simpson", "42" },
        { "Marge", "Simpson", "35" },
        { "Bart", "Simpson", "10" },
        { "Lisa", "Simpson", "9" },
        { "Maggie", "Simpson", "2" },
        { "Ned", "Flanders", "40" },
    };

    for (auto& row : data) {
        cout << setw(10) << row[0] << setw(10) << row[1] << setw(5) << row[2] << endl;
    }
}

Output:

     Homer   Simpson   42
     Marge   Simpson   35
      Bart   Simpson   10
      Lisa   Simpson    9
    Maggie   Simpson    2
       Ned  Flanders   40

Upvotes: 1

Related Questions