Book shop Program

Pages: 12
Hello, I have this assignment to do which is writing a program that will help a book shop owner to manage his store. Until now, I finished most of it but I face a few issues:
1- I have trouble opening the file containing the list of books, each time I press the view books option, It shows blank.
2- I need to add two functions: "Update stock level. Write a function called updateBookItem() that takes as parameter the book and the quantity to be added/removed. Check that enough items are there if the operation is a remove operation." and "Remove an item. You cannot remove an array element. However, let us use a negative bookNo to indicate a removed item. When saving the arrays to the file, only save items with positive bookNo". I don't even know where to start with these two functions.
3- I need to transform the whole code into a function based program. I have a general knowledge of functions but I would really appreciate it if anyone could help me.
[code]
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
int main() {

double choice;
char RTM;
static int entry = 1;
static string AuthorName[500], BookName[500];
double averagePrice[500], BookISBN[500], BookPrice[500], BookQuantity[500], GivenPrice[500];

ofstream outputFile;
ifstream inputFile;

backtomenu:

cout << "____________________________________________________________\n";

cout << " BOOKSHOP MANAGEMENT SYSTEM\n";
cout << "____________________________________________________________\n";
cout << " " << endl;
cout << "Enter [1] to Add New Item\n";
cout << "Enter [2] to View All Items\n";
cout << "Enter [3] to View Items with quantity < 5\n";
cout << "Enter [4] to View Items Above Given Price\n";
cout << "Enter [5] to View Books Above Average Price\n";
cout << "Enter [6] to View Stock Statistics\n";
cout << "Enter [7] to Remove an Item\n";
cout << "Enter [0] to Exit\n";
cout << " " << endl;
cout << "Please enter your choice: ";
cin >> choice;


if (choice == 1)
{
cout << "You have chosen choice [1]" << endl;

bookEntry:

do
{

outputFile.open("C:\\Users\\isss1\\OneDrive\\Desktop\\book.txt");

cout << "Please Enter the Author Name: ";
cin.ignore();
getline(cin, AuthorName[entry]);

outputFile << AuthorName[entry] << endl;
cout << "Please Enter the Book Name: ";
getline(cin, BookName[entry]);
outputFile << BookName[entry] << endl;

cout << "Please Enter the Book ISBN: ";
cin >> BookISBN[entry];
outputFile << BookISBN[entry] << endl;

cout << "Please Enter the Price in QAR: ";
cin >> BookPrice[entry];
outputFile << BookPrice[entry] << endl;

cout << "Please Enter the Quantity: ";
cin >> BookQuantity[entry];
outputFile << BookQuantity[entry] << endl;

cout << "Data Entry Complete!" << endl;

cout << "The Book \'" << BookName[entry] << "\' has been added a file" << endl;

cout << "Do you want to enter more books?(Y/N)" << endl;
cin >> RTM;

entry++;

} while (RTM == 'y' || RTM == 'Y');

outputFile.close();

cout << "Press M to go back to the main menu or 0 to terminate the program" << endl;
cin >> RTM;

if (RTM == 'M' || RTM == 'm')
{
goto backtomenu;
}
else
return 0;
}


if (choice == 2)

{
cout << "You have chosen choice [2]" << endl;

inputFile.open("C:\\Users\\isss1\\OneDrive\\Desktop\\book.txt");

cout << "Reading data from the file" << endl;
cout << " " << endl;
cout << setw(45) << "Author Name" << setw(75) << "Book Name" << setw(25) << "Book ISBN" << setw(20) << "Book Price(QAR" << setw(20) << "Book Quantity" << endl;

for (int i = 1; i <= 10; i++) {

if (BookISBN[i] != 0) {
inputFile >> AuthorName[i];
inputFile >> BookName[i];
inputFile >> BookISBN[i];
inputFile >> BookPrice[i];
inputFile >> BookQuantity[i];
cout << setw(45) << AuthorName[i] << setw(75) << BookName[i] << fixed << setprecision(0) << setw(25) << BookISBN[i] << setw(20) << BookPrice[i] << setw(20) << BookQuantity[i] << endl;
}
}

inputFile.close();

char RTM;

cout << " " << endl;

cout << "Press M to go back to the main menu or 0 to terminate the program" << endl;
cin >> RTM;

{if (RTM == 'm' || RTM == 'M')
goto backtomenu;
else
return 0;
}
}


if (choice == 3)
{
cout << "You have chosen choice [3]" << endl;
for (int i = 1; i <= 10; i++) {
if (BookISBN[i] != 0) {
if (BookQuantity[i] < 5)
{
cout << "The item with a quantity less than 5 is: " << BookName[i] << " by " << AuthorName[i] << " ,with a quantity of: " << BookQuantity[i] << endl;
}
}
else {

cout << "There are no books whose quantity is less than 5" << endl;
cout << " " << endl;
cout << "Press M to go back to the main menu or 0 to terminate the program" << endl;
cin >> RTM;

{if (RTM == 'm' || RTM == 'M')
goto backtomenu;
else
return 0;
}
}
}

cout << "Press M to go back to the main menu or 0 to terminate the program" << endl;
cin >> RTM;

if (RTM == 'm' || RTM == 'M')
{
goto backtomenu;
}
else
return 0;
}


if (choice == 4) {
cout << "You have chosen choice [4]" << endl;
float GivenPrice;
GivenPrice:
cout << "Please Enter a Price and I will display all items above the given price" << endl;
cin >> GivenPrice;

cout << "The items above the given price are: ";
for (int i = 1; i <= 10; i++) {
if (BookISBN[i] != 0) {
if (BookPrice[i] > GivenPrice)
{
cout << BookName[i] << " by " << AuthorName[i] << endl;
}
else {
cout << "There are no items above the given price" << endl;

cout << "Press M to go back to the main menu or 0 to terminate the program" << endl;
cin >> RTM;

if (RTM == 'M' || RTM == 'm')
goto backtomenu;
else
return 0;

}
}
}
cout << "Do you want to enter another given price?(Y/N)" << endl;
cin >> RTM;

if (RTM == ('y' | 'Y'))
{
goto GivenPrice;
}
else
cout << "Press M to go back to the main menu or 0 to terminate the program" << endl;
cin >> RTM;

if (RTM == 'M' || RTM == 'm')
goto backtomenu;
else
return 0;

}






if (choice == 5) {
cout << "You have chosen choice [5]" << endl;

float AveragePrice = 0;
int k = 0;

for (int i = 1; i <= 10; i++) {
AveragePrice += BookPrice[i];
if (BookISBN[i] > 0)
k++;
}

cout << "The Average price is : " << AveragePrice / k << " QAR" << endl;

for (int i = 1; i <= 10; i++) {
if (BookPrice[i] > AveragePrice / k) {
cout << BookName[i] << " by " << AuthorName[i] << " costs " << BookPrice[i] << " QAR" << endl;
}
}

cout << "Press M to go back to the main menu or 0 to terminate the program" << endl;
cin >> RTM;

{if (RTM == 'M' || RTM == 'm')
goto backtomenu;
else
return 0; }
}


if (choice == 6) {

float totalstockvalue = 0;

for (int i = 1; i <= 10; i++) {

totalstockvalue += BookPrice[i] * BookQuantity[i];

}

cout << "The Total Stock Value is: " << totalstockvalue << " QAR" << endl;

float AveragePrice = 0;
int k = 0;

for (int i = 1; i <= 10; i++) {

AveragePrice += BookPrice[i];
if (BookISBN[i] > 0)
k++;
}

cout << "The Average price is : " << AveragePrice / k << "QAR" << endl;

int cheapest = 1000000, mostexpensive = BookPrice[0], leaststocked = 1000000;
int cheapestcounter, mostexpensivecounter, leaststockedcounter;

for (int i = 0; i <= 10; i++) {

if (cheapest >= BookPrice[i] && BookISBN[i] > 0) {
cheapest = BookPrice[i];
cheapestcounter = i;
}

if (mostexpensive <= BookPrice[i]) {
mostexpensive = BookPrice[i];
mostexpensivecounter = i;
}

if (leaststocked >= BookQuantity[i] && BookISBN[i] > 0) {
leaststocked = BookQuantity[i];
leaststockedcounter = i;
}
}

cout << "The cheapest book is " << BookName[cheapestcounter] << " by " << AuthorName[cheapestcounter] << " at price " << cheapest << " QAR" << endl;
cout << "The most expensive book is " << BookName[mostexpensivecounter] << " by " << AuthorName[mostexpensivecounter] << " at price " << mostexpensive << " QAR" << endl;
cout << "The least stocked item " << BookName[leaststockedcounter] << " by " << AuthorName[leaststockedcounter] << " at quantity " << leaststocked << " QAR" << endl;

cout << " " << endl;
cout << "Enter M to return to the main menu or 0 to terminate the program" << endl;

cin >> RTM;

if (RTM == 'M' || RTM == 'm')
goto backtomenu;

else
return 0;
}



/*

if (choice == 7)

{ //Remove items

cout << "You have chosen choice [7]" << endl;

cout << "Press M to go back to the main menu or 0 to terminate the program" << endl;



cin >> RTM;



{if (RTM == 'M' || RTM == 'm')



goto backtomenu;

else

return 0; }
}
*/


if (choice == 0)
cout << "You have chosen choice [0]" << endl;
return 0;


if (choice != (1 | 2 | 3 | 4 | 5 | 6 | 7 | 0))
cout << "WRONG ENTRY, ENTER A CORRECT NUMBER" << endl;
goto backtomenu;

}

