Yo guys! How's it going?

To give you a quick gist of my goals : I want to allow the user to enter a bunch of criteria for a book (title, author, etc) so I made a structure. Then I store that structure in an array so they can enter a bunch of books. Then I want to give them the option to sort that array (all their books) by a certain criteria like clicking album on itunes. Finally, I want to give them the option to see the average and total cost of their books.

Needless to say I am having a little trouble here - mainly with the last function that finds the average cost and total cost which is essentially garbage at this point lol so any help there is great! Also, the output functions don't seem to work right, it is outputting outrageous numbers. I think the Printdata one is okay because it is so simple, but maybe printer2 is causing the problem, which calls upon Printdata.

Anyway any help on any part of this code would be so awesome! I have researched a lot to get even this far and my book doesn't go into detail about the combination of these ideas like the assignment requires.

So thank you, peace and happiness!

Kevin

PS I will try and get on here as often as possible to update my code so we can hopefully work towards a workable solution! I attached the assignment as well

/

/* Program will keep track of ebooks and sort them according to a list of criteria including: title, author, publisher, copyight year, number of pages and cost.
   Input: Title, author, publisher, copyight year, number of pages and cost for a single variable of the structure
   Output: Title, author, publisher, copyight year, number of pages and cost which are stored in an array. Output the sorted array by either author, title, cost, or number of pages.
           Output total cost and average cost.
   Processing: Call function that asks user for all fields for a single variable of the structure data type and return a structure
               Call function to fill an array of the structure type. Employ a loop to allow user to enter values  until array is filled
			   Call function that accepts structure array and outputs it with a for loop that calls the function to output all fields of a single structure variable
			   Depending on scenario:
			   1) Call function to sort by ascending order the author using bubble sort
			   2) Call function to sort by ascending order the title using selection sort
			   3) Call function to sort by descending order the cost using insertion sort
			   4) Call function to sort by descending order the number of pages
			   Call function to calculate total cost and average cost using loops
*/

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

struct Book
{
	string title;
	string author;
	string publisher;
	int year;
	int pages;
	double cost;
};

Book getitems (void);
int filler (Book h[], int s);
void Printdata(Book element);
void printer2(Book g[], int total);
void AuthorSort (Book a[],int size);
void TitleSort (Book a[],int size);
void CostSort (Book a[],int size);
void PageSort (Book a[],int size);
double Getaverage(double daArray[], int iSize);

int main ()
{
	char choice;
	const int sz = 50;
	Book info[sz];

	filler (info,sz);
	printer2(info, sz);

	cout<<"Would you like to:\n";
	cout<<"A:Sort the books ascending by author?\n";
	cout<<"B:Sort the books ascending by title?\n";
	cout<<"C:Sort the books descending by cost?\n";
	cout<<"D:Sort the books descending by number of pages?\n";
	cin>>choice;

	switch (choice)
	{
	case 'A':
	AuthorSort (info, sz);
	case 'B':
	TitleSort (info, sz);
	case 'C':
	CostSort (info, sz);
	case 'D':
	PageSort (info, sz)	;
	case 'E':
	Getaverage(info, sz);

	}
	printer2(info, sz);

	return 0;
}

/* Function that asks user for all fields for a single variable of the structure data type and return a structure
   input: title, author, publisher, copyight year, number of pages and cost
   output: variable of data structure type
   processing: prompt user to enter values for structure data
*/

Book getitems (void)
{
	Book p;
	
	cout<<"Enter title of book"<<endl;
	cin.ignore() ;
	getline(cin,p.title,'\n');

	cout<<"Enter author's name"<<endl;
	getline(cin,p.author,'\n');;

	cout<<"Enter publisher's name"<<endl;
	getline(cin,p.publisher,'\n');

	cout<<"Enter copyright year"<<endl;
	cin>> p.year;

	cout<< "Enter number of pages in book"<<endl;
	cin >> p.pages;

	cout<< "Enter cost of the book"<<endl;
	cin >> p.cost;
	return p;

}


