0

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!

Michael
  • 626
  • 5
  • 22
Fiery-Skyline
  • 67
  • 1
  • 4
  • 2
    You should never call `main` from any function in your program. – NathanOliver Jan 25 '16 at 15:51
  • 1
    The code you copy-pasted is a pretty bad mixture of C and C++ (and what's with the forward declarations of `int main()`)? The issue you're trying to understand is dynamically allocated arrays. Perhaps you could use a `std::vector` to simplify your life. – AndyG Jan 25 '16 at 15:53
  • Also see: [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – NathanOliver Jan 25 '16 at 15:53
  • 1
    There are many problems in this code. I advise you to **try something simpler** first. Write a program that reads the file and prints it to the screen, line by line. Once that works perfectly, write a program that reads the file field by field (title, author, genre); there's more than one way to do this, so save yourself some grief and use C++. Then set all that aside and learn dynamically allocated arrays. Then join the two pieces of code, and proceed in small steps. – Beta Jan 25 '16 at 16:46

1 Answers1

0

Instead of initialising total_books based on number_of_books when it's declared, you should probably set it after actually reading number_of_books. Variables, whether global or in a scope, don't update themselves dynamically. So, you could have something like this:

int number_of_books = 0;

void read_number_of_books() {
    // parse input file for value
    total_books = number_of_books * 3;
}

Is that what you're looking for?