[/code]

Many Thanks.
1- I have trouble opening the file containing the list of books, each time I press the view books option, It shows blank.


First, see if you can actually open the file.

1
2
3
4
5
6
std::ifstream infile; // create the object for file opening
infile.open("myfile.txt"); // try to open it
if(!infile) {
  std::cerr << "Unable to open the file.\n";
  exit(1);
}


I need to add two functions...You cannot remove an array element...

I am going to assume you're using simple arrays denoted with the subscript operator []. In this case, we'll need to see some code first.

I need to transform the whole code into a function based program.

We'll need to see what you have so far.
Can you post your book.txt file?
1;Adamson Iain T.; Data Structures and Algorithms: A First Course; ISBN: 3540760474; 59; 2
2;Cormen Thomas H.; Introduction to Algorithms; ISBN: 0262032937; 100; 5
3;Drozdek Adam; Data Structures and Algorithms in C++; ISBN: 0534375979; 6.01; 3
4;Flamig Bryan; Practical Data Structures in C++; ISBN: 047155863X; 201; 11
5;Ford William; Data Structures with C++ Using STL; ISBN: 0130858501; 64.99; 1
6;Gilberg Richard F.; Data Structures: A Pseudocode Approach with C++; ISBN: 053495216X; 50; 7
7;Harrington Jan L.; Object-Oriented C++ Data Structures for Real Programmers; ISBN: 0123264294; 200; 2
8;Headington Mark R.; Data Abstraction & Structures Using C++; ISBN: 0763702951; 125; 4
9;Horowitz Ellis; Fundamentals of Data Structures in C++; ISBN: 0716782928; 25; 10
10;Hubbard John; Schaum's Outline of Data Structures with C++; ISBN: 0071353453; 10; 6
11;Knuth Donald Ervin; The Art of Computer Programming: Fundamental Algorithms; ISBN: 0201896834; 129.99; 10
12;Knuth Donald Ervin; The Art of Computer Programming, Volume 2: Seminumerical Algorithms; ISBN: 0201896842; 110; 2
13;Knuth Donald Ervin; Art of Computer Programming, Volume 3: Sorting and Searching; ISBN: 0201896850; 105; 2
14;Kruse Robert L.; Data Structures and Program Design in C++; ISBN: 0137689950; 7.50; 6
15;Lafore Robert; Sams Teach Yourself Data Structures and Algorithms in 24 Hours; ISBN: 0672316331; 1000; 100
16;Langsam Yedidyah; Data Structures Using C and C++; ISBN: 0130369977; 49.99; 3
17;Nyhoff Larry R.; C++: An Introduction to Data Structures; ISBN: 0023887257; 15; 2
18;Main Michael; Data Structures and Other Objects Using C++; ISBN: 0201702975; 500; 99
19;Preiss Bruno R.; Data Structures and Algorithms: With Object-Oriented Design Patterns in C++; ISBN: 0471241342; 1.99; 57
20;Sahni Sartaj; Data Structures, Algorithms, and Applications in C++; ISBN: 0071092196; 45; 17
21;Sedgewick Robert; Algorithms in C++, Parts 1-4: Fundamentals, Data Structure, Sorting, Searching; ISBN: 0201350882; 70; 38
22;Shaffer Clifford A.; A Practical Introduction to Data Structures and Algorithm Analysis; ISBN: 0130284467; 2000; 1
23;Weiss Mark Allen; Data Structures & Algorithm Analysis in C++; ISBN: 0201361221; 2500; 1
@fiji885
I just tried your code and the file is opening.
However, it's still not showing the books, Its either blank or it shows crazy numbers.

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;


