calling a function that holds struct arrays

In this program I am attempting to allow a user to input three different authors and then input three books they have written as well as the price. I am struggling with calling the functions and am not sure what to do.

Any help would be greatly appreciated. Thanks!

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
#include <iostream>
#include <string>
using namespace std;


struct BookInfo{
	string bookTitle;
	double price;
};

struct Author{
	string authorName;
	BookInfo b[3];
};
//Above are the two structs I am using

//Function prototypes
void showInfo(Author a[], int size);
void getInfo(Author a[], int size);

int main(){
	Author a[3];
	a[0].authorName = "None"; a[0].b[0].bookTitle = "None"; a[0].b[0].price = 0;
	a[1].authorName = "None"; a[1].b[1].bookTitle = "None"; a[1].b[1].price = 0;
	a[2].authorName = "None"; a[2].b[2].bookTitle = "None"; a[2].b[2].price = 0;
	//Problem comes here when I attempt to call the functions
	cout << showInfo(Author a[], size); 
	cout << getInfo(Author a[], size);
	cout << showInfo(Author a[], size);

	system("Pause");
	return 0;
}

void showInfo(Author a[], int size){ //Purpose is to show the information stored
	cout << "Author Name: " << a[0].authorName;
	cout << "Title: " << a[0].b[0].bookTitle << " Price: $" << a[0].b[0].price << "\n";

	cout << "Author Name: " << a[1].authorName;
	cout << "Title: " << a[1].b[1].bookTitle << " Price: $" << a[1].b[1].price << "\n";

	cout << "Author Name: " << a[2].authorName;
	cout << "Title: " << a[2].b[2].bookTitle << " Price: $" << a[2].b[2].price << "\n";
}

void getInfo(Author a[], int size){ 
	/*Purpose is to get information for the two arrays
Also I would like it so if "NONE" is entered for a book title the loop will not ask for the next title
and move on to the next author.
I feel as if I should not be using a for loop here to achieve my goal */
	size = 3;
	Author a[3];
	for(int i=0; i < size; i++){
		int t=1; //Used for title number
		int p=1; //Used for price number

		cout << "Please enter the name of the author: ";
		cin >> a[i].authorName; cout << "\n";

		cout << "Enter Title " << t << ": ";
		cin >> a[i].b[0].bookTitle; cout << "\n";
		cout << "Enter Price " << p << ": $";
		cin >> a[i].b[0].price; cout << "\n";

		++t; ++p;

		cout << "Enter Title " << t << ": ";
		cin >> a[i].b[1].bookTitle; cout << "\n";
		cout << "Enter Price " << p << ": $";
		cin >> a[i].b[1].price; cout << "\n";

		++t; ++p;

		cout << "Enter Title " << t << ": ";
		cin >> a[i].b[2].bookTitle; cout << "\n";
		cout << "Enter Price " << p << ": $";
		cin >> a[i].b[2].price; cout << "\n";
		
		t = 1; p = 1; //Reset for next iteration of loop
	}}


Once again, any help would be greatly appreciated!
Last edited on
1. Functions are not called like this.

1
2
3
	cout << showInfo(Author a[], size); 
	cout << getInfo(Author a[], size);
	cout << showInfo(Author a[], size);


change it to

1
2
3
	cout << showInfo(a, size); 
	cout << getInfo(a, size);
	cout << showInfo(a, size);

This still have problem. These functions do not return anything (return void), so you cannot cout << void;
So, modify it to:
1
2
3
	cout << showInfo(a, size); 
	cout << getInfo(a, size);
	cout << showInfo(a, size);




2. You've to tell the compiler what is size, so write int size = 3; before these statements




3.
1
2
3
void getInfo(Author a[], int size){ 
	size = 3;
	Author a[3];

You are again defining a[3] where a has been declared in main and is now in this function since 'a' has been passed.

I've removed all syntax Errors. I won't tell any logic, because i'm busy now-a-days.
Thank you very much! I knew it would end up being something simple like that. I'm still somewhat new to c++ so I figure the logic errors will work themselves out through experience so don't worry about that. Once again thank you!
You're welcome ngeisler. That's good to figure out logical errors yourself.
Topic archived. No new replies allowed.