What i have to create is a program that establishes 2 structs with arrays of CD Albums and then search the arrays for album names and display the album name, Artist or group, songs and their track numbers.
My question is; [COLOR="red"]have i missed something in the code?[/COLOR], because it does not display the first album in the array when i do the search, it allows me to enter the name to search for but does not conduct the search, i would like to know what is wrong with the code.

thank you.

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

using namespace std;

struct Song
{
    string title;
    int track;
};

struct Album
{
    string albumName;
    string artistName;
    struct 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;


string albumName;

    cout<<"what album to search for? ";
    cin>> albumName;
}

int search(Album collection[], string albumName)
{
    int location;
    for(int i = 0; i<5; i++);
    {
        if(location == -1)          
            return -1;
        else
            cout<<"Album name is:"<<collection[i].albumName<<" "<<collection[i].artistName<<"at"<<location<<".";
Member Avatar for iamthwee

Do you know how to compare strings?

>have i missed something in the code?
Probably. You don't initialize or set the location variable in your search. So basically the entire thing is hopelessly broken as posted.

the semicolon after the for statement is a really bad idea. This for will only count up to 5, becouse with the ";" you close the loop statement. On the other hand you should compare the elements of the collection with the searched string.

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

using namespace std;

struct Song
{
    string title;
    int track;
};

struct Album
{
    string albumName;
    string artistName;
    struct 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;

string albumName;

    cout<<"what album to search for? ";
    cin>> albumName;
}

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

That's a good way to confuse the hell out of readers and introduce subtle scoping bugs. My general guideline for blocks with optional braces is that if there's a one line statement, you can go ahead with the omission. If the block has more than one line, including comments, use braces. So your code would be:

for(int i = 0; i<5; i++)
{
  if(strcmp(collection[i].albumName, albumName)==0)
  {
    location = i;break;
  }
}

Of course, that brings me to the real error, which is using strcmp with std::string objects. Not such a hot idea, especially since the string class overloads the == operator for comparison. I'm guessing you didn't compile that code.

if (location != -1)
{
  cout<<"Album name is:"<<collection[i].albumName<<" "<<collection[i].artistName<<"at"<<location<<".";
  return location;
}
else return -1;

I think you fell into the trap that I described above. The second if statement isn't inside the loop. The code shouldn't compile because i isn't visible anymore.

On top of that, you've complicated the function a little too much. It could be as simple as this:

int search ( Album collection[], int size, string albumName )
{
  for ( int i = 0; i < size; i++ ) {
    if ( collection[i].albumName == albumName )
      return i;
  }

  return -1;
}

And of course, unless you know for a fact that your code will compile and run as expected (which I do in this case), you should write a test framework. Something like so:

int main()
{
  Album collection[5];

  collection[0].albumName = "Private Investigations1";
  collection[1].albumName = "Private Investigations2";
  collection[2].albumName = "Private Investigations3";
  collection[3].albumName = "Private Investigations4";
  collection[4].albumName = "Private Investigations5";

  cout<< search ( collection, 5, "Private Investigations1" ) <<'\n';
  cout<< search ( collection, 5, "Private Investigations2" ) <<'\n';
  cout<< search ( collection, 5, "Private Investigations3" ) <<'\n';
  cout<< search ( collection, 5, "Private Investigations4" ) <<'\n';
  cout<< search ( collection, 5, "Private Investigations5" ) <<'\n';
  cout<< search ( collection, 5, "Private Investigations" ) <<'\n';
}

I appreciate all who have posted to this Thread, but when i compile, i get no errors, but when i type in the name of the album to search the array for. it returns the statement

Press any key to continue.

With this, why is it not searching the array?
or is it and not finding the album name.

You'll need to post your current code.

I have added additional album names, artists, songs and track numbers to the array.

I also made the changes that were submitted in this thread and compiled with no errors.

Still no results.

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

using namespace std;

struct Song
{
	string title;
	int track;
};

struct Album
{
	string albumName;
	string artistName;
	struct 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 = "The Millennium Collection";
	collection[1].artistName = "The Who";
	collection[1].songList[0].title = "My Generation ";
	collection[1].songList[0].track = 1;
	collection[1].songList[1].title = "Pinball Wizard ";
	collection[1].songList[1].track = 2;
	collection[1].songList[2].title = "Who are You ";
	collection[1].songList[2].track = 3;
	collection[1].songList[3].title = "Squeeze Box ";
	collection[2].songList[3].track = 4;
	collection[2].albumName = "Moving Pictures";
	collection[2].artistName = "RUSH";
	collection[2].songList[0].title = "Tom Sawyer";
	collection[2].songList[0].track = 1;
	collection[2].songList[1].title = "Rad Barchetta";
	collection[2].songList[1].track = 2;
	collection[2].songList[2].title = "YYZ";
	collection[2].songList[2].track = 3;
	collection[2].songList[3].title = "Limelight";
	collection[2].songList[3].track = 4;
	collection[3].albumName = "The Best of 3 Dog Night";
	collection[3].artistName = "3 Dog Night";
	collection[3].songList[0].title = "Joy to the World";
	collection[3].songList[0].track = 1;
	collection[3].songList[1].title = "Easy to be Hard";
	collection[3].songList[1].track = 2;
	collection[3].songList[2].title = "Family of Man";
	collection[3].songList[2].track = 3;
	collection[3].songList[3].title = "Sure as i'm sitting here";
	collection[3].songList[3].track = 4;
	collection[4].albumName = "The Best of Deep Purple";
	collection[4].artistName = "Deep Purple";
	collection[4].songList[0].title = "HUSH";
	collection[4].songList[0].track = 1;
	collection[4].songList[1].title = "Kentucky Woman";
	collection[4].songList[1].track = 2;
	collection[4].songList[2].title = "Black Night";
	collection[4].songList[2].track = 3;
	collection[4].songList[3].title = "Speed King";
	collection[4].songList[3].track = 4;

	string albumName;
	cout<<"What album to search for? ";
	cin>> albumName;

}		
int search(Album collection[], int size, string albumName)
{
	for(int i = 0; i<size; i++)
               {	
		if (collection[i].albumName == albumName)
		return i;
	}
	return -1;
}

Thank you.

You need to call your search function. Just defining it doesn't do anything.

Narue,
Sorry but you lost me with the call the search function, i must have missed that portion of the class.

What and where in the code should it go.

Thank you.

>i must have missed that portion of the class.
Clearly you need to pay more attention. A function doesn't do anything until you call it. When you call it, all of the statements in the function are executed. Here's the working code. I fixed several problems that you would have encountered which you can find and ask about if you want.

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

using namespace std;

struct Song
{
  string title;
  int track;
};

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

int search(Album collection[], int size, string albumName)
{
  for(int i = 0; i<size; i++)
  {	
    if (collection[i].albumName == albumName)
      return i;
  }
  return -1;
}

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 = "The Millennium Collection";
  collection[1].artistName = "The Who";
  collection[1].songList[0].title = "My Generation ";
  collection[1].songList[0].track = 1;
  collection[1].songList[1].title = "Pinball Wizard ";
  collection[1].songList[1].track = 2;
  collection[1].songList[2].title = "Who are You ";
  collection[1].songList[2].track = 3;
  collection[1].songList[3].title = "Squeeze Box ";
  collection[2].songList[3].track = 4;
  collection[2].albumName = "Moving Pictures";
  collection[2].artistName = "RUSH";
  collection[2].songList[0].title = "Tom Sawyer";
  collection[2].songList[0].track = 1;
  collection[2].songList[1].title = "Rad Barchetta";
  collection[2].songList[1].track = 2;
  collection[2].songList[2].title = "YYZ";
  collection[2].songList[2].track = 3;
  collection[2].songList[3].title = "Limelight";
  collection[2].songList[3].track = 4;
  collection[3].albumName = "The Best of 3 Dog Night";
  collection[3].artistName = "3 Dog Night";
  collection[3].songList[0].title = "Joy to the World";
  collection[3].songList[0].track = 1;
  collection[3].songList[1].title = "Easy to be Hard";
  collection[3].songList[1].track = 2;
  collection[3].songList[2].title = "Family of Man";
  collection[3].songList[2].track = 3;
  collection[3].songList[3].title = "Sure as i'm sitting here";
  collection[3].songList[3].track = 4;
  collection[4].albumName = "The Best of Deep Purple";
  collection[4].artistName = "Deep Purple";
  collection[4].songList[0].title = "HUSH";
  collection[4].songList[0].track = 1;
  collection[4].songList[1].title = "Kentucky Woman";
  collection[4].songList[1].track = 2;
  collection[4].songList[2].title = "Black Night";
  collection[4].songList[2].track = 3;
  collection[4].songList[3].title = "Speed King";
  collection[4].songList[3].track = 4;

  string albumName;
  cout<<"What album to search for? ";
  getline ( cin, albumName );

  cout<< search ( collection, 5, albumName ) <<'\n';
}

Hello again,

Still having issues with this code. i made some minor changes to the cout entry in the search function, to display the artist name, songs and track numbers. When i compile there are no errors, but when i run the program and type in the album name to find it, it lists all the artists names and the songs and track numbers in the array. How do i set it up to only display the artist name and songs and tracks for that album when searching by the album name?
Here is the code i have so far.

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

using namespace std;

struct Song
{
	string title;
	int track;
};

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

int search(Album collection[], int size, string albumName)
{
	for(int i = 0; i<size; i++)
	for(int sl = 0; sl<4; sl++)
	{	
	if (collection[i].albumName == albumName)
		cout<<collection[i].albumName<<", "<<'\n';
		cout<<collection[i].artistName<<", "<<'\n';
		cout<<collection[i].songList[sl].title <<", "; 
		cout<<collection[i].songList[sl].track << ", "<<'\n';
	}
	return -1;
}

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 = "The Millennium Collection ";
	collection[1].artistName = "The Who ";
	collection[1].songList[0].title = "My Generation ";
	collection[1].songList[0].track = 1;
	collection[1].songList[1].title = "Pinball Wizard ";
	collection[1].songList[1].track = 2;
	collection[1].songList[2].title = "Who are You ";
	collection[1].songList[2].track = 3;
	collection[1].songList[3].title = "Squeeze Box ";
	collection[1].songList[3].track = 4;
	collection[2].albumName = "Moving Pictures ";
	collection[2].artistName = "RUSH ";
	collection[2].songList[0].title = "Tom Sawyer ";
	collection[2].songList[0].track = 1;
	collection[2].songList[1].title = "Red Barchetta ";
	collection[2].songList[1].track = 2;
	collection[2].songList[2].title = "YYZ ";
	collection[2].songList[2].track = 3;
	collection[2].songList[3].title = "Limelight ";
	collection[2].songList[3].track = 4;
	collection[3].albumName = "The Best of 3 Dog Night ";
	collection[3].artistName = "3 Dog Night ";
	collection[3].songList[0].title = "Joy to the World ";
	collection[3].songList[0].track = 1;
	collection[3].songList[1].title = "Easy to be Hard ";
	collection[3].songList[1].track = 2;
	collection[3].songList[2].title = "Family of Man ";
	collection[3].songList[2].track = 3;
	collection[3].songList[3].title = "Sure as i'm sitting here ";
	collection[3].songList[3].track = 4;
	collection[4].albumName = "The Best of Deep Purple ";
	collection[4].artistName = "Deep Purple ";
	collection[4].songList[0].title = "HUSH ";
	collection[4].songList[0].track = 1;
	collection[4].songList[1].title = "Kentucky Woman ";
	collection[4].songList[1].track = 2;
	collection[4].songList[2].title = "Black Night ";
	collection[4].songList[2].track = 3;
	collection[4].songList[3].title = "Speed King ";
	collection[4].songList[3].track = 4;

	string albumName;
	cout<<"What album to search for? "<<'\n';
	getline (cin, albumName);

	cout<< search ( collection, 5, albumName ) <<'\n';
}
if (collection[i].albumName == albumName)
		cout<<collection[i].albumName<<", "<<'\n';
		cout<<collection[i].artistName<<", "<<'\n';
		cout<<collection[i].songList[sl].title <<", "; 
		cout<<collection[i].songList[sl].track << ", "<<'\n';