double choice;
char RTM;
static int entry = 1;
static string AuthorName[500], BookName[500];
double averagePrice[500], BookISBN[500], BookPrice[500], BookQuantity[500], GivenPrice[500];

ofstream outputFile;
ifstream inputFile;


void menu()
{

backtomenu:

cout << "____________________________________________________________\n";

cout << " BOOKSHOP MANAGEMENT SYSTEM\n";
cout << "____________________________________________________________\n";
cout << " " << endl;
cout << "Enter [1] to Add New Item\n";
cout << "Enter [2] to View All Items\n";
cout << "Enter [3] to View Items with quantity < 5\n";
cout << "Enter [4] to View Items Above Given Price\n";
cout << "Enter [5] to View Books Above Average Price\n";
cout << "Enter [6] to View Stock Statistics\n";
cout << "Enter [7] to Remove an Item\n";
cout << "Enter [0] to Exit\n";
cout << " " << endl;
cout << "Please enter your choice: ";
cin >> choice;
}

void addBooks()
{
if (choice == 1)
{
cout << "You have chosen choice [1]" << endl;

bookEntry:

do
{

outputFile.open("C:\\Users\\isss1\\OneDrive\\Desktop\\book.txt");

cout << "Please Enter the Author Name: ";
cin.ignore();
getline(cin, AuthorName[entry]);

outputFile << AuthorName[entry] << endl;
cout << "Please Enter the Book Name: ";
getline(cin, BookName[entry]);
outputFile << BookName[entry] << endl;

cout << "Please Enter the Book ISBN: ";
cin >> BookISBN[entry];
outputFile << BookISBN[entry] << endl;

cout << "Please Enter the Price in QAR: ";
cin >> BookPrice[entry];
outputFile << BookPrice[entry] << endl;

cout << "Please Enter the Quantity: ";
cin >> BookQuantity[entry];
outputFile << BookQuantity[entry] << endl;

cout << "Data Entry Complete!" << endl;

cout << "The Book \'" << BookName[entry] << "\' has been added a file" << endl;

cout << "Do you want to enter more books?(Y/N)" << endl;
cin >> RTM;

entry++;

} while (RTM == 'y' || RTM == 'Y');

outputFile.close();

cout << "Press M to go back to the main menu or 0 to terminate the program" << endl;
cin >> RTM;
}
}

