0

Im beginner to classes and I tried to make a simple library management system without file handling. So i used array instead. But when I try to show the input data by indexing(basically, entering number 2 after adding book to call the showBook()), it shows me random numbers and others are empty. I don't have any idea what is happening.

#include <iostream>

using namespace std;
int p = 0;

void addBook();
void showBooks();

struct Library{
  string title[5], author[5];
  int isbn[5], price[5];
};

int main()
{
    while (true){
    cout << "\t\tLibrary Management Sytem\n";
    cout << "1. ADD BOOK.\n";
    cout << "2. SHOW BOOKS\n";
    cout << "Number: ";
    int x;
    cin >> x;
    
    switch(x){
        case 1:
        addBook();
        break;
        case 2:
        showBooks();
        break;
        default:
        cout << "Invalid input.";
    }
    };
    
    return 0;
}
void addBook(){
    Library lib;
    cout << "Enter Book title: ";
    getline(cin, lib.title[p]);
    cin.ignore(232, '\n');
    cout << "Enter The Author: ";
    getline(cin, lib.author[p]);
    cout << "Enter ISBN: ";
    cin >> lib.isbn[p];
    cout << "Enter price: ";
    cin >> lib.price[p];
    p++;
}

void showBooks(){
    Library lib;
    for (int i = 0; i < p; i++){
        cout << "Book: " << lib.title[i] << endl;
    cout << "Author: " << lib.author[i] << endl;
    cout << "ISBN: " << lib.isbn[i] << endl;
    cout << "Price: " << lib.price[i];
    }
    
}
equinox_xy
  • 25
  • 6
  • Note: instead of a structure full of arrays, consider an array of structures. This is usually easier to manage. – user4581301 Nov 09 '21 at 01:07
  • Note rather than `p` being a global, make it a function parameter. Less potential for confusion. You should also give it a descriptive name. If all of your identifiers describe what they do or represent, code practically comments itself. Plus it's really easy to accidentally swap `p` and `q`, and if you have a `q` in the program, the compiler will probably allow it. Chaos ensues. – user4581301 Nov 09 '21 at 01:10
  • `cin.ignore(232, '\n');` kind of came out of nowhere. You might want to talk the purpose of the line over with [your Rubber Duck](https://en.wikipedia.org/wiki/Rubber_duck_debugging). It might be necessary, but there's no way for me to know. It just looks weird. – user4581301 Nov 09 '21 at 01:12
  • @user4581301 to be honest, i dont have idea about that cin.ignore(). i just saw it here cause we also had the same prblem where it skips when asked to input book title. https://stackoverflow.com/questions/25475384/when-and-why-do-i-need-to-use-cin-ignore-in-c – equinox_xy Nov 09 '21 at 01:18
  • To prevent the problem you're worried about the `ignore` needs to be after the last use of `>>` to read in data. `>>` stops as soon as it finds whitespace, newline is whitespace, and leaves the whitespace in the stream. You want to ignore that extra whitespace, otherwise the next call to `getline` will instantly find the newline and exit without reading anything useful. Do not place the `ignore` before a `getline` because sooner or later you'll find a path through the code that hits the `ignore` without data you need ignored and wind up `ignore`ing data you needed. – user4581301 Nov 09 '21 at 01:24

1 Answers1

0

I recommend you create a Book class that has one member each from Library. Change Library to be a collection of Book, such as std::vector or array. Parallel arrays (that you have in Library), can get out of sync. Also, by have an array of struct (or class) your elements are next to each other in the memory cache. Otherwise, author[0] will be placed after title[4], and author[0] may not be in the data cache, so the data cache will need to be reloaded.

struct Book
{
  std::string title;
  std::string author;
  std::string isbn;
  int         price;
};

typedef std::vector<Book> Library;

You may want to overload operator>> to input a Book from the console.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154