Okay, until you know how things work, always use braces:

if (collection[i].albumName == albumName)
{
		cout<<collection[i].albumName<<", "<<'\n';
		cout<<collection[i].artistName<<", "<<'\n';
		cout<<collection[i].songList[sl].title <<", "; 
		cout<<collection[i].songList[sl].track << ", "<<'\n';
}

Attached is the printout window when i run the following code.

I want to display only the artist name once and the songs and tracks associated with that album.

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 search(Album collection[], int size, string albumName)
{
	int location = 0;
	for(int i = 0; i < size; i++)
	for(int sl = 0; sl < size; sl++)
		{
		if(collection[i].albumName == albumName)
		cout<<" by " <<collection[i].artistName<<'\n'
		<<collection[i].songList[sl].title<<" on Track #"<<collection[i].songList[sl].track<<"."<<'\n';
	}
	return -1;
}

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 = "The Millennium Collection ";
	collection[1].artistName = "The Who ";
	collection[1].songList[0].title = "My Generation ";
	collection[1].songList[0].track = 1;
	collection[1].songList[1].title = "Pinball Wizard ";
	collection[1].songList[1].track = 2;
	collection[1].songList[2].title = "Who are You ";
	collection[1].songList[2].track = 3;
	collection[1].songList[3].title = "Squeeze Box ";
	collection[1].songList[3].track = 4;
	collection[2].albumName = "Moving Pictures ";
	collection[2].artistName = "RUSH ";
	collection[2].songList[0].title = "Tom Sawyer ";
	collection[2].songList[0].track = 1;
	collection[2].songList[1].title = "Rad Barchetta ";
	collection[2].songList[1].track = 2;
	collection[2].songList[2].title = "YYZ ";
	collection[2].songList[2].track = 3;
	collection[2].songList[3].title = "Limelight ";
	collection[2].songList[3].track = 4;
	collection[3].albumName = "The Best of 3 Dog Night ";
	collection[3].artistName = "3 Dog Night ";
	collection[3].songList[0].title = "Joy to the World ";
	collection[3].songList[0].track = 1;
	collection[3].songList[1].title = "Easy to be Hard ";
	collection[3].songList[1].track = 2;
	collection[3].songList[2].title = "Family of Man ";
	collection[3].songList[2].track = 3;
	collection[3].songList[3].title = "Sure as i'm sitting here ";
	collection[3].songList[3].track = 4;
	collection[4].albumName = "The Best of Deep Purple ";
	collection[4].artistName = "Deep Purple ";
	collection[4].songList[0].title = "HUSH ";
	collection[4].songList[0].track = 1;
	collection[4].songList[1].title = "Kentucky Woman ";
	collection[4].songList[1].track = 2;
	collection[4].songList[2].title = "Black Night ";
	collection[4].songList[2].track = 3;
	collection[4].songList[3].title = "Speed King ";
	collection[4].songList[3].track = 4;

	string albumName;
	cout<<"what album to search for? ";
	getline( cin, albumName);

	cout<< search (collection, 5, albumName);
}

