Apr 13, 2015 at 6:43am UTC
#include <iostream>
#include <fstream>
#include <cstring>
#include <iomanip>
#include <cstdlib>
using namespace std;
typedef struct
{
char isbn[200];
char author[200];
char title[200];
char edition[30];
char publisher[500];
char price[100];
}BOOK;
void menu();
void list(BOOK record[], int rows);
int search(BOOK record[], int rows);
int remove(BOOK record[], int rows);
int main()
{
ifstream infile("data.txt");
if(!infile)
cout << "Error opening file.\n";
else
{
BOOK record[30];
int choice;
int i=-1;
while(infile)
{
infile.getline(record[++i].isbn, 200);
infile.getline(record[i].author, 200);
infile.getline(record[i].title, 200);
infile.getline(record[i].edition, 30);
infile.getline(record[i].publisher,500);
infile.getline(record[i].price, 100);
}
infile.close();
ofstream outfile("data.txt");
while(true)
{
menu();
cout << "\nPlease pick an option --> ";
cin >> choice;
switch(choice)
{
case 1:
{
system("cls");
list(record, i);
system("pause");
system("cls");
break;
}
case 2:
{
system("cls");
search(record, i);
system("pause");
system("cls");
break;
}
case 3:
{
system("cls");
i = remove(record, i);
system("pause");
system("cls");
break;
}
case 4:
{
system("cls");
}
case 5:
{
for (int h = 0; h < i; h++)
{
outfile << record[h].isbn << endl;
outfile << record[h].author << endl;
outfile << record[h].title << endl;
outfile << record[h].edition << endl;
outfile << record[h].publisher << endl;
outfile << record[h].price << endl;
}
outfile.close();
return 0;
}
break;
default:
{
cout << "Unknown number detected. Please enter again. \n\n";
}
}
}
}
return 0;
}
void menu()
{
cout<<"*********************MENU*****************************\n";
cout<<"******************************************************\n";
cout<<"*\t1. List all records **\n";
cout<<"*\t2. Search for a book record **\n"; //display menu
cout<<"*\t3. Delete an existing book record **\n";
cout<<"*\t4. Exit **\n";
cout<<"******************************************************\n\n";
return;
}
void list(BOOK record[], int rows)
{
int j;
cout << fixed << setprecision(2);
for (j = 0; j < rows; j++)
{
cout << record[j].isbn << setw(20) << record[j].author<< setw(20) << record[j].title << setw(20) << record[j].edition << setw(20) << record[j].publisher << setw(20) << record[j].price <<endl;
}
}
int search(BOOK record[], int rows)
{
char search_isbn[15];
int j = 0;
cout << "Please enter ISBN of book or 0 to return\n"
<< "Enter your number: ";
fflush(stdin);
cin.getline(search_isbn, 15);
if (search_isbn[0] == '0' && strlen(search_isbn) == 1)
return -1;
for (int j = 0; j < rows; j++) //This loop serves to scan through all isbn in the system and find the correct isbn as input.
{
if (strcmp(search_isbn, record[j].isbn) == 0)
{
cout << "\nThis is the book found.\n" << endl;
cout << "ISBN \t\t: " << record[j].isbn << endl;
cout << "Author \t\t: " << record[j].author << endl;
cout << "Title \t\t: " << record[j].title << endl;
cout << "Edition \t: " << record[j].edition << endl;
cout << "Publisher \t: " << record[j].publisher << endl;
cout << "Price \t\t: " << fixed << setprecision(2) << record[j].price << endl;
cout << "\n";
return j;
}
wanna add additional item to menu and maintaining book records