void viewBooks()
{
if (choice == 2)
{
cout << "You have chosen choice [2]" << endl;

inputFile.open("C:\\Users\\isss1\\OneDrive\\Desktop\\book.txt");

cout << "Reading data from the file" << endl;
cout << " " << endl;
cout << setw(45) << "Author Name" << setw(75) << "Book Name" << setw(25) << "Book ISBN" << setw(20) << "Book Price(QAR" << setw(20) << "Book Quantity" << endl;

for (int i = 0; i <= 30; i++) {

if (BookISBN[i] != 0) {
inputFile >> AuthorName[i];
inputFile >> BookName[i];
inputFile >> BookISBN[i];
inputFile >> BookPrice[i];
inputFile >> BookQuantity[i];
cout << setw(45) << AuthorName[i] << setw(75) << BookName[i] << fixed << setprecision(0) << setw(25) << BookISBN[i] << setw(20) << BookPrice[i] << setw(20) << BookQuantity[i] << endl;
}
}

inputFile.close();

char RTM;

cout << " " << endl;

cout << "Press M to go back to the main menu or 0 to terminate the program" << endl;
cin >> RTM;
}
}

void viewless5()
{
if (choice == 3)
{
cout << "You have chosen choice [3]" << endl;
for (int i = 1; i <= 10; i++) {
if (BookISBN[i] != 0) {
if (BookQuantity[i] < 5)
{
cout << "The item with a quantity less than 5 is: " << BookName[i] << " by " << AuthorName[i] << " ,with a quantity of: " << BookQuantity[i] << endl;
}
}
else {

cout << "There are no books whose quantity is less than 5" << endl;
cout << " " << endl;
cout << "Press M to go back to the main menu or 0 to terminate the program" << endl;
cin >> RTM;
}
}

cout << "Press M to go back to the main menu or 0 to terminate the program" << endl;
cin >> RTM;

}
}

