Need help with outout of a function

Well I'm working on some Code, and i have most of it done for myself, but when i run the getInfo Function it just keeps writing over my last iteration. I know the solution must be simple but its getting me so aggravated. I've been looking at it for hours and cant seem to spot the problem. So I'm breaking down and asking for any help you guys can offer.


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;
}



Sorry if that was a lot but i don't know what parts people might need to help.
All of my problems seem to be when it calls getInfo it overwrites that last bit of data that was input and in the end just displays the same books and prices for all 3 authors.

PS. if you didn't know i am a beginner.

Thanks in advace for any help you can give me.
Last edited on
You cannot use a[count1] and a[count2]. it's supposed to be a[count1].b[count2].title. Therefore line 19:

BookInfo b[...]; // your really need and array! //array to hold info on books author has written


Ok so if i make Line 19:

BookInfo b[3]

and i change showInfo line 70 to:

cout << "\tThe title: " << a[count1].b[count2].title << ", the price: $" << a[count1].b[count2].price << endl;

it doesn't do the showInfo output correctly, but fixes everything else. so how would i fix the array in lines 33-37 to refelect the new BookInfo b[3] array i made?
Last edited on
Take line 34 as an example:
{"A1", { {"B1", 1}, {"B2", 2}, {"B3", 3} } },

Btw: for getInfo() and showInfo() you provide just int size which means that the number of authors and the number of books is always the same. i.e. if you have 20 authors then they wrote 20 books etc.
So i now have the first iteration of the showInfo works but for the 2nd and 3rd Author it doesnt. Here is the data after initialization

The author: NONE
        The title: NONE, the price: $0
        The title: NONE, the price: $0
        The title: NONE, the price: $0
The author: NONE
        The title: , the price: $-9.25596e+061
        The title: , the price: $-9.25596e+061
        The title: , the price: $-9.25596e+061
The author: NONE
        The title: , the price: $-9.25596e+061
        The title: , the price: $-9.25596e+061
        The title: , the price: $-9.25596e+061



Lines 16-46

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
//global variable
const int SIZE = 3;

struct BookInfo
{
	string title;		//title of book
	double price;		//price of book
};

struct Author
{
	string name;			//author of books
	BookInfo b[SIZE];				//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}}
	};


Lines 70-82
1
2
3
4
5
6
7
8
9
10
11
12
13
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[count1].b[count1].title << ", the price: $" << a[count1].b[count1].price << endl;
		}
	}
}


Like i just dont know what exactly is going wrong, just looking at it it seems like it should work. But obviously it doesn't
Ok a has to be:
1
2
3
4
5
	Author a[SIZE] = {  // You may initialize it with NONE (later) but for testing purposes this is better
		{"A1", { {"B1", 1}, {"B2", 2}, {"B3", 3} } },
		{"A2", { {"B1", 1}, {"B2", 2}, {"B3", 3} } },
		{"A3", { {"B1", 1}, {"B2", 2}, {"B3", 3} } }
	};


cout << "\tThe title: " << a[count1].b[count2].title << ", the price: $" << a[count1].b[count2].price << endl; // don't confuse count1 with count2
Thank you so very very much, I've been up since 8pm doing this and its 4am now D:

So just for future reference if i have a nested structure array i should do it the way you showed me?

P.S. I'm sorry if i was asking dumb questions I'm still learning this, i plan to get better since programming is the only thing that seems to challenge me these days
So just for future reference if i have a nested structure array i should do it the way you showed me?
Yes, except you want to switch to C++. In that case you would introduce a constructor which would do that for you.
Topic archived. No new replies allowed.