1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
#include <iostream>
#include <windows.h>
#include <iomanip>
#include <fstream>
using namespace std;
//constant definitions
const char FILE_NAME[] = "BooksLibrary.txt";
const int ARRAY_SIZE = 100;
class Books
{
public:
//default constructor
Books();
Books(string, string, string, string);
//Mutators
void setData(string, string, string, string);
void writeFile();
//Accessors
void getData() const;
void printData() const;
void readfile();
private:
struct BookData
{
string title;
string authur;
string publisher;
string isbn;
}data[ARRAY_SIZE];
};
int main()
{
//variables definition
char option = ' ';
string title = "";
string authur = "";
string publisher = "";
string isbn = "";
//class object
//Books books;
do {
system("cls");
cout << "\n ==========================================================";
cout << "\n Library ";
cout << "\n ==========================================================";
cout << "\n [1] New Entry ";
cout << "\n [2] View Listing";
cout << "\n [3] Exit";
cout << "\n ==========================================================";
cout << "\n Please input your selection (1,2,3): ";
cin >> option;
switch (option)
{
case '1':
{
system("cls");
cout << "\n ==========================================================";
cout << "\n New Entry ";
cout << "\n ==========================================================" << endl;
cout << "\n Please enter the title of the book: ";
cin.get();
getline(cin, title);
cout << "\n Please enter the author of the book: ";
getline(cin, authur);
cout << "\n please enter the publisher of the book: ";
getline(cin, publisher);
cout << "\n Please enter the ISBN number of the book: ";
getline(cin, isbn);
//takes the data the use input and stores it in struct array
Books libary(title, authur, publisher, isbn);
system("cls");
cout << "\n ===================================";
cout << "\n Thank You! ";
cout << "\n ===================================" << endl;
cout << "\n Your Book has been added!" << endl;
system("pause");
}//end of case 1
break;
case '2':
//books.setData();
system("pause");
break;
case '3':
break;
default:
cout << "\n\a ERROR INVALID ENTRY PLEASE TRY AGAIN!!!" << endl;
system("pause");
cout << endl;
break;
}//end of switch statement
}while(option!= '3');//end of do-while
return 0;
}
//function definitions
Books::Books()
{
for (int x = 0; x < ARRAY_SIZE ; x++)
{
Books::data[x].title = "";
Books::data[x].authur = "";
Books::data[x].publisher = "";
Books::data[x].isbn = "";
}//end of for loop
}//end of default constructor
Books::Books(string t, string a, string p, string i)
{
setData(t, a, p, i);
}
void Books::setData(string t, string a, string p, string i)
{
for (int x = 0; x < ARRAY_SIZE; x++)
{
if(data[x].title == " ")
{
data[x].title = t;
}
if(data[x].authur == " ")
{
data[x].authur = a;
}
if(data[x].publisher == " ")
{
data[x].publisher = p;
}
if(data[x].isbn == " ")
{
data[x].isbn = i;
break;
}
}
}//end of set data
void Books::writeFile()
{
fstream fileObject;//file stream variable
fileObject.open(FILE_NAME, ios::app);//open file to write to it
if(!fileObject)//check to see if the file is open.
{
cout << "\n\a Error File Cannot Open!"
<< "\n Terminating Program";
exit(1);
}
else
{
for (int x = 0; x < ARRAY_SIZE; x++)
{
fileObject << left
<< data[x].title << " "
<< data[x].authur << " "
<< data[x].publisher << " "
<< data[x].isbn << " " << endl;
}
}//end of if else
fileObject.close();
}//end of write file function
|