Initializing a structure array

Hello all, this is my first post and I am new to C++ so please be gentle, lol.

The assignment requires all strings and doubles within the array be initialized to "NONE" and 0 without using any loops. If I could use a loop it's easy, but doing it in one line is completely stumping me

The issue lies with line 31, I don't receive any errors, I just can't initialize every value so I get memory addresses where it was not initialized.

Below is my code, any assistance is appreciated.

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
#include<iostream>
#include<string>
#include<iomanip>

using namespace std;

//Structure to hold the book information for an author
struct BookInfo
{
	string title;
	double price;
};

//Structure to hold the author and their book information
struct Author
{
	string author;
	BookInfo books[3];
};

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

int main()
{
	int index = 3;

//This is where I run into the problem, I cannot figure out how to initialize the whole array
	Author catalogue[3] = {"NONE", {"NONE", 0},"NONE", {"NONE", 0}, "NONE", {"NONE", 0}};

	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;
			}
	}
}
Last edited on
I would probably go ahead and make those data types classes, that way you can initialize the data right away. Instead of arrays, i'd use vectors, that way you can handle an infinitely expanding amount of books, instead of having a fixed amount of 3.

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
#include <vector>

class BookInfo
{
public:
    BookInfo() //default constructor
    BookInfo(string _title, double _price) //optional constructor to take title and price.
    ~BookInfo()
    
    string getTitle() {return title;} //returns book title
    double getPrice() {return price;} //returns book price

    void setTitle(string _title) {title = _title;} //function to set title
    void setPrice(double _price) {price = _price;} //and another to set price.

private:
    string title;
    double price;    
};

BookInfo::BookInfo() {
    title = "NONE";  //initialize to default text
    price = 0;

    //every time you create an object "BookInfo" title and price will be given
    //those default values unless you specify otherwise. 
}
BookInfo::BookInfo(string _title, double _price) { //Optional constructor lets you set data differently
    title = _title;
    price = _price;
}

class Author
{
public:
    Author() {author = "NONE";}
    Author(string _author) {author = _author;}
    ~Author()

    string getAuthor() {return author;}
    vector<BookInfo> getBooks() {return books;}

    void setAuthor(string _author) {author = _author;}
    void addBook(BookInfo book) {books.push_back(book);}

private:
    string author;
    vector<BookInfo> books;

};


it's a little bit lengthier, but it'l make programming down the line a lot more simple. So to modify your main function to use those classes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
   int index = 3;
   Author catalogue[3];

   //you don't need to worry about initialization, that's already taken care of
   //when the object was created because of how we defined Author/BookInfo.

   //But lets add a book as an example
   catalogue[0].setAuthor("John Steinbeck");
   catalogue[0].addBook(BookInfo("Of Mice and Men", 9.99));
   cout << "Data after initialization:" << endl;
   showInfo(catalogue, index);
}


and you would inherently have to change up your other functions to use the new implementation of Author and BookInfo.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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].getAuthor() << endl;
		
		vector<BookInfo> authorBooks = a[x].getBooks();
		if(!authorBooks.empty()) //test to make sure the author has books
		{
			for(int y = 0; y < authorBooks.size(); y++) //go through each entry in authorBooks. Almost identical to using arrays.
			{
				cout << "\tThe title: " << authorBooks[y].title << ", the price: $" << authorBooks[y].price << endl;
			}
		}
		else
			cout << "Author has no books." << endl;
	}
}


The output should look something like this
Data after initialization:
The author: John Steinbeck
The title: Of Mice and Men, the price: $9.99
The author: NONE
Author has no books.
The author: NONE
Author has no books.


So I haven't tested this code, so it's bound to be buggy. This is just the general idea of how i would approach the situation you're in. Hope i could help.
Last edited on
Thumper, I appreciate the reply, and will definitely benefit from your code as a learning tool.

However, we have not learned classes and my professor said he isn't covering vectors until Comp SCI II.

Essentially I have to use the format currently in place with structures and arrays as the assignment calls for it. I just am stumped on the initialization, which was one of the requirements the professor made, since I missed that class taking my daughter to an appointment.

Once again, I really appreciate it, and will definitely take some good time to learn from your code and get ahead with classes/vectors.
Right, i often forget assignments require things to be done a certain way. Sorry about that.

You can also create a constructor for structs if i'm not mistaken.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//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]; //the constructor in BookInfo takes care of initialization.

        Author() { //so we don't have to worry about it in here.
                author = "NONE"; 
        }
};


Give that bit a shot. See if it works.

EDIT:
Let me know if these things work. I'm at work right now and don't have a compiler to test them.
Last edited on
Works like a charm, thank you very much Thumper.

Never heard of a constructor before, so I will be sure to look into those as well.
Anytime!

Constructors are functions you define in a struct or class that are called immediately as the object is created (without you having to do anything). The constructors we defined set the values of the struct to the defaults that you wanted.

Also, this site has a wonderful reference of the language. If you get stumped it's a great resource. Sometimes even more so than a text book. http://cplusplus.com/doc/tutorial/

Best of luck in CS!
-Thumper
Topic archived. No new replies allowed.