void aboveGprice()
{

if (choice == 4) {
cout << "You have chosen choice [4]" << endl;
float GivenPrice;
GivenPrice:
cout << "Please Enter a Price and I will display all items above the given price" << endl;
cin >> GivenPrice;

cout << "The items above the given price are: ";
for (int i = 1; i <= 10; i++)
{
if (BookISBN[i] != 0)
{
if (BookPrice[i] > GivenPrice)
{
cout << BookName[i] << " by " << AuthorName[i] << endl;
}
else {
cout << "There are no items above the given price" << endl;


}
}
}

}
}





void viewAverage()
{
if (choice == 5) {
cout << "You have chosen choice [5]" << endl;

float AveragePrice = 0;
int k = 0;

for (int i = 1; i <= 10; i++) {
AveragePrice += BookPrice[i];
if (BookISBN[i] > 0)
k++;
}

cout << "The Average price is : " << AveragePrice / k << " QAR" << endl;

for (int i = 1; i <= 10; i++) {
if (BookPrice[i] > AveragePrice / k) {
cout << BookName[i] << " by " << AuthorName[i] << " costs " << BookPrice[i] << " QAR" << endl;
}
}

cout << "Press M to go back to the main menu or 0 to terminate the program" << endl;
cin >> RTM;
}
}


void viewStock()
{
if (choice == 6) {

float totalstockvalue = 0;

for (int i = 1; i <= 10; i++) {

totalstockvalue += BookPrice[i] * BookQuantity[i];

}

cout << "The Total Stock Value is: " << totalstockvalue << " QAR" << endl;

float AveragePrice = 0;
int k = 0;

for (int i = 1; i <= 10; i++) {

AveragePrice += BookPrice[i];
if (BookISBN[i] > 0)
k++;
}

cout << "The Average price is : " << AveragePrice / k << "QAR" << endl;

int cheapest = 1000000, mostexpensive = BookPrice[0], leaststocked = 1000000;
int cheapestcounter, mostexpensivecounter, leaststockedcounter;

for (int i = 0; i <= 10; i++) {

if (cheapest >= BookPrice[i] && BookISBN[i] > 0) {
cheapest = BookPrice[i];
cheapestcounter = i;
}

if (mostexpensive <= BookPrice[i]) {
mostexpensive = BookPrice[i];
mostexpensivecounter = i;
}

if (leaststocked >= BookQuantity[i] && BookISBN[i] > 0) {
leaststocked = BookQuantity[i];
leaststockedcounter = i;
}
}

cout << "The cheapest book is " << BookName[cheapestcounter] << " by " << AuthorName[cheapestcounter] << " at price " << cheapest << " QAR" << endl;
cout << "The most expensive book is " << BookName[mostexpensivecounter] << " by " << AuthorName[mostexpensivecounter] << " at price " << mostexpensive << " QAR" << endl;
cout << "The least stocked item " << BookName[leaststockedcounter] << " by " << AuthorName[leaststockedcounter] << " at quantity " << leaststocked << " QAR" << endl;
}
}





