Click here to Skip to main content
15,889,743 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
my code below :

C++
/*password is admin*/
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>

using namespace std;

void list_all_books_1();
void list_available_books_2();
void borrow_books_3();
void search_for_abook_4();
void add_new_book_5();
void delete_books_6();


char path[] = "library books.txt";

void output(){
	//this function for displaying choices only
	cout << "***********************************" << endl;
	cout << "1. List all books in library" << endl;
	cout << "2. List available books to borrow " << endl;
	cout << "3. Borrow a Book from library" << endl;
	cout << "4. Search For a Book" << endl;
	cout << "5. Add New Books" << endl;
	cout << "6. Delete a Book" << endl;
	cout << "7. EXIT The Library" << endl;
	cout << "***********************************" << endl;
}

//=====================================================================================================================================================

struct books{
	//identfying books with all needed things
	int id, status;
	string title, p_name, p_address;
	string date;
};
struct author{
	string aut_name;
	string aut_nationality;
};

//=====================================================================================================================================================

//function for choice 1 showing the books (under constructions)

void list_all_books_1(){
	ifstream show;
	char all;
	show.open(path, ios::in | ios::app);
	while (!show.eof()){
		show >> all;
		if (all == '%')
			cout << "\n\n";
		
		else if (all == '.')
			cout << "\n\n\n";
		
		else if (all == ' ')
			cout << " ";
		
		else
			cout << all;
	}
	cout << endl;
	show.close();
}

//=====================================================================================================================================================

void list_available_books_2(){

	//function for choice 2 (list available books to borrow)
}

//=====================================================================================================================================================

void borrow_books_3(){

	//function for choice 3( Borrow a Book )
}

//=====================================================================================================================================================

void search_for_abook_4(){
	ifstream search;
	int idx, i = 0,get;
	cout << "enter the ID of the book you're looking for";
	cin >> idx;
	search.open(path, ios::in | ios::app);
	while (!search.eof()){
		search >> get;
		if (idx == get)
			i++;
		else
			i = 0;
	}
	if (i > 0)
		cout << "found";
	else
		cout << "not found";
}

//=====================================================================================================================================================

//for choice 5 to fill books (under constructions)
void add_new_book_5(){
	fstream d_base;
	int aut_number, booksnumber;
	books newbook[1000];
	author aut[100];
	cout << "how many books you want to add ? ";
	cin >> booksnumber;
	cout << "what books you want to add :" << endl;
	d_base.open(path, ios::out | ios::app);
	for (int i = 0; i < booksnumber; i++){

		cout << "id please : "; cin >> newbook[i].id;
		cout << "title : ";				 cin.ignore();   getline(cin, newbook[i].title);
		cout << "publisher name :";						 getline(cin, newbook[i].p_name);
		cout << "publisher address : ";				     getline(cin, newbook[i].p_address);

		d_base << "[BOOK INFO]" << "%Book Id : " << newbook[i].id << "%title : " << newbook[i].title;
		d_base << "%[PUBLISHER INFO]" << "%publisher name : " << newbook[i].p_name << "%puplisher address :" << newbook[i].p_address;
		d_base << "%[AUTHOR(s) INFO]";

		cout << "how many authors for the books";
		cin >> aut_number;

		for (int j = 0; j < aut_number; j++){
			cout << "author" << j + 1 << " name : ";	cin.ignore();    getline(cin, aut[j].aut_name);
			cout << "Nationality : ";									 getline(cin, aut[j].aut_nationality);

			d_base << "% Authors Name : " << aut[j].aut_name << "% Nationality : " << aut[j].aut_nationality;
		}

		cout << "Publish date :";					    getline(cin, newbook[i].date);
		cout << "How many copies of " << newbook[i].title << " ";		cin >> newbook[i].status;

		d_base << "%[MORE INFO]";
		d_base << "%PublishedAt : " << newbook[i].date << "%status :" << newbook[i].status << "." << endl;
	}
	d_base.close();
	cout << "Books Have Been Saved Sucessfully";

}

//=====================================================================================================================================================

void delete_books_6(){

	//function for searching for a book

}

//=====================================================================================================================================================

int main(){
	string choice;
	cout << "welcome to FCIS library\n\n";

	do{
		output();
		cout << "what do you want to do ? ";
		getline(cin, choice);

		if (choice == "1"){
			list_all_books_1();
		}

		//this one for list available books
		else if (choice == "2"){

			//not completed

		}

		//this one for borrow a book
		else if (choice == "3"){

			//not completed yet don't choose 3

		}
		else if (choice == "4"){

			search_for_abook_4();

		}

		//this one is for adding new books to the list
		else if (choice == "5"){
			string pass;

			do{

				cout << "you must be an admin to add new books." << endl << "please enter passowrd (use small letters) : ";
				cin >> pass;

				if (pass == "b")
					break;

				else if (pass == "admin"){
					cout << "ACCESS GAINED	 WELCOME " << endl;

					add_new_book_5();
				}
				else{
					cout << "Wrong password try again or press (b) to try another choice";
					continue;
				}

			} while (pass != "admin");
		}

		//this one for deleteing a book
		else if (choice == "6"){

			//not completed yet

		}
		else if (choice == "7"){

			cout << "Thanks for Using FCIS LIBRARY" << endl;
			break;

		}
		else
			cout << "\nwrong choice please choose again\n\n";

	} while (true);

}


What I have tried:

i have tried to open the file and search for id only but it doesn't do anything
Posted
Updated 21-Apr-16 13:04pm
Comments
Patrice T 21-Apr-16 17:35pm    
What is the, problem in this code and where?
Show a piece of data file.

1 solution

In the lines
C++
else
    i = 0;

you are resetting i to 0, even if you have found the idx before. Just delete those lines from your code.
 
Share this answer
 
Comments
CPallini 22-Apr-16 2:46am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900