Here's to good style and C++ code:
- use structs for related data
- use vectors
- use getline to allow for spaces in input fields
separate concerns (don't print output while reading input, that makes no sense from a functional point of view)
int main() {
Books issued = borrowBooks();
std::cout << "Successfully borrowing " << issued.size() << " books, saving receipt\n";
printReceipt(issued, "IssueRecord.txt");
}
handle errors
As a bonus I showed pretty printing the table guided by statically configured columns:
struct {
int width;
std::string header;
} columns[] = { { 10, "ID" }, { 30, "Title" }, { 25, "Author" },
{ 25, "Department" }, { 15, "Issuer" } };
Live On Coliru
#include <vector>
#include <fstream>
#include <iomanip>
#include <iostream>
struct Book {
int id = 0;
std::string title, department, author, issuer;
};
using Books = std::vector<Book>;
bool promptDetails(Book& book) try {
std::cin.exceptions(std::ios::failbit);
std::cout << "Enter Book ID (enter 0 to complete): ";
if (std::cin >> book.id) {
std::cin.ignore(1024, '\n');
}
if (book.id == 0)
return false;
std::cout << "Enter Book Title: ";
std::getline(std::cin, book.title);
std::cout << "Enter Book Author: ";
std::getline(std::cin, book.author);
std::cout << "Enter Book Department: ";
std::getline(std::cin, book.department);
std::cout << "Enter Book Issuer: " << std::endl;
std::getline(std::cin, book.issuer);
return true;
} catch(std::exception const& e) {
std::cout << "Input error (" << e.what() << ")\n";
return false;
}
Books borrowBooks() {
Books books;
while (true) {
Book book;
if (!promptDetails(book))
break;
books.push_back(std::move(book));
}
return books;
}
void printReceipt(Books const& receipt, std::string const& fname) {
std::ofstream out(fname);
struct {
int width;
std::string header;
} columns[] = { { 10, "ID" }, { 30, "Title" }, { 25, "Author" },
{ 25, "Department" }, { 15, "Issuer" } };
for (auto& col : columns) out << std::setw(col.width) << col.header << " | ";
out << "\n" << std::setfill('-');
for (auto& col : columns) out << std::setw(col.width) << "" << "-+-";
out << "\n" << std::setfill(' ');
for (auto& book : receipt) {
out << std::setw(columns[0].width) << book.id << " | "
<< std::setw(columns[1].width) << book.title << " | "
<< std::setw(columns[2].width) << book.author << " | "
<< std::setw(columns[3].width) << book.department << " | "
<< std::setw(columns[4].width) << book.issuer << " |" << "\n";
}
for (auto& col : columns) out << std::setw(col.width) << std::setfill('-') << "" << "-+-";
out << "\n";
}
int main() {
Books issued = borrowBooks();
std::cout << "Successfully borrowing " << issued.size() << " books, saving receipt\n";
printReceipt(issued, "IssueRecord.txt");
}
If the input is
29134
The Hen And Her Trust Issues
Mojit Bunghead
Pop Culture
Central Library
4893
Most Unnecessary
P.H.W. Graftink
Science Fiction
Central Library
6083
Code Of Concoct
Public Domain
Folklore
Central Library
0
The output file looks like
ID | Title | Author | Department | Issuer |
-----------+--------------------------------+---------------------------+---------------------------+-----------------+-
29134 | The Hen And Her Trust Issues | Mojit Bunghead | Pop Culture | Central Library |
4893 | Most Unnecessary | P.H.W. Graftink | Science Fiction | Central Library |
6083 | Code Of Concoct | Public Domain | Folklore | Central Library |
-----------+--------------------------------+---------------------------+---------------------------+-----------------+-