/* Function to fill an array of the structure type. Employ a loop to allow user to enter values  until array is filled
   input: data of structure type from other function and input yes or no
   output: fills array with structure information
   processing: use while loop to prompt, call function to fill and repeat
*/

int filler (Book single[], int s)
{
	int i=0;
	char ans[4];
	char ans1[]="no";
	char ans2[]="n";
	char ans3[]="No";
	char ans4[]="N";

	do 
	{
		single[i]=getitems();
		i++;
		cout<<"Would you like to enter another book?(y/n)";
		cin>>ans;
	}
	while (i < s & (strcmp (ans1,ans) != 0) & (strcmp (ans2,ans) != 0) & (strcmp (ans3,ans)!=0) & (strcmp (ans4,ans)!=0 ));
	
		return i;
}

/* Function prints each data value of the structure
   input: single variable of structure
   output: each portion of the structure : title, author, publisher, year, pages, cost
   processing:cout each element individually using a single variable element
*/

void Printdata(Book element)
{
	cout<<element.title;
	cout<<element.author;
	cout<<element.publisher;
	cout<<element.year;
	cout<<element.pages;
	cout<<element.cost;		
}

/* Function to output elements of the array by calling print function
   input: array of structure type and number of elements stored
   output: the title, author, publisher, year, pages, and cost of all books
   processing: loop will call the function that outputs all the fields of a single structure variable until all elements of array are output
*/

void printer2(Book g[], int total)
{
	for (int i=0; i<total; i++)
	{
		Printdata (g[i]);
	}
}

/* Function to sort by ascending order the author using bubble sort
   input: array of structure data and size
   output: none
   processing: sort array by author by swapping entire variables using bubble sort
*/
void AuthorSort (Book a[],int size)
{
	int i;
	int last = size - 2;
	int first = 0, pass = 0;
    Book temp;
	bool sorted = false;

	while (!sorted)
	{
		sorted = true;
		for (i = first; i <= last; i++)

		{	if (a[i].author > a[i+1].author)
			{
				temp = a[i];
				a[i] = a[i + 1];
				a[i + 1] = temp;
				sorted = false;
			}
		}
		last = last -1;
		pass++;
	}
}
/* Function to sort by ascending order the title using insertion sort
   input: array of structure data and size
   output: none
   processing: sort array by title by swapping entire variables using insertion sort
*/
void TitleSort (Book a[],int size)
{
	int i;     
	int last = size - 2;
	int marker;
    Book temp;
	bool sorted = true;

	for (i = 0; i<= last; i++)
	{
		if (a[i].title > a [i + 1].title)
		{
			sorted = false;
			marker = i;
			while (!sorted)
			{
				temp = a[marker];
				a[marker] =  a [marker + 1];
				a[marker + 1] = temp;

				if (marker == 0)
					sorted = true;
				else if (a[marker].title > a[marker - 1].title)
					sorted = true;
				else
					marker = marker - 1;
			}
		}
	}
}

/* Function to sort by descending order the cost using selection sort
   input: array of structure data and size
   output: none
   processing: sort array by cost by swapping entire variables using selection sort
*/

void CostSort (Book a[],int size)
{
	int passcount;
	int placecount;
	int minIndex;   
    Book temp;

	for (passcount=0; passcount<size-1; passcount++)
	{
		minIndex = passcount;
		for (placecount = passcount + 1; placecount < size; placecount++)
		{
			if (a[placecount].cost > a[minIndex].cost)  
				minIndex = placecount;
		}

        temp = a[minIndex];
		a[minIndex] = a[passcount];
		a[passcount] = temp;
	}
}

/* Function to sort by descending order the page number using selection sort
   input: array of structure data and size
   output: none
   processing: sort array by pages by swapping entire variables using selection sort
*/

void PageSort (Book a[],int size)
{
	int passcount;
	int placecount;
	int minIndex;   
    Book temp;

	for (passcount=0; passcount<size-1; passcount++)
	{
		minIndex = passcount;
		for (placecount = passcount + 1; placecount < size; placecount++)
		{
			if (a[placecount].pages > a[minIndex].pages)  
				minIndex = placecount;
		}

        temp = a[minIndex];
		a[minIndex] = a[passcount];
		a[passcount] = temp;
	}
}