There are some bugs in your code. Considering that you pass the size as 5 but have a Song array of size 4 causes you to access an location which doesn't belong to you. Also make your function 'search' return void since the return value is not serving any purpose.

Here is the partially modified code:

//Untested
#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];
};

void search(Album collection[], int size, string albumName)
{
    int location = 0;
    bool found = false;
    for(int i = 0; i < size; ++i)
    {
        if(collection[i].albumName == albumName)
        {
            if(!found)
                found = true;
            cout << albumName << " by " << collection[i].artistName << '\n';
            for(int j = 0; j < size; ++j)
            {
                cout << '\t' << collection[i].songList[j].track << ". "
                    << collection[i].songList[j].title << '\n';
            }
        }
    }
    if(!found)
        cout << "No such album found!!!";
}

int main()
{

    Album collection[4];

    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 = "The Millennium Collection ";
    collection[1].artistName = "The Who ";
    collection[1].songList[0].title = "My Generation ";
    collection[1].songList[0].track = 1;
    collection[1].songList[1].title = "Pinball Wizard ";
    collection[1].songList[1].track = 2;
    collection[1].songList[2].title = "Who are You ";
    collection[1].songList[2].track = 3;
    collection[1].songList[3].title = "Squeeze Box ";
    collection[1].songList[3].track = 4;
    collection[2].albumName = "Moving Pictures ";
    collection[2].artistName = "RUSH ";
    collection[2].songList[0].title = "Tom Sawyer ";
    collection[2].songList[0].track = 1;
    collection[2].songList[1].title = "Rad Barchetta ";
    collection[2].songList[1].track = 2;
    collection[2].songList[2].title = "YYZ ";
    collection[2].songList[2].track = 3;
    collection[2].songList[3].title = "Limelight ";
    collection[2].songList[3].track = 4;
    collection[3].albumName = "The Best of 3 Dog Night ";
    collection[3].artistName = "3 Dog Night ";
    collection[3].songList[0].title = "Joy to the World ";
    collection[3].songList[0].track = 1;
    collection[3].songList[1].title = "Easy to be Hard ";
    collection[3].songList[1].track = 2;
    collection[3].songList[2].title = "Family of Man ";
    collection[3].songList[2].track = 3;
    collection[3].songList[3].title = "Sure as i'm sitting here ";
    collection[3].songList[3].track = 4;

    string albumName;
    cout<<"what album to search for? ";
    getline( cin, albumName);

    search (collection, 4, albumName);
}

The following code compiles without errors, and when i search for the first album name, it displays the first Album in the array with the artist name, songs and track numbers as the code is written, BUT when i enter in another album name from the Array the printout displays

No such album found!!!
Press any key to continue.

Here is the code and attached is the printout window with the name of the second album in the array.

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

void search(Album collection[], int size, string albumName);

int main()
{
	Album collection[4];

	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 = "The Millennium Collection ";
	collection[1].artistName = "The Who ";
	collection[1].songList[0].title = "My Generation ";
	collection[1].songList[0].track = 1;
	collection[1].songList[1].title = "Pinball Wizard ";
	collection[1].songList[1].track = 2;
	collection[1].songList[2].title = "Who are You ";
	collection[1].songList[2].track = 3;
	collection[1].songList[3].title = "Squeeze Box ";
	collection[1].songList[3].track = 4;
	collection[2].albumName = "Moving Pictures ";
	collection[2].artistName = "RUSH ";
	collection[2].songList[0].title = "Tom Sawyer ";
	collection[2].songList[0].track = 1;
	collection[2].songList[1].title = "Rad Barchetta ";
	collection[2].songList[1].track = 2;
	collection[2].songList[2].title = "YYZ ";
	collection[2].songList[2].track = 3;
	collection[2].songList[3].title = "Limelight ";
	collection[2].songList[3].track = 4;
	collection[3].albumName = "The Best of 3 Dog Night ";
	collection[3].artistName = "3 Dog Night ";
	collection[3].songList[0].title = "Joy to the World ";
	collection[3].songList[0].track = 1;
	collection[3].songList[1].title = "Easy to be Hard ";
	collection[3].songList[1].track = 2;
	collection[3].songList[2].title = "Family of Man ";
	collection[3].songList[2].track = 3;
	collection[3].songList[3].title = "Sure as i'm sitting here ";
	collection[3].songList[3].track = 4;
	
	string albumName;
	cout<<"what album to search for? ";
	getline( cin, albumName);

	search (collection, 4, albumName);
}

void search(Album collection[], int size, string albumName)
{
    bool found = false;
    for(int i = 0; i < size; i++)
    {
	if(collection[i].albumName == albumName)
	{	
	if(!found)                
	found = true;
                     cout<<albumName<<" by " <<collection[i].artistName<<'\n';
	for(int sl = 0; sl < size; sl++)
	     {
	      cout<<'\t'<<collection[i].songList[sl].title<<" on Track # "
	      <<collection[i].songList[sl].track<<"."<<'\n';
	     }
	}
     }
        if(!found)
	cout << "No such album found!!!"<<'\n';
}

Thats because there is a space after the word when you enter it into the collection while when you search for it, you don't. Try pressing a single space after entering your query and it should work.

So you need to insert the data in the collection without spaces and trim spaces from the input entered by the user.

Where in the following code would i enter a display statement that asks the user if they would like to see the list of albums in the database with the requirement to enter a Y or N with the Y conducting a search of the database for the album names and N quitting the application.

Here is the code

//Kevin Pillsbury 10 Jun 2007 Program #3 Using Arrays in structs.

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

void seqsearch(Album collection[], int size, string albumName);

void search(Album collection[], int size, string albumName); /

int main()
{
	Album collection[4];						

	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 = "The Millennium Collection";
	collection[1].artistName = "The Who ";
	collection[1].songList[0].title = "My Generation";
	collection[1].songList[0].track = 1;
	collection[1].songList[1].title = "Pinball Wizard";
	collection[1].songList[1].track = 2;
	collection[1].songList[2].title = "Who are You";
	collection[1].songList[2].track = 3;
	collection[1].songList[3].title = "Squeeze Box";
	collection[1].songList[3].track = 4;
	collection[2].albumName = "Moving Pictures";
	collection[2].artistName = "RUSH ";
	collection[2].songList[0].title = "Tom Sawyer";
	collection[2].songList[0].track = 1;
	collection[2].songList[1].title = "Rad Barchetta";
	collection[2].songList[1].track = 2;
	collection[2].songList[2].title = "YYZ";
	collection[2].songList[2].track = 3;
	collection[2].songList[3].title = "Limelight";
	collection[2].songList[3].track = 4;
	collection[3].albumName = "The Best of 3 Dog Night";
	collection[3].artistName = "3 Dog Night ";
	collection[3].songList[0].title = "Joy to the World";
	collection[3].songList[0].track = 1;
	collection[3].songList[1].title = "Easy to be Hard";
	collection[3].songList[1].track = 2;
	collection[3].songList[2].title = "Family of Man";
	collection[3].songList[2].track = 3;
	collection[3].songList[3].title = "Sure as i'm sitting here";
	collection[3].songList[3].track = 4;
	
	string albumName;						
	cout<<"Enter the album name: ";		
	getline( cin, albumName);				

	search (collection, 4, albumName);		
}//end main

void search(Album collection[], int size, string albumName) 
{
	bool found = false;						
	for(int i = 0; i < size; i++)			
	{
		if(collection[i].albumName == albumName)	
		{	
			if(!found)						
				 found = true;				
            cout<<albumName<<" by " <<collection[i].artistName<<'\n';	
			for(int sl = 0; sl < size; sl++)	
			{
				cout<<'\t'<<collection[i].songList[sl].title<<" on Track # "
					<<collection[i].songList[sl].track<<"."<<'\n';	
			}
		}
	}
	if(!found)								
		cout << "No such album found!!!"<<'\n';	
}//end search

Trying to display the command to enter Y or N to display the album names only, then have the command to enter an album name and then display the artist name, songs, and track numbers. I have some code that displays the list of albums in the array, but it will not display the next line that asks them to enter the name of the Album to search for the songs and track numbers.

Any help would be appreciated.

Thank you.

Here is the code i have so far.

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

void asearch(Album collection[], int size, string albumName);

void search(Album collection[], int size, string albumName);

int main()
{
	Album collection[4];						

	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 = "The Millennium Collection";
	collection[1].artistName = "The Who ";
	collection[1].songList[0].title = "My Generation";
	collection[1].songList[0].track = 1;
	collection[1].songList[1].title = "Pinball Wizard";
	collection[1].songList[1].track = 2;
	collection[1].songList[2].title = "Who are You";
	collection[1].songList[2].track = 3;
	collection[1].songList[3].title = "Squeeze Box";
	collection[1].songList[3].track = 4;
	collection[2].albumName = "Moving Pictures";
	collection[2].artistName = "RUSH ";
	collection[2].songList[0].title = "Tom Sawyer";
	collection[2].songList[0].track = 1;
	collection[2].songList[1].title = "Rad Barchetta";
	collection[2].songList[1].track = 2;
	collection[2].songList[2].title = "YYZ";
	collection[2].songList[2].track = 3;
	collection[2].songList[3].title = "Limelight";
	collection[2].songList[3].track = 4;
	collection[3].albumName = "The Best of 3 Dog Night";
	collection[3].artistName = "3 Dog Night ";
	collection[3].songList[0].title = "Joy to the World";
	collection[3].songList[0].track = 1;
	collection[3].songList[1].title = "Easy to be Hard";
	collection[3].songList[1].track = 2;
	collection[3].songList[2].title = "Family of Man";
	collection[3].songList[2].track = 3;
	collection[3].songList[3].title = "Sure as i'm sitting here";
	collection[3].songList[3].track = 4;

	string albumName;
	bool choice;
	bool Y = true;
	bool N = false;
	cout<<"To display the list of the albums? Enter Y or N "<<'\n';
	cin>>choice;
	{
		if(choice != N)
			{
				asearch (collection, 4, albumName);
			}
		else if (choice == N)
			cout<<"Not displaying albums in Database!!!"<<'\n';
			return -1;
	}

	cout<<"Enter the album name: ";		
	getline( cin, albumName);				

	search (collection, 4, albumName);		
}//end main

void asearch(Album collection[], int size, string albumName)
{
	bool Y = true;
	bool N = false;
	if(Y)
		for (int i = 0; i<size; i++)
		{
			cout<<collection[i].albumName<<"."<<'\n';
		}
	else if(N)
		cout<<"Not displaying albums in Database!!!"<<'\n';
}

void search(Album collection[], int size, string albumName) 
{
	bool found = false;						
	for(int i = 0; i < size; i++)			
	{
		if(collection[i].albumName == albumName)	
		{	
			if(!found)						
				 found = true;				
            cout<<albumName<<" by " <<collection[i].artistName<<'\n';	
			for(int sl = 0; sl < size; sl++)	
			{
				cout<<'\t'<<collection[i].songList[sl].title<<" on Track # "
					<<collection[i].songList[sl].track<<"."<<'\n';	
			}
		}
	}
	if(!found)								
		cout << "No such album found!!!"<<'\n';	
}//end search

Your basics need a bit of reworking.

This code:

else if (choice == N)
    cout<<"Not displaying albums in Database!!!"<<'\n';
    return -1;

doesn't do what you intend it to do. Here N is interpreted as a variable and not a literal, which it should have been. Plus just because two statements are indented doesn't mean they lie in the same block, this is C++ not Python. Plus your return statement returns from the main function, so the code following it is never executed.

You need to accept the input as character, check if it is 'N' or 'Y' (quotes are important) and depending on that display search result or display error message.

I think your basics are not strong enough to attempt something like this. Read some good tutorials here and here.

Hello even i would try to help.
It ca be simple like this

in int main() you can incorporate a few more lines:

int choice;
        cout<<"enter your choice";
        cin>>choice;
        if(choice==1)
  search(collection,4,albumname);
else
cout<<"no  album found"
cin.get();
return 0;
}

void search(Album collection[], int size, string albumName) 
     { 
    for(int i = 0; i < size; i++)            
    {
        if(collection[i].albumName == albumName)    
        {                        
                   cout<<albumName<<" by " <<collection[i].artistName<<'\n';    
            for(int sl = 0; sl < size; sl++)    
      
                cout<<'\t'<<collection[i].songList[sl].title<<" on Track # "
                    <<collection[i].songList[sl].track<<"."<<'\n';    
            }
        }
    }

Besides I have a question that why have you declared albumname of string type 2 times.

One in struct Album the other in main()?

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.