Home > Software engineering >  Arranging Vector Outputs from multiple string variables
Arranging Vector Outputs from multiple string variables

Time:10-21

This is an assignment I am attempting to work on where I am attempting to create a program that asks for a books title, its author, and its date of publication, before displaying all inputs at the end. My primary obstacle is trying to get everything to display in a somewhat decent way, with additional text written to clarify the information properly.

It should look like this:

Title: Great Expectations     Fahrenheit 451       Animal Farm 
Author:   Charles Dickens       Ray Bradbury     George Orwell
Date:                1824               1956              1948
#include <string>
#include <vector>
using namespace std;

int main ()
{
  int i=0;
  int n;
  string bookTitle;
  string bookAuthor;
  string bookDate;
  vector<string> Title;
  vector<string> Author;
  vector<string> Date;

  cout << "Enter the Number of Books: ";
  cin >> n;
  cin.ignore();
  for (i; i < n; i  )
    {
      cout << "Type the Book's Title: ";
      getline (cin, bookTitle);
      Title.push_back(bookTitle);
      cout << "Type the Book's Author: ";
      getline (cin, bookAuthor);
      Author.push_back(bookAuthor);
      cout << "Type the Book's Date: ";
      getline (cin, bookDate);
      Date.push_back(bookDate);
    }
    cout << endl;
    cout << "The Titles: ";
    for (auto it = Title.begin(); it != Title.end();   it) {
    cout << *it << " ";
}
    cout << endl;
    cout << "Written by: ";
    for (auto ib = Author.begin(); ib != Author.end();   ib) {
cout << *ib << " ";
}
    cout << endl;
    cout << "Written in: ";
    for (auto ic = Date.begin(); ic != Date.end();   ic) {
cout << *ic << " ";
}
  return 0;
}

CodePudding user response:

First, let's organize your code a bit, by storing each book in a structure:

struct Book {
    string title;
    string author;
    string date;
};

vector<Book> books;

Read them like this:

for (int i = 0; i < n; i  )
{
    Book book;
    getline (cin, book.title);
    getline (cin, book.author);
    getline (cin, book.date);
    books.push_back(book);
}

For each book, you can store a useful column width for your output. To keep it simple, only the book's title and author are considered when choosing the column width:

vector<int> widths;
int padding = 2;
for (const Book& book : books)
{
    widths.push_back(padding   std::max(book.title.size(), book.author.size()));
}

Now, use std::setw from <iomanip> to set the appropriate column width for each book:

cout << "The Titles: ";
for (int i = 0; i < n; i  ) {
    cout << std::setw(widths[i]) << books[i].title;
}
cout << endl;

cout << "Written by: ";
for (int i = 0; i < n; i  ) {
    cout << std::setw(widths[i]) << books[i].author;
}
cout << endl;

cout << "Written in: ";
for (int i = 0; i < n; i  ) {
    cout << std::setw(widths[i]) << books[i].date;
}
cout << endl;

Output:

The Titles:   Great Expectations  Fahrenheit 451    Animal Farm
Written by:      Charles Dickens    Ray Bradbury  George Orwell
Written in:                 1824            1956           1948

CodePudding user response:

I would recommend using the std::setw() function within the library to set a consistent field width to format the output neatly.

For example:

cout << setw(WIDTH) << *it << " ";
  • Related