/* Function to calculate total cost and average cost using loops
   input: array of structure and size
   output: return total cost and average cost
   processing:
*/

Book Getaverage(Book daArray[], int iSize)
{
	Book dSum = daArray[0];
	Book result = 0;

	for (int i = 1; i < iSize; ++i)
			{
				dSum += daArray[i];
			}

	for(int i = 0; i < iSize; ++i)
			{
				result = result + daArray[i];
			}

return dSum;
return result;

}

/

Post your code inside the code tags: [code] /* CODE HERE */ [/code]

Also, please preserve the formatting (posting in the code tags will do that). You will need to copy it from it's original source again.


Here's a tip for your "filler" function though,
read a line of input at a time and extract (parse) what you need from it.
The reason to do so is to avoid causing "cin" to fail and have junk left in the input buffer because the process of clearing the buffer and ignoring the garbage is different from platform to platform.
It's much easier to do this:

string inputLine;
getline(cin,inputLine);
if(inputLine.size() == 0)
   return;
char answer = inputLine[0];

Also there are useful character functions in <cctype> like "toupper" and "tolower".

string inputLine;
getline(cin,inputLine);
if(inputLine.size() == 0)
   return;
char answer = toupper(inputLine[0]);
if( answer != 'Y' )
   return;

Thanks. Its kinda specific what I can use though. But does this make sense the way i wrote it?
I feel like i need something more there idk why.

single=getitems();

Post your code inside the code tags: [code] /* CODE HERE */ [/code]

Also, please preserve the formatting (posting in the code tags will do that). You will need to copy it from it's original source again.


Here's a tip for your "filler" function though,
read a line of input at a time and extract (parse) what you need from it.
The reason to do so is to avoid causing "cin" to fail and have junk left in the input buffer because the process of clearing the buffer and ignoring the garbage is different from platform to platform.
It's much easier to do this:

string inputLine;
getline(cin,inputLine);
if(inputLine.size() == 0)
   return;
char answer = inputLine[0];

Also there are useful character functions in <cctype> like "toupper" and "tolower".

string inputLine;
getline(cin,inputLine);
if(inputLine.size() == 0)
   return;
char answer = toupper(inputLine[0]);
if( answer != 'Y' )
   return;

I think it would make more sense as a for-loop.
Also the creation of a function to determine whether it's a yes or no would probably simplify things.

Sadly since you've said you can only use certain features of the language then your answer will probably suck.

Perhaps this will help:

bool GetAnswerResult(string answer)
{
	//if you must list every acceptable answer
	//why not use const strings?
	const string falseOne = "n";
	const string falseTwo = "N";
	const string falseThree = "No";
	const string falseFour = "NO";
	const string falseFive = "nO";
	//Work your magic here.
}

int filler (Book single[], const int s)
{
	string ans;

	for(int i = 0; i < s; i++)
	{
		single[i]=getitems();
		cout<<"Would you like to enter another book?(y/n)";
		cin >> ans;
		if( !GetAnswerResult(ans) )
			break;
	}
	return i;
}

Also, your naming system seems a bit flakey.
Pick a style and stick with it until you know better.

I still say use the first character of the y/n answer to determine the result.

Here is my updated code - working other than the sort seems to output only the criteria sorted by

/

/* Program will keep track of ebooks and sort them according to a list of criteria including: title, author, publisher, copyight year, number of pages and cost.
   Input: Title, author, publisher, copyight year, number of pages and cost for a single variable of the structure
   Output: Title, author, publisher, copyight year, number of pages and cost which are stored in an array. Output the sorted array by either author, title, cost, or number of pages.
           Output total cost and average cost.
   Processing: Call function that asks user for all fields for a single variable of the structure data type and return a structure
               Call function to fill an array of the structure type. Employ a loop to allow user to enter values  until array is filled
			   Call function that accepts structure array and outputs it with a for loop that calls the function to output all fields of a single structure variable
			   Depending on scenario:
			   1) Call function to sort by ascending order the author using bubble sort
			   2) Call function to sort by ascending order the title using selection sort
			   3) Call function to sort by descending order the cost using insertion sort
			   4) Call function to sort by descending order the number of pages
			   Call function to calculate total cost and average cost using loops
*/

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