if (choice == 7)

{ //Remove items

cout << "You have chosen choice [7]" << endl;

cout << "Press M to go back to the main menu or 0 to terminate the program" << endl;



cin >> RTM;



{if (RTM == 'M' || RTM == 'm')



goto backtomenu;

else

return 0; }
}


int main()
{
if (choice == 0)
cout << "You have chosen choice [0]" << endl;
return 0;


if (choice != (1 | 2 | 3 | 4 | 5 | 6 | 7 | 0))
cout << "WRONG ENTRY, ENTER A CORRECT NUMBER" << endl;

}


int main()
{
if (choice == 2)
{
cout << "You have chosen choice [2]" << endl;

infile.open("myfile.txt"); // try to open it

if (!infile) {
std::cerr << "Unable to open the file.\n";
exit(1);
}
else
cout << "Reading data from the file" << endl;
cout << " " << endl;
cout << setw(45) << "Author Name" << setw(75) << "Book Name" << setw(25) << "Book ISBN" << setw(20) << "Book Price(QAR" << setw(20) << "Book Quantity" << endl;

for (int i = 0; i <= 30; i++) {

if (BookISBN[i] != 0) {
inputFile >> AuthorName[i];
inputFile >> BookName[i];
inputFile >> BookISBN[i];
inputFile >> BookPrice[i];
inputFile >> BookQuantity[i];
cout << setw(45) << AuthorName[i] << setw(75) << BookName[i] << fixed << setprecision(0) << setw(25) << BookISBN[i] << setw(20) << BookPrice[i] << setw(20) << BookQuantity[i] << endl;
}
}

inputFile.close();

char RTM;

cout << " " << endl;

cout << "Press M to go back to the main menu or 0 to terminate the program" << endl;
cin >> RTM;
}
}
Here is the code that I tried to turn to an array.
Are you sure that ISBN is supposed to be a double? Why not a string? I need to know this before giving you an answer.

Also, it's empty because you're not reading the file in properly. Your data file is formatted a certain way, but you're reading it in another way.
Yes, I'm sure that's it's a double because the ISBN is a 10 numbers long code. It can also be an int.
Make it a string. ISBN numbers can start with a 0. It's very hard to read the code you've given because things like

1
2
3
if (BookISBN[i] != 0) {
    ...
}


does not make sense.

Get your reading from the file correct first. You can't really do anything useful otherwise without it. In my honest opinion, you are better off coding it from scratch instead of trying to refactor this code. There is too much stuff you've tried to do in here that I just can't make sense out of and things that you should not have done.

Read in a whole line from the input file and parse that line. Collect their title, author, price, etc by parsing the string. The way you're currently reading will not work because you have to work with delimiters.
This is my code:

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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include <iostream>
#include <fstream>
#include<iomanip>
#include<string>
#include<cstdlib >
using namespace std;

int choice;
const int ARRAY_SIZE = 500;
string authorName[ARRAY_SIZE], bookName[ARRAY_SIZE];
int bookISBN[ARRAY_SIZE], bookPrice[ARRAY_SIZE], bookQuantity[ARRAY_SIZE], bookNo[ARRAY_SIZE];
ofstream outFile;
ifstream inFile;
string RTM;

int menu()
{
    

menu:

    cout << "____________________________________________________________\n";

    cout << "                 BOOKSHOP MANAGEMENT SYSTEM\n";
    cout << "____________________________________________________________\n";
    cout << " " << endl;
    cout << "Enter [1] to Add New Item\n";
    cout << "Enter [2] to View All Items\n";
    cout << "Enter [3] to Remove an Item\n";
    cout << "Enter [4] to update book\n";
    cout << "Enter [5] to View Items with quantity < 5\n";
    cout << "Enter [6] to View Items Above Given Price\n";
    cout << "Enter [7] to View Books Above Average Price\n";
    cout << "Enter [8] to View Stock Statistics\n";
    cout << "Enter [0] to Exit\n";
    cout << " " << endl;

    cout << "Please enter your choice: ";     //input validation to add
    cin >> choice;

    return choice;
}

