This program starts out with authors and book titles initialized to NONE and prices initialized to $0. The user is then allowed to input all of the information and the program outputs it back at the end.
When I try to run the program I get this error message in lines 39, 42, and 45:
- error C2056: 'catalogue' : undeclared identifier
Can anybody tell me what I'm doing wrong? This is my code:
#include<iostream>
#include<string>
#include<iomanip>
usingnamespace std;
//Structure to hold the book information for an author
struct BookInfo
{
string title;
double price;
BookInfo() {
title = "NONE";
price = 0;
}
};
//Structure to hold the author and their book information
struct Author
{
string author;
BookInfo books[3];
Author() {
author = "NONE";
}
};
//Function Prototypes
void showInfo(Author a[], int size);
void getInfo(Author a[], int size);
int main()
{
int index = 3;
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
elsefor(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;
}
}
}
As far as I can tell it turns into an array of Author structs, so I'd probably initialise it as a string and set the value to something inane as a test to see if it causes problems.
string catalogue = "Test";
Usually I find the initialisation value doesn't matter that much as it just becomes the name of the array it creates after you pass it to the function.
int main()
{
int index = 3; // if this should be the size of the array make it const
Author catalogue[100]; // No need for initialization since the constructor of Author does it already
cout << "Here is the data after initialization:\n";
showInfo(catalogue,index); //Displays the data in the author structure called catalogue
...
system("pause");
return 0;
}