Can anybody tell me why I'm getting these errors?

closed account (3UMLy60M)
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:

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
#include<iostream>
#include<string>
#include<iomanip>
using namespace 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
			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;
			}
	}
}
You have not declared catalogue anywhere.
You are using a variable named 'catalogue' which hasn't been declared. The error message was kindof a hint!!
closed account (3UMLy60M)
I have to declare it along with index right? And what would I have to initialize it to?
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.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
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;
}
Topic archived. No new replies allowed.