void addBook(int decision)
{
    int entry = 0, i = 0;

    if (decision == 1)
    {
        cout << "You have chosen choice [1]" << endl;

        outFile.open("bookList.txt");

        if (!outFile)
        {
            cout << "Unable to open the file.\n";
            exit(1);
        }
        else
        {
            do
            {
                //NEED TO ADD INPUT VALIDATION

                cout << "Please Enter the Author Name: ";
                cin.ignore();
                getline(cin, authorName[entry]);

                outFile << authorName[entry] << endl;
                cout << "Please Enter the Book Name: ";
                getline(cin, bookName[entry]);
                outFile << bookName[entry] << endl;

                cout << "Please Enter the Book ISBN: ";
                cin >> bookISBN[entry];
                outFile << bookISBN[entry] << endl;

                cout << "Please Enter the Price in QAR: ";
                cin >> bookPrice[entry];
                outFile << bookPrice[entry] << endl;

                cout << "Please Enter the Quantity: ";
                cin >> bookQuantity[entry];
                outFile << bookQuantity[entry] << endl;

                cout << "Data Entry Complete!" << endl;

                cout << "The Book \'" << bookName[entry] << "\' has been added a file" << endl;

                entry++;

                cout << "Do you want to enter more books?(Y/N)" << endl;
                cin >> RTM;
                /*

                while ((RTM != "y") || (RTM != "Y") || (RTM != "n") || (RTM != "N"));
                {
                    cout << "WRONG ENTRY! TRY AGAIN" << endl;                         // ADD IMPUT VALIDATION
                    cin >> RTM;
                }

                */
            } while (RTM == "y" || "Y"); //PROBLEM WITH LOOP

            outFile.close();
        }
    }
}

void viewAllItems(int decision)
{
    int count = 0;

    if (decision == 2)
    {
        cout << "You have chosen choice [2]" << endl;

        if (!inFile)
        {
            cout << "Unable to open the file.\n";
            exit(1);
        }

        else
        {
            while (count < ARRAY_SIZE && inFile >> bookNo[count])
            {
                count++;
            }
            //THE BOOKS ARE NOT BEING DISPLAYED
            inFile.open("bookList.txt");

            cout << "Reading data from the file" << endl;
            cout << " " << endl;
            cout << setw(45) << "Author Name" << setw(75) << "Book Name" << setw(25) << "Book ISBN" << setw(20) << "Book Price(QAR)" << setw(20) << "Book Quantity" << endl;

            for (int i = 0; i <= count; i++)
            {
                if (bookISBN[i] != 0) 
                {
                    inFile >> bookNo[i];
                    inFile >> authorName[i];
                    inFile >> bookName[i];
                    inFile >> bookISBN[i];
                    inFile >> bookPrice[i];
                    inFile >> bookQuantity[i];
                    cout << bookNo[i] << setw(45) << authorName[i] << setw(75) << bookName[i] << fixed << setprecision(0) << setw(25) << bookISBN[i] << setw(20) << bookPrice[i] << setw(20) << bookQuantity[i] << endl;
                }
            }
        }
        inFile.close();
    }
}

void removeItems(int decision)
{
    if (decision == 3)
    {

        /////////////





    }
}

void updateBook(int decision)
{

    if (decision == 4)
    {

        /////////////////




    }
}

void viewLess5(int decision)
{
    int count = 0;

    if (decision == 5)
    {
        while (count < ARRAY_SIZE && inFile >> bookNo[count])
        {
            count++;
        }

        cout << "You have chosen choice [5]" << endl;
        for (int i = 0; i <= count; i++)
        {
            if (bookQuantity[i] < 5)
            {
                cout << "The item with a quantity less than 5 is: " << bookName[i] << " by " << authorName[i] << " ,with a quantity of: " << bookQuantity[i] << endl;
            }
            else
            {

                cout << "There are no books whose quantity is less than 5" << endl;
                cout << " " << endl;
            }
        }
    }
}

