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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
#include <iostream>
#include <string>
#include <cstring>
#include <iomanip>
#include <cstdlib>
using namespace std;
struct BookInfo
{
string title; //title of book
double price; //price of book
};
struct Author
{
string name; //author of books
BookInfo b; //array to hold info on books author has written
}a[];
void showInfo(Author [], int);
void getInfo(Author [], int);
int main()
{
//variable
const int SIZE = 3; //the size of the arrays
//Array
Author a[SIZE] = {
{"NONE", {"NONE", 0}},
{"NONE", {"NONE", 0}},
{"NONE", {"NONE", 0}}
};
//Output
cout << "Here is the data after initialization" << endl;
showInfo(a, SIZE);
cout << endl;
//User Input
cout << "Get user's input" << endl;
getInfo(a, SIZE);
cout << endl;
//Shows the array when filled with information from user
cout << "Here is the data after user's input" << endl;
showInfo(a, SIZE);
cout << endl;
system("pause");
return 0;
}
//shows the information stored in a[]
void showInfo(Author a[], int size)
{
//displays info for each author
for (int count1 = 0; count1 < size; count1++)
{
cout << "The author: " << a[count1].name << endl;
//displays info for each book that the author has
for (int count2 = 0; count2 < size; count2++)
{
cout << "\tThe title: " << a[count2].b.title << ", the price: $" << a[count2].b.price << endl;
}
}
}
//gets array information from user
void getInfo(Author a[], int size)
{
//loop to get author name
for (int count1 = 0; count1 < size; count1++)
{
//gets author name from user
cout << "Enter the author's name: ";
getline(cin, a[count1].name);
//loop to get the title and of the 3 books from user
for (int count2 = 0; count2 < size; count2++)
{
cout << "Enter title "<< (count2 + 1) << " :";
getline(cin, a[count2].b.title);
//if user enters "NONE" then it kicks out into next Author
if(a[count2].b.title != "NONE")
{
cout << "Enter price " << (count2 + 1) << " : $";
cin >> a[count2].b.price;
cin.ignore();
}else{
break;
}
}
cout << endl;
}
cout << endl;
}
|