1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
//Structure to hold the book information for an author
struct BookInfo
{
string title;
double price;
};
//Structure to hold the author and their book information
struct Author
{
string author;
BookInfo books[3];
};
//Function Prototypes
void showInfo(Author a[], int size);
void getInfo(Author a[], int size);
int main()
{
int index = 3;
//This is where I run into the problem, I cannot figure out how to initialize the whole array
Author catalogue[3] = {"NONE", {"NONE", 0},"NONE", {"NONE", 0}, "NONE", {"NONE", 0}};
cout << "Here is the data after initialization:\n";
showInfo(catalogue,index); //Displays the data in the author structure called catalogue
cout << "\nGet user's input:";
getInfo(catalogue,index); //Allows the user to enter data in the author structure called catalogue
cout << "\nHere is the data after the user's input:";
showInfo(catalogue,index); //Displays the data in the author structure called catalogue
system("pause");
return 0;
}
void showInfo(Author a[], int size) //Uses nested for loops to display the information contained in the catalogue
{
cout << fixed << showpoint << setprecision(2); //Sets the formatting used for doubles
for(int x = 0; x < size; x++)
{
cout << "The author: " << a[x].author << endl;
for(int y = 0; y < size; y++)
{
cout << "\tThe title: " << a[x].books[y].title << ", the price: $" << a[x].books[y].price << endl;
}
}
}
void getInfo(Author a[], int size) //Uses nested for loops to read data into the catalogue
{
for(int x = 0; x < size; x++)
{
cout << "\nEnter the author's name: ";
cin >> a[x].author;
if(a[x].author == "NONE")
break; //Breaks the loop if the author has no further books
else
for(int y = 0; y < size; y++)
{
cout << "Enter title " << y+1 << ": ";
cin >> a[x].books[y].title;
if(a[x].books[y].title == "NONE")
break;
else
cout << "Enter price " << y+1 << ": ";
cin >>a[x].books[y].price;
}
}
}
|