-2

can you check what's wrong with my code? I just want to make program that can store information in a text file and will display in text file like this:

BookID BookTitle AuthorName BookDepartment BookIssuer

But it only success for 1 book only, and if I enter for 2 books, it will get infinity

#include<iostream>
#include<fstream>
#include<iomanip>

using namespace std;

void IssueBooks(){
    int SIZE;
    cout << "How many books you want to borrow?"<< endl;
    cin >> SIZE;

    string booksTitle[SIZE], department[SIZE], authorName[SIZE], 
    BooksIssuer[SIZE];
    double booksID[SIZE];

    for (int i = 0; i < SIZE; i++ ){
            for (int j = 0; j < SIZE; j++){
                cout << "Enter Book ID:" << endl;
                cin >> booksID[j];

                cout << "\nEnter Book Title:" << endl;
                cin >> booksTitle[j];

                cout << "\nEnter Book Author:" << endl;
                cin >> authorName[j];

                cout << "\nEnter Book Department:" << endl;
                cin >> department[j];

                cout << "\nEnter Book Issuer: " << endl;
                cin >> BooksIssuer[j];

                ofstream out("IssueRecord.txt");
                out << setw(10) << booksID[j];
                out << setw(10) << booksTitle[j];
                out << setw(10) << authorName[j];
                out << setw(10) << department[j];
                out << setw(10) << BooksIssuer[j];

            }
        }




}

int main(){
    IssueBooks();

return 0;
}

Thank you

  • 1
    I don't think you need the outer for loop for starters – Tyler Sep 18 '17 at 21:16
  • Unrelated: By making all of the variables local to `IssueBooks` you've made it hard on yourself to get the gathered information back out of function. – user4581301 Sep 18 '17 at 21:27
  • 1
    `string booksTitle[SIZE]` and other fixed-sized arrays using `SIZE` for their size are known as "variable-length arrays" which is a non-standard extension supported by only a few compilers. Use `std::vector` instead. Actually, in this example, there is no need to use any arrays at all. – Remy Lebeau Sep 18 '17 at 21:40
  • @RemyLebeau I had time to spare :) +1 – sehe Sep 18 '17 at 21:56
  • I only started writing C++ a little while ago not even 3 months. The least i could do is array for now. – Mohamad Azimuddin Kamaludin Sep 19 '17 at 07:43

1 Answers1

4

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 |
-----------+--------------------------------+---------------------------+---------------------------+-----------------+-
sehe
  • 374,641
  • 47
  • 450
  • 633
  • 1
    @MohdAzimuddin I have never used it. Just tried it. **Use c++11 mode.** Here's my [complete guided tour in 19 visual steps](https://imgur.com/gallery/OhRI3) – sehe Sep 20 '17 at 21:40