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.
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 (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;
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.
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
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;
}
}
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.