struct Book
{
	string title;
	string author;
	string publisher;
	int year;
	int pages;
	double cost;
};

Book getitems (void);
int filler (Book h[], int s);
void Printdata(Book element);
void printer2(Book g[], int total);
void AuthorSort (Book a[],int size);
void TitleSort (Book a[],int size);
void CostSort (Book a[],int size);
void PageSort (Book a[],int size);
double Getaverage(double daArray[], int iSize);

int main ()
{
	char choice;
	const int sz = 50;
	int size;
	Book info[sz];

	size = filler (info,sz);
	printer2(info, size);

	cout<<"Would you like to:\n";
	cout<<"A:Sort the books ascending by author?\n";
	cout<<"B:Sort the books ascending by title?\n";
	cout<<"C:Sort the books descending by cost?\n";
	cout<<"D:Sort the books descending by number of pages?\n";
	cin>>choice;

	switch (choice)
	{
	case 'A':
	AuthorSort (info, sz);
	case 'B':
	TitleSort (info, sz);
	case 'C':
	CostSort (info, sz);
	case 'D':
	PageSort (info, sz)	;

	}
	printer2(info, size);

	return 0;
}

/* Function that asks user for all fields for a single variable of the structure data type and return a structure
   input: title, author, publisher, copyight year, number of pages and cost
   output: variable of data structure type
   processing: prompt user to enter values for structure data
*/

Book getitems (void)
{
	Book p;
	
	cout<<"Enter title of book"<<endl;
	cin.ignore() ;
	getline(cin,p.title,'\n');

	cout<<"Enter author's name"<<endl;
	getline(cin,p.author,'\n');;

	cout<<"Enter publisher's name"<<endl;
	getline(cin,p.publisher,'\n');

	cout<<"Enter copyright year"<<endl;
	cin>> p.year;

	cout<< "Enter number of pages in book"<<endl;
	cin >> p.pages;

	cout<< "Enter cost of the book"<<endl;
	cin >> p.cost;
	return p;

}


/* Function to fill an array of the structure type. Employ a loop to allow user to enter values  until array is filled
   input: data of structure type from other function and input yes or no
   output: fills array with structure information
   processing: use while loop to prompt, call function to fill and repeat
*/

int filler (Book single[], int s)
{
	int i=0;
	char ans;
	do 
	{
		single[i]=getitems();
		i++;
		cout<<endl;
		cout<<"Would you like to enter another book?(y/n)";
		cin>>ans;
	}
	while ((i < s) & (ans != 'no') & (ans != 'No') & (ans != 'n')& (ans != 'N'));
	
		return i;
}

/* Function prints each data value of the structure
   input: single variable of structure
   output: each portion of the structure : title, author, publisher, year, pages, cost
   processing:cout each element individually using a single variable element
*/

void Printdata(Book element)
{
	cout<<element.title;
	cout<<element.author;
	cout<<element.publisher;
	cout<<element.year;
	cout<<element.pages;
	cout<<element.cost;		
	cout<<endl;
}

/* Function to output elements of the array by calling print function
   input: array of structure type and number of elements stored
   output: the title, author, publisher, year, pages, and cost of all books
   processing: loop will call the function that outputs all the fields of a single structure variable until all elements of array are output
*/

void printer2(Book g[], int total)
{
	for (int i=0; i<total; i++)
	{
		Printdata (g[i]);
	}
}

/* Function to sort by ascending order the author using bubble sort
   input: array of structure data and size
   output: none
   processing: sort array by author by swapping entire variables using bubble sort
*/
void AuthorSort (Book a[],int size)
{
	int i;
	int last = size - 2;
	int first = 0, pass = 0;
    Book temp;
	bool sorted = false;

	while (!sorted)
	{
		sorted = true;
		for (i = first; i <= last; i++)

		{	if (a[i].author > a[i+1].author)
			{
				temp = a[i];
				a[i] = a[i + 1];
				a[i + 1] = temp;
				sorted = false;
			}
		}
		last = last -1;
		pass++;
	}
}
/* Function to sort by ascending order the title using insertion sort
   input: array of structure data and size
   output: none
   processing: sort array by title by swapping entire variables using insertion sort
*/
void TitleSort (Book a[],int size)
{
	int i;     
	int last = size - 2;
	int marker;
    Book temp;
	bool sorted = true;

	for (i = 0; i<= last; i++)
	{
		if (a[i].title > a [i + 1].title)
		{
			sorted = false;
			marker = i;
			while (!sorted)
			{
				temp = a[marker];
				a[marker] =  a [marker + 1];
				a[marker + 1] = temp;

				if (marker == 0)
					sorted = true;
				else if (a[marker].title > a[marker - 1].title)
					sorted = true;
				else
					marker = marker - 1;
			}
		}
	}
}

/* Function to sort by descending order the cost using selection sort
   input: array of structure data and size
   output: none
   processing: sort array by cost by swapping entire variables using selection sort
*/

void CostSort (Book a[],int size)
{
	int passcount;
	int placecount;
	int minIndex;   
    Book temp;

	for (passcount=0; passcount<size-1; passcount++)
	{
		minIndex = passcount;
		for (placecount = passcount + 1; placecount < size; placecount++)
		{
			if (a[placecount].cost > a[minIndex].cost)  
				minIndex = placecount;
		}

        temp = a[minIndex];
		a[minIndex] = a[passcount];
		a[passcount] = temp;
	}
}

/* Function to sort by descending order the page number using selection sort
   input: array of structure data and size
   output: none
   processing: sort array by pages by swapping entire variables using selection sort
*/

void PageSort (Book a[],int size)
{
	int passcount;
	int placecount;
	int minIndex;   
    Book temp;

	for (passcount=0; passcount<size-1; passcount++)
	{
		minIndex = passcount;
		for (placecount = passcount + 1; placecount < size; placecount++)
		{
			if (a[placecount].pages > a[minIndex].pages)  
				minIndex = placecount;
		}

        temp = a[minIndex];
		a[minIndex] = a[passcount];
		a[passcount] = temp;
	}
}

/* Function to calculate total cost and average cost using loops
   input: array of structure and size
   output: return total cost and average cost
   processing:

Book Getaverage(Book daArray[], int iSize)
{
	Book dSum = daArray[0];
	Book result = 0;

	for (int i = 1; i < iSize; ++i)
			{
				dSum += daArray[i];
			}

	for(int i = 0; i < iSize; ++i)
			{
				result = result + daArray[i];
			}

return dSum;
return result;

}
*/

So the new issue (aside from the fact that I don't know how to find average and total cost) is that after I sort the books by title, author, etc...it only outputs the title author etc. where I want it to still output the entire structure

here is what i have for avg / total - how can i make this legal? right now i cant return avg/sum...

Book AverageTotal (Book index[],int s)
{
   int i;
   int sum;
   int avg;

   for (i=0; i<s; i++)
	   {
		 sum = sum + index[i].cost;
	   }
   avg = sum / i+1;

   return avg/sum;
}

here is what i have for avg / total - how can i make this legal? right now i cant return avg/sum...

Book AverageTotal (Book index[],int s)
{
   int i;
   int sum;
   int avg;

   for (i=0; i<s; i++)
	   {
		 sum = sum + index[i].cost;
	   }
   avg = sum / i+1;

   return avg/sum;
}

I find it hard to believe you can't solve that problem.

double sum = 0.0;
for(int i = 0; i < s; i++)
{
   sum += index[i].cost;
}
return sum / s;

Now, what is the advantage to returning a "book" structure instead of a double?

Also I overlooked your use of the bitwise-AND operator (&) while ((i < s) & (ans != 'no') & (ans != 'No') & (ans != 'n')& (ans != 'N')); Please see my solution in an above post, or use "&&" (the logical-AND).

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.