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];
}
}