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;
}
/