void aboveGprice(int decision)
{
    int count = 0, givenPrice;

    if (decision == 6)
    {
        cout << "You have chosen choice [6]" << endl;
        
        while (count < ARRAY_SIZE && inFile >> bookNo[count])
        {
            count++;
        }
   
        cout << "Please Enter a Price and I will display all items above the given price" << endl;
        cin >> givenPrice;

        cout << "The items above the given price are: ";
        for (int i = 0; i <= count; i++)
        {
            if (bookPrice[i] > givenPrice)
            {
                cout << bookName[i] << " by " << authorName[i] << endl;
            }
            else {
                cout << "There are no items above the given"<<endl;
            }
        }
    }
}
Last edited on
This is the rest of my code:


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
void aboveAveragePrice(int decision)
{

    float AveragePrice = 0;
    int k = 0, count = 0;

    if (decision == 7)
    {
        cout << "You have chosen choice [7]" << endl;
        ;

        while (count < ARRAY_SIZE && inFile >> bookNo[count])
        {
            count++;
        }

        for (int i = 0; i <= count; i++)
        {
            AveragePrice += bookPrice[i];
        }


        cout << "The Average price is : " << AveragePrice / k << " QAR" << endl;

        for (int i = 0; i <= count; i++)
        {
            if (bookPrice[i] > AveragePrice / k)
            {
                cout << bookName[i] << " by " << authorName[i] << " costs " << bookPrice[i] << " QAR" << endl;
            }
        }

    }
}

void ViewStockStat(int decision)
{
    float AveragePrice = 0, totalstockvalue = 0;
    int k = 0, count = 0;

    if (decision == 8)
    {
        cout << "You have chosen choice [8]" << endl;

        while (count < ARRAY_SIZE && inFile >> bookNo[count])
        {
            count++;
        }

        for (int i = 0; i <= count; i++)
        {
            totalstockvalue += bookPrice[i] * bookQuantity[i];
        }

        cout << "The Total Stock Value is: " << totalstockvalue << " QAR" << endl;

        for (int i = 0; i <= count; i++)
        {
            AveragePrice += bookPrice[i];
        }

        cout << "The Average price is : " << AveragePrice / k << "QAR" << endl;

        int cheapest = 1000000, mostexpensive = bookPrice[0], leaststocked = 1000000;
        int cheapestcounter, mostexpensivecounter, leaststockedcounter;

        for (int i = 0; i <= count; i++)
        {
            if (cheapest >= bookPrice[i] && bookISBN[i] > 0) {
                cheapest = bookPrice[i];
                cheapestcounter = i;
            }

            if (mostexpensive <= bookPrice[i]) {
                mostexpensive = bookPrice[i];
                mostexpensivecounter = i;
            }

            if (leaststocked >= bookQuantity[i])
            {
                leaststocked = bookQuantity[i];
                leaststockedcounter = i;
            }
        }
        cout << "The cheapest book is " << bookName[cheapestcounter] << " by " << authorName[cheapestcounter] << " at price " << cheapest << " QAR" << endl;
        cout << "The most expensive book is " << bookName[mostexpensivecounter] << " by " << authorName[mostexpensivecounter] << " at price " << mostexpensive << " QAR" << endl;
        cout << "The least stocked item  " << bookName[leaststockedcounter] << " by " << authorName[leaststockedcounter] << " at quantity " << leaststocked << " QAR" << endl;
    }
}

void exitProgram(int decision)
{
    if (decision == 0)
    {
        exit(1);
    }

}

int main()
{
    choice = menu();

    addBook(choice);

    viewAllItems(choice);

    removeItems(choice);

    updateBook(choice);

    viewLess5(choice);

    aboveGprice(choice);

    aboveAveragePrice(choice);

    ViewStockStat(choice);

    exitProgram(choice);

    return 0;
}

/*
THINGS TO FIX:
1- ask if the user wants to go back to the main menu
2- input validation for choice 1, 5, 6
3- add the removeBook() and updateBook() functions.

*/
Last edited on
Hello, I worked all day today by starting the code from 0. Here is what I got. However, the file is still not opening and I don't know the reason.
PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/

HINT, you should edit your posts and add code tags.
Thank you, I will take it into consideration in my future posts.
Change your current posts as well. It helps us to help you, seriously.
Pages: 12