As you can tell....I am a beginner.... Please help me get past this problem. I am not sure how to compare strings for equal signs.

I am at a brick wall. I have some unsorted albums with the name of the album, release date, CD Number and unsorted songs. I am reading in as a file. The albums are separated by equal signs.

I need to sort each set of songs and then sort the group of Albums. I have made a program to read in the file, but I am having trouble with figuring out how to make it read in the file up to the === signs and then read in the next group and so forth until it reaches the last group and reads them in.

This is the beginning of my code for reading them in:

#include <iostream>
#include <string>
using namespace std;

int main()
{

char line[255]; //read in line
int position;
string songTitle;
string songTitles[100];
int count = 0;
while(cin.getline(line,255))
{
songTitle = line;
songTitles[count++] = songTitle;
}
cout << "Song Titles: " << endl;
for(int i = 0; i < count - 1; i++) cout << songTitles[i] <<

endl;
return 0;
}

Recommended Answers

All 4 Replies

between lines 15 and 16, just use string's find() method to locate the = symbol and then extract the remainder

size_t pos = songTitle.find('=');
if( pos != string::npos)
{
    songTitle = songTitle.substr(pos+1); // bypass the = 
}

This assumes the string is something like this song title=Hello Dolly

It would help to also see sample of how your data is formatted in the file.

I'm assuming it's something like:
album name 1
song 1
song 2
song 3
===
album name 2
song 1
song 2
song 3
===
album name 3
song 1
song 2
song 3


You need an array of strings for album titles, and a 2D array of strings for the song lists. Or use vectors so you don't have to allocate memory up front, potentially wasting a lot.

Your reading loop needs to account for album titels and the songs in the album, in general:

int num_albums = 0;
int num_songs = 0;
while(cin.getline(line,255))
{
    if( line != "===" )
    {
       albumTitle[num_albums] = line;
       while( cin.getline( line, 255 )
       {
           if( line != "=== )
              songTitles[num_albums][num_songs++] = line;
       }
       num_albums++;
     }
}

You will also need to account for your array limits in the while loop tests, if you're using array types, with vectors, you just keep adding on. And include reading statements for the other album data.

between lines 15 and 16, just use string's find() method to locate the = symbol and then extract the remainder

size_t pos = songTitle.find('=');
if( pos != string::npos)
{
    songTitle = songTitle.substr(pos+1); // bypass the = 
}

This assumes the string is something like this song title=Hello Dolly

It read in everything, but how do I get it to separate into three groups of albums after I apply my bubble sort.

#include <iostream>
      #include <string>
      using namespace std;

      int main()

      {

      char line[255]; //read in line
      int position;
      string songTitle;
      string songTitles[100];
      int count = 0;
      while(cin.getline(line,255))
      {
      songTitle = line;
      songTitles[count++] = songTitle;

      size_t pos = songTitle.find("=");
      if( pos != string::npos)
      {
      songTitle = songTitle.substr(pos+1); // bypass the =
}

	 }
       cout << "Song Titles: " << endl;

      for(int i = 0; i < count - 1; i++) cout << songTitles[i] <<

      endl;
      }
void sortArray(string songTitles[], int size) // size is the number of items in the array
{
   bool swap;
   string temp;

   do
   {
     swap = false;

     for (int count = 0; count < (size - 1); count++)
     {
       if (songTitles[count].substr(4,255) > songTitles[count + 1].substr(4,255))
       {
         temp = songTitles[count];
         songTitles[count] = songTitles[count + 1];
         songTitles[count + 1] = temp;
         swap = true;
       }
     }
   }  while (swap);
}

void showArray(int songTitles[], int size)
{
     for (int count = 0; count < size; count++)
        cout << songTitles[count] << " ";
        cout << endl;

}

This is an example of my data file:

Personal Christmas Collection
Release Date: Aug 23, 1994
(CK 064154)
1. The Christmas Spirit
2. I Heard The Bells On Christmas Day
3. Blue Christmas
4. Christmas As I Knew It
5. The Little Drummer Boy
6. Christmas With You (With June Carter Cash)
7. Silent Night (With Entire Cash Family)
8. Joy To The World
9. Away In A Manger
10. O Little Town Of Bethlehem
11. Hark! The Herald Angels Sing
12. O Come All Ye Faithful
===============================
The Gospel Collection
Release Date: Jul 28, 1992
(CK 048952)
1. It Was Jesus
2. I Saw A Man
3. Are All The Children In
4. The Old Account
5. Lead Me Gently Home
6. Swing Low, Sweet Chariot
7. Snow In His Hair
8. Lead Me Father
9. I Call Him
10. These Things Shall Pass
11. He'll Be A Friend
12. God Will
13. He'll Understand And Say Well Done
14. God Must Have My Fortune Laid Away
15. When I've Learned
16. I Got Shoes
17. Let The Lower Lights Be Burning
18. If We Never Meet Again
19. When I Take My Vacation In Heaven
20. When The Savior Reached Down For Me
21. Taller Than Trees
22. I Won't Have To Cross Jordan Alone
23. My God Is Real (Yes, God Is Real)
24. These Hands
===============================
Blood, Sweat & Tears
Release Date: Oct 11, 1994
(CK 066508)
1. The Legend Of John Henry's Hammer
2. Tell Him I'm Gone
3. Another Man Done Gone
4. Busted
5. Casey Jones
6. Nine Pound Hammer
7. Chain Gang
8. Waiting For A Train
9. Roughneck
===============================

Instead of one big array, read them into an array for each group of albums, similar to the way the file is arranged. And use a vector<string> instead of an array. If you using vector then you can use std::sort instead of coding your own sort algorithm.

vector<string> Christmas;
vector<string> Gospel;
vector<string> BoodSweatTears;

...
std::sort(Christmas.begin(), Christmas.end());
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.