I have to create a school library as an OOP assignment. I'm finding it very difficult to understand, my question here is:
int RANGE = total_books;
the total_books
should represent the current books in the text file.
The formatting makes it read 3 parts of info (title, author, genre). How can I point to this between functions?
I want to load the program and read the file in order to see how many there are currently (lets say 7 books, so the variable should be 7 * 3 = 21
). Then when the user views the file, it will display the 7 books.
Currently it's static: I have it set to 21. if I add another book it will only read the first 7 books. If I set it to 24 and there are 7 books (not 8 as needed) it will crash. I've tried looking on these forums and other places online, got the "C++ Programming in easy steps" book, it's where I got this formatting code but it's not very useful.
#include "stdio.h"
#include "malloc.h"
#include "stdlib.h"
#include "string.h"
#include "conio.h"
#include "fstream"
#include "iostream"
using namespace std;
unsigned int number_of_books = 0; //this is based on the number of books *3
int total_books = number_of_books * 3; //*3 to read 1 more book
class Book
{
private: // properties
char Title[16];
char Author[16];
char Genre[16];
public: // methods
int iDetailsGet(void);
int iDetailsShow(void);
int iRecordWrite(void);
};
int Book::iDetailsGet(void)
{
// prompt for the data
fflush(stdout);
puts("\n \t !USE_UNDERSCORE_FOR_SPACE!");
puts("\n \t Please enter the Book name: ");
fflush(stdin);
scanf("%s", Title, 16);
fflush(stdout);
puts("\n \t Please enter the Author: ");
fflush(stdin);
scanf("%s", Author, 16);
fflush(stdout);
puts("\n \t Please enter the Genre: ");
fflush(stdin);
scanf("%s", Genre, 16);
// Get total number of lines(books)
FILE *infile = fopen("books.txt", "r");
int ch;
while (EOF != (ch = getc(infile)))
if ('\n' == ch)
++number_of_books; // read from variable above but static.
printf("%u\n", number_of_books);
//return to menu
int main();
} // end method definition
int Book::iDetailsShow()
{
system("CLS");
int RANGE = total_books; // should be dynamically read on start up
string tab[RANGE];
int i = 0, j = 0;
ifstream reader("books.txt");
if(!reader)
{
cout << "Error Opening input file" << endl;
return -1;
}
while(!reader.eof())
{
if((i + 1) % 3 == 0) // the 3 read title,author,genre then adds new line
getline(reader, tab[i++], '\n');
else
getline(reader, tab[i++], '\t');
}
reader.close();
i = 0;
while (i < RANGE)
{
cout << endl << "Record Number: " << ++j << endl;
cout << "Title: " << tab[i++] << endl;
cout << "Author: " << tab[i++] << endl;
cout << "Genre: " << tab[i++] << endl;
}
int main();
} // end method definition
// code for the method: iRecordWrite(void)
int Book::iRecordWrite(void)
{
ofstream NewBook("books.txt", ios::app);
if (!NewBook)
{
printf("Error Recording Book");
return -1;
}
NewBook << " " << Title << " " << Author << " " << Genre << endl;
NewBook.close();
int main();
} // end of method deinition
Thanks!