I need to read from a txt file and then pass that array into another function which prints the data in a neat organised manner depending which type pf book it is...
So if its audiobook then the book is listed under audiobook, if ebook then under ebook etc...

I get lots of errors and my array isn't being passed to the function so i am getting very confused.
This is my code thus far:

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
class Product
{
    string title;
    string surname;
    string isbn;
    double wholesalePrice;
//  
public:
    string getTitle();
    string getSurName();
    string getIsbn();
    double getWholePrice();
    void setInfo(string,string,string,double);
};

string Product::getTitle(){
    return title;
}

string Product::getSurName(){
    return surname;
}

string Product::getIsbn(){
    return isbn;
}

double Product::getWholePrice(){
    return wholesalePrice;
}

void Product::setInfo(string t,string sN,string ibn,double price)
{
    title = t;
    surname = sN;
    isbn = ibn;
    wholesalePrice = price;
}

class Stock:public Product{
public:
    double retailPrice;
    char bookFormat;
    int stockLevel;

   /* Stock();
    ~Stock() {}*/
    double getRetail(double wholesalePrice, char bookFormat);
    void orders(Stock *);
    void report(Stock *);
};


double Stock::getRetail(double wholesale, char bF)
{
    double retailPrice = 0;
    switch (bF)
    {
    case 'a':
        retailPrice = wholesale + (0.4*wholesale);
        break;
    case 'e':
        retailPrice = wholesale + (0.1*wholesale);
        break;
    case 'h':
        retailPrice = wholesale + (0.45*wholesale);
        break;
    case 's':
        retailPrice = wholesale + (0.4*wholesale);
        break;
    }
    return retailPrice;
}
void Stock::report(Stock[])
{
    double retailPrice = 0;
    ofstream oFile("reports.txt");
    if(bookFormat == 'a')
    {
        retailPrice = getRetail(getWholePrice(), 'a');
        oFile << "AudioBooks :" << "/n";
        oFile << "##########################################################################" ;
        oFile << "ISBN" << setw(20) << "Title" << setw(20) << "Retail Cost" << setw(20)<< "Quantity in Stock\n";
        oFile << getIsbn() << setw(20) << getTitle() << setw(20) << retailPrice << setw(20) << stockLevel;
    }
    if (bookFormat == 'e')
    {
        retailPrice = getRetail(getWholePrice(), 'e');
        oFile << "AudioBooks :" << "/n";
        oFile << "##########################################################################";
        oFile << "ISBN" << setw(20) << "Title" << setw(20) << "Retail Cost" << setw(20) << "Quantity in Stock\n";
        oFile << getIsbn() << setw(20) << getTitle() << setw(20) << retailPrice << setw(20) << stockLevel;
    }
    if (bookFormat == 'h')
    {
        retailPrice = getRetail(getWholePrice(), 'h');
        oFile << "AudioBooks :" << "/n";
        oFile << "##########################################################################";
        oFile << "ISBN" << setw(20) << "Title" << setw(20) << "Retail Cost" << setw(20) << "Quantity in Stock\n";
        oFile << getIsbn() << setw(20) << getTitle() << setw(20) << retailPrice << setw(20) << stockLevel;
    }
    if (bookFormat == 's')
    {
        retailPrice = getRetail(getWholePrice(), 's');
        oFile << "AudioBooks :" << "/n";
        oFile << "##########################################################################";
        oFile << "ISBN" << setw(20) << "Title" << setw(20) << "Retail Cost" << setw(20) << "Quantity in Stock\n";
        oFile << getIsbn() << setw(20) << getTitle() << setw(20) << retailPrice << setw(20) << stockLevel;
    }
    oFile.close();
}

//void Stock::orders(Stock[])
//{
//  ofstream outFile;
//  outFile.open("orders.txt");
//
//  
//      if(Stock.bookFormat == 'a'){
//          outFile << "AudioBooks :" << "/n";
//          outFile << "##################################################################/n" ;
//          outFile << 
//
//cout << "#############################################################################/n";
//  cout << " 
//  }
//}

int main() {


    string title, sName,isbn;
    double wPrice;
    int stock;
    char bFormat;

    Stock Books[20];


    fstream inFile("products.txt");
    if (!inFile)
    {
        cout << "\ninFile: File not found.\n";
        return 1;
    }

    for(int i = 0; i < 20; i++)
    {
        inFile >> title >> sName >>  bFormat >> isbn >> wPrice >> stock;
        Books[i].setInfo(title,sName,isbn, wPrice);
        Books[i].bookFormat = bFormat;
        Books[i].stockLevel = stock;    
    }
    for (int i = 0; i < 20; i++)
    {
        Books[i].report(Books);
    }

    return 0;
}

Are these compiler, or are they runtime errors? Please show them. As for passing an array into a function, either the function already knows the size of the array, or you need to pass the size in another argument. Better is to use a C++ vector<type> class, and you can determine the size directly at runtime. As opposed to a C++ array<type> class, it can be resized as necessary if you add/remove elements.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.