Need assistance in the assignment on using an Array of structs.
I need to know if the array is initialized properly or what i need to do to get it initialized.

Some code has been commented out until i get the array initialized.

Here is the code:

#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<string>

using namespace std;

struct Song
{
	string title;
	int track;
};

struct Album
{
	string albumName;
	string artistName;
	Song songList[4];
};

int main()
{
	Album collection[5];

	collection[0].albumName = "Private Investigations";
	collection[0].artistName = "Dire Straits";
	collection[0].songList[0].title = "Sultans of Swing";
	collection[0].songList[0].track = 2;
	collection[0].songList[1].title = "Romeo and Juliet ";
	collection[0].songList[1].track = 4;
	collection[0].songList[2].title = "Money for Nothing ";
	collection[0].songList[2].track = 9;
	collection[0].songList[3].title = "Walk of Life ";
	collection[0].songList[3].track = 10;
	/*collection[1].albumName = " ";
	collection[1].artistName = " ";
	collection[1].songList[0].title = " ";
	collection[1].songList[0].track = 1;
	collection[1].songList[1].title = " ";
	collection[1].songList[1].track = 2;
	collection[1].songList[2].title = " ";
	collection[1].songList[2].track = 3;
	collection[1].songList[3].title = " ";
	collection[2].songList[3].track = 4;
	collection[2].albumName = " ";
	collection[2].artistName = " ";
	collection[2].songList[0].title = " ";
	collection[2].songList[0].track = 1;
	collection[2].songList[1].title = " ";
	collection[2].songList[1].track = 2;
	collection[2].songList[2].title = " ";
	collection[2].songList[2].track = 3;
	collection[2].songList[3].title = " ";
	collection[2].songList[3].track = 4;
	collection[3].albumName = " ";
	collection[3].artistName = " ";
	collection[3].songList[0].title = " ";
	collection[3].songList[0].track = 1;
	collection[3].songList[1].title = " ";
	collection[3].songList[1].track = 2;
	collection[3].songList[2].title = " ";
	collection[3].songList[2].track = 3;
	collection[3].songList[3].title = " ";
	collection[3].songList[3].track = 4;
	collection[4].albumName = " ";
	collection[4].artistName = " ";
	collection[4].songList[0].title = " ";
	collection[4].songList[0].track = 1;
	collection[4].songList[1].title = " ";
	collection[4].songList[1].track = 2;
	collection[4].songList[2].title = " ";
	collection[4].songList[2].track = 3;
	collection[4].songList[3].title = " ";
	collection[4].songList[3].track = 4;*/

	string albumName;

	cout<<"what album to search for? ";
	cin>> albumName;
}	
int search(Album collection[], string albumName)
{
     int location;
     location = albumName;
     for(int i = 0; i<5; i++);
       {
       if(location == -1)
          return -1;
          cout<<"Album name not found.";
       else
cout<<"Album name is:"<<collection[i].albumName<<"at"
                <<location<<".";
        } 
}

Recommended Answers

All 4 Replies

struct Song
{
    string title;
    int track;
};

struct Album
{
    string albumName;
    string artistName;
   struct Song songList[4];//If you want to initialize this without struct //keyword you've to use "typedef"
};

int main(){
struct Album collection[5];
//.....
//....
return 0;
}

Yes, that works. There are a couple other ways to accomplish it

int main()
{
    Album collection[5] = {
        {"Private Investigations","Dire Straits",
            {"Sultans of Swing",2,"Romeo and Juliet ",4,
            "Money for Nothing ",9,"Walk of Life ",10}}

    };

You can also use vector instead of c-style arrays.

Would this search function work with the following code?

Album collection[5] = { {"Private Investigations","Dire Straits", {"Sultans of Swing",2,"Romeo and Juliet ",4, "Money for Nothing ",9,"Walk of Life ",10}}

int search(Album collection[], string albumName){ int location; location = albumName; for(int i = 0; i<5; i++); { if(location == -1) return -1; cout<<"Album name not found."; elsecout<<"Album name is:"<<collection.albumName<<"at" <<location<<"."; } }

That's about the coolest formatting I've seen in a post to date. :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.