Can anyone please this line, if y == totalBook-1 then print book not found. This is a library management system and this code is for searching a book. I just dont get the y == totalBook-1 how does it work?
if(totalBook!=0){
cout<<"Enter the book ID you want to search: ";
cin>>id;
for(int y=0; y<totalBook; y ){
if(id==bookID[y]){
cout<<"Book found!"<<endl;
cout<<endl;
cout<<"Book name: "<<bookName[y]<<endl;
cout<<"Book author: "<<bookAuthor[y]<<endl;
cout<<"Book ID: "<<bookID[y]<<endl;
cout<<endl;
break;
}if(y == totalBook-1){
cout<<endl;
cout<<"Book not found!"<<endl;
cout<<endl;
Heres the full code
#include<iostream>
#include<stdlib.h>
using namespace std;
int main(){
char yesNo;
string bookName[50];
string bookAuthor[50];
string bookID[50];
int totalBook=0, choice; //for case 1
string id; // for case 2 and 3
int j; //case 2
do{
system("CLS");
cout << "==========================" << endl;
cout << "LIBRARY MANAGEMENT SYSTEM" << endl;
cout << "==========================" << endl;
cout << "[1] Add Books" << endl;
cout << "[2] Delete Books" << endl;
cout << "[3] Search Books" << endl;
cout << "[4] View Book List" << endl;
cout << "[5] Close Application" << endl;
cout<<"Enter a number: ";
cin>>choice;
switch(choice){
case 1: // add book
if(totalBook<50){
cin.ignore();
cout<<"Enter book name: ";
getline(cin, bookName[totalBook]);
cout<<"Enter book author: ";
getline(cin, bookAuthor[totalBook]);
cout<<"Enter book ID: ";
getline(cin, bookID[totalBook]);
totalBook ;
cout<<"Book succesfully added!"<<endl;
}else{
cout<<"Library is full!"<<endl;
}
break;
case 2: //delete a book
if(totalBook!=0){
cout<<"Enter the book ID you want to delete: ";
cin>>id;
for(int y=0; y<totalBook; y ){
if(id==bookID[y]){
for(j=y; j<totalBook; j )
bookID[j]=bookID[j 1];
bookName[j]=bookName[j 1];
bookAuthor[j]=bookAuthor[j 1];
totalBook--;
cout<<"Book deleted succesfully"<<endl;
cout<<endl;
break;
}else if(y == totalBook-1){
cout<<endl;
cout<<"Book not found!"<<endl;
cout<<endl;
}}
}else{
cout<<"The library is empty!"<<endl;
}
break;
case 3: // search book
if(totalBook!=0){
cout<<"Enter the book ID you want to search: ";
cin>>id;
for(int y=0; y<totalBook; y ){
if(id==bookID[y]){
cout<<"Book found!"<<endl;
cout<<endl;
cout<<"Book name: "<<bookName[y]<<endl;
cout<<"Book author: "<<bookAuthor[y]<<endl;
cout<<"Book ID: "<<bookID[y]<<endl;
cout<<endl;
break;
}if(y == totalBook-1){
cout<<endl;
cout<<"Book not found!"<<endl;
cout<<endl;
break;
}}
}else{
cout<<"The library is empty!"<<endl;
}
break;
case 4: // display all the books
if(totalBook!=0){
cout<<endl;
cout<<"===================="<<endl;
cout<<"All Books Available"<<endl;
cout<<"===================="<<endl;
cout<<endl;
for(int y=0; y<totalBook; y ){
cout<<"Book name: "<< bookName[y] << endl;
cout<<"Book author: "<< bookAuthor[y] << endl;
cout<<"Book ID: "<< bookID[y] << endl;
cout<<endl;
}
}else{
cout<<"The library is empty!"<<endl;
}break;
case 5:
cout<<"Thank You!";
exit(0);
}cout<<"Do you want to continue (Y/N): ";
cin>>yesNo;
}while (yesNo == 'y' || yesNo == 'Y');
cout<<"Thank You!";
return 0;
}
CodePudding user response:
If the book is available in the system, the program will exit the for loop.
The last value that will be assigned to y will be totalBook - 1
since y < totalBook
. If at that point, book could not be found, the text Book not found
will be printed.