Hello. I have been working on a program that deals with array of structures. In this function, category report, i'm taking my structure and reading it through the a loop. Specifically, i'm reading category and price. In that loop there's a loop for the five categories. Basically, when it finds that category id number, it wants to write down all the publisher id's and prices for that category ID into an array. When it's searched for all the cases of category id 1 and recorded all the publisher and prices for it, it wants to send that info to printCategory (which is currently incomplete), reset, and move to category id 2..3... up to 5.

Yes, this is a homework assignment :P I have been at this for awhile, but i am stuck. I don't expect anyone to write it for me, but if i could get pointed in the right direction i'd appreciate it. Thank you.

struct Book{
   int issn;   //The book's issn number
   int publisher;  //The id of the book's publisher.
   int category;   //The id of the book's category.
   double price;  //The book's price.
}
book [max_books];

void categoryReport(Book[], int i)
{
	int num[max_books];
	double price[max_books];
    int x = 0;
	int j = 0;
	int n = 0;
	int c = 0;
	
	for( j = 0; j < i; j++)
	{
        while(book[j].category == 1)
            {
                memset(num, 0, i);
                memset(price, 0, i);
                num[x] = book[j].publisher;
                price[x] = book[j].price;
                j++;
                n++;
                c = 1;
                
            }
    

        while(book[j].category == 2)
            {
                memset(num, 0, i);
                memset(price, 0, i);
                num[x] = book[j].publisher;
                price[x] = book[j].price;
                j++;
                n++;
                c = 2;

            }
            
  
        while(book[j].category == 3)
            {
                memset(num, 0, i);
                memset(price, 0, i);
                num[x] = book[j].publisher;
                price[x] = book[j].price;
                j++;
                n++;
                c = 3;

            }
     
        while(book[j].category == 4)
            {
                memset(num, 0, i);
                memset(price, 0, i);
                num[x] = book[j].publisher;
                price[x] = book[j].price;
                j++;
                n++;
                c = 4;

            }
                    
        while(book[j].category == 5)
            {
                memset(num, 0, i);
                memset(price, 0, i);
                num[x] = book[j].publisher;
                price[x] = book[j].price;
                j++;
                n++;
                c = 5;
            }
            
    }
}

void printCategory(int c, int num[], double price[], int n)
{
    cout << setw(30) << "BOOK PRICE REPORT\n\n";
    cout << "CATEGORY\t";
    cout << "PUBLISHER\t";
    cout << "NUM OF BOOKS\t";
    cout << "TOTAL PRICE\t";
    cout << "AVERAGE PRICE\n";
    cout << "******************************************************************************\n";
    assignCategory(c);
    cout << "\n";
}

Recommended Answers

All 3 Replies

Line 9 - Am I correct that this doesn't compile?

I'd say you need to post a driver/main program for anyone to make any sense of these functions. It could make perfect sense, but not knowing what i represents and not knowing what the book[] array is initialized to be, it's really hard to judge. I see no point at all to your num[] and price[] arrays. You do nothing with them and they are local variables. The book[] array isn't changed at all. I don't see the purpose of the function. It doesn't seem to do anything since book[] isn't changed and printCategory isn't called, and even if it was, it doesn't really do anything but call a function that isn't posted. What exactly is the question?

Sorry, let me post the whole program:

struct Book{
   int issn;   //The book's issn number
   int publisher;  //The id of the book's publisher.
   int category;   //The id of the book's category.
   double price;  //The book's price.
}
book [max_books];


/*Function prototypes*/
   
void categoryReport(Book[], int);
void printCategory(int currentCategory, int num[], double price[], int n);
void bookpriceReport(Book[], int);
void printRange(double lowerLimit, double upperLimit, Book[], int n);
void sortBooks(int Book[], int i);
string assignPublisher(int);
string assignCategory(int);
int buildAR(Book[]);


/*int main*/
int main()
{
    int cat;
    int i; 
	i = buildAR(book);
	categoryReport(book,i);
	bookpriceReport(book, i);
	return 0;
}

int buildAR(Book[])
{
    /*Declare local variables*/
	int issn;
	int pub;
	int cat;
	double price;
	int i = 0;
	cin >> issn;
	/*While loop to fill structure arrays*/
	while(cin)
	{
		cin >> issn;
		cin >> pub;
		cin >> cat;
		cin >> price;
		book[i].issn = issn;
		book[i].publisher = pub;
		book[i].category = cat;
		book[i].price = price;
		
		i++;
	}
	/*returns the number of books*/
	return i;
}

void categoryReport(Book[], int i)
{
	int num[max_books];
	double price[max_books];
    int x = 0;
	int j = 0;
	int n = 0;
	int c = 0;
	
	for( j = 0; j < i; j++)
	{
        while(book[j].category == 1)
            {
                memset(num, 0, i);
                memset(price, 0, i);
                num[x] = book[j].publisher;
                price[x] = book[j].price;
                j++;
                n++;
                c = 1;
                
            }
    

        while(book[j].category == 2)
            {
                memset(num, 0, i);
                memset(price, 0, i);
                num[x] = book[j].publisher;
                price[x] = book[j].price;
                j++;
                n++;
                c = 2;

            }
            
  
        while(book[j].category == 3)
            {
                memset(num, 0, i);
                memset(price, 0, i);
                num[x] = book[j].publisher;
                price[x] = book[j].price;
                j++;
                n++;
                c = 3;

            }
     
        while(book[j].category == 4)
            {
                memset(num, 0, i);
                memset(price, 0, i);
                num[x] = book[j].publisher;
                price[x] = book[j].price;
                j++;
                n++;
                c = 4;

            }
                    
        while(book[j].category == 5)
            {
                memset(num, 0, i);
                memset(price, 0, i);
                num[x] = book[j].publisher;
                price[x] = book[j].price;
                j++;
                n++;
                c = 5;
            }
            
    }
}

void printCategory(int c, int num[], double price[], int n)
{
    cout << setw(30) << "BOOK PRICE REPORT\n\n";
    cout << "CATEGORY\t";
    cout << "PUBLISHER\t";
    cout << "NUM OF BOOKS\t";
    cout << "TOTAL PRICE\t";
    cout << "AVERAGE PRICE\n";
    cout << "******************************************************************************\n";
    assignCategory(c);
    cout << "\n";
}

string assignCategory(int cid) 
{
       string cName;
       /*switch statement*/
       switch(c) 
	   {
           case 1:
                cName = "Computer Science";
                cout << cName;
                break;
           case 2:
                cName = "Children's Book";
                cout << cName;
                break;
           case 3:
                cName = "Travel";
                cout << cName;
                break;
           case 4:
                cName = "English Literature";
                cout << cName;
                break;
           case 5:
                cName = "Philosophy";
                cout << cName;
                break;
       }
       return cName;

string assignPublisher(int pid) 
	{ 
       string pName;
       /*switch statement*/
       switch(pid) 
	   {
       case 1:
            pName = "Pearson\n";
            cout << pName;
            break;
       case 2:
            pName = "Random House\n";
             cout << pName;
            break;
       case 3:
            pName = "Harper Collins\n";
            cout << pName;
            break;
       case 4:
            pName = "O'Reilly\n";
            cout << pName;
            break;
       }
       return pName;
	}
}

It compiles fine, though.

No, it doesn't compile fine, even if I add the following lines to the top.

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

const int max_books = 10;

You have a brackets problem, you have at least one unidentified variable problem, and this compiles, but we have no way to break out of the input loop, or at least you don't tell us how to break out of the loop, nor do you tell us what we're supposed to enter:

while(cin)
	{
		cin >> issn;
		cin >> pub;
		cin >> cat;
		cin >> price;
		book[i].issn = issn;
		book[i].publisher = pub;
		book[i].category = cat;
		book[i].price = price;
		
		i++;
	}

Have you tried to compile or run this?

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.