14 Replies - 713 Views - Last Post: 25 April 2011 - 01:58 AM Rate Topic: -----

#1 collegejunkie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 05-February 11

Pretty much creating itunes! Arrays, getline, and classes

Posted 18 April 2011 - 12:48 PM

You are tasked with implementing the myTunes application’s interface. Your program
should read in from a file called library.txt the specifics of 25 songs – the name of the
song, the artist of the song, the album the song comes from, the minutes and seconds of
the song. At the beginning of your program, the songs should be printed out in the order
in which they were read from the file (unsorted).
You are to present the following options for sorting the songs – by song name, artist
name, album name, or time. You will need to allow the user to choose to sort in
ascending or descending order.
Additionally, you will need to allow for searching. Allow the user to type in the name of
a song, artist, or album. If there is a match, print out the songs that match the criteria. If
there is no match, print out a message to that effect. You may not use any predefined
libraries or functions for searching or sorting.
Requirements:
You must use a class called myTunes. This class has five data attributes – song name,
artist name, album title, minutes, and seconds. You must have getters and setters that
address these.
Other methods for this class (at minimum): display – prints out a track, in order of song
name, artist name, album name, and time (mm:ss); and assign – takes the contents of an
object of myTunes and assigns it to the object calling assign.
Outside of the class definition, you’ll need functions to address the following: searching,
sorting, reading data from the file, menu, and printing of tracks. You may find writing a
swap function particularly useful, too.
You’ll need some kind of array to handle the data (hint: an array of myTunes objects,
maybe?).
Other tips:
Look at the way the data is formatted in the file and look at how it appears in the sample
screenshots. You’ll have to figure out how to address changing the data in places, after
you’ve read it in.
You may find the cin.ignore command useful in certain places.

// Viers, Ethan 
// April 12, 2011
//assignment 10

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
using std::getline;
using std::setw;

int menuoption;

class myTunes{
      
      public:
             
             myTunes(string n, string art, string alb, int mm, int ss){
                       setSongName(n);
                       setSongArtist(art);
                       setSongAlbum(alb);
                       setSongMinutes(mm);
                       setSongSeconds(ss);
                      
             }
             
             void setSongName(string n){
                  SongName = n;
                  }
             void setSongArtist(string art){
                  SongArtist = art;
                  }
             void setSongAlbum(string alb){
                  SongAlbum = alb;
                  }
             void setSongMinutes(int mm){
                  SongMinutes = mm;
                  }
             void setSongSeconds(int ss){
                  SongSeconds = ss;
                  }
             string getSongName(){
                    return SongName;
                    }
             string getSongArtist(){
                    return SongArtist;
                    }
             string getSongAlbum(){
                    return SongAlbum;
                    }
             int getMinutes(){
                 return SongMinutes;
                 }
             int getSeconds(){
                 return SongSeconds;
                 }
      private:
              string SongName, SongArtist, SongAlbum;
              int SongMinutes, SongSeconds;


};
int menu(){
    cout<<"1) Song Name"<<endl;
    cout<<"2) Artist Name"<<endl;
    cout<<"3) Album Name"<<endl;
    cout<<"4) Track Time"<<endl;
    cout<<"5) Search for a song name" <<endl;
    cout<<"6) Search for an artist name"<<endl;
    cout<<"7) Search for an album name"<<endl;
    cout<<"8) Quit"<<endl;
    cin >> menuoption;
}

int main() 
{
    ifstream inFile;
    inFile.open("library1.txt");
    string songname, songartist, songalbum;
    int songminutes, songseconds;
    int counter = 0;
    char letter;
    int text[25];
    string myTunes[25];
    string insert;
    int input;
    
    while(getline(inFile, songname))
{
    myTunes[counter] = songname;
    songartist = songname.substr( songname.find(" ")+1, songname.length()-1 );
    letter = songartist;
    input = letter;
    text[counter] = input;
    counter++;
}   
                          
    for (int count = 0; count < 25; count++)
    inFile >> myTunes[count];
    


    for (int count = 0; count < 25; count++)
        cout << setw(18) << myTunes[count] << endl;
    

    
    
    
inFile.close();// close the text file (names2.txt)

system("PAUSE");
return 0; 
}


Is This A Good Question/Topic? 0
  • +

Replies To: Pretty much creating itunes! Arrays, getline, and classes

#2 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: Pretty much creating itunes! Arrays, getline, and classes

Posted 18 April 2011 - 01:03 PM

and your question is?
Was This Post Helpful? 1
  • +
  • -

#3 collegejunkie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 05-February 11

Re: Pretty much creating itunes! Arrays, getline, and classes

Posted 18 April 2011 - 01:07 PM

Well first i have to print out the unsorted array! When I run it my text is on top of each other and not in one line, so I don't get all of the songs! How do I get it to print out the songs in name, artist, album, time format?
Was This Post Helpful? 0
  • +
  • -

#4 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: Pretty much creating itunes! Arrays, getline, and classes

Posted 18 April 2011 - 01:46 PM

so you are reading it from a textfile, and that text file is set out the way you want to print it before it is sorted?
Then you will need to use getline to copy the entire line, or have separate arrays for each of the inputs, and input into them in the same loop that you input into the myTunes array.
If you do the first option you can keep the rest of the code, but if you do the second you would need something like
for(i = 0; i < 25; i++)
{
    cout << songName[i] << " " << artist[i] << " " << album[i] << " " << timeMade[i] << endl;
}


Hope This Helps

This post has been edited by sk1v3r: 18 April 2011 - 01:47 PM

Was This Post Helpful? 1
  • +
  • -

#5 collegejunkie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 05-February 11

Re: Pretty much creating itunes! Arrays, getline, and classes

Posted 18 April 2011 - 03:15 PM

// Viers, Ethan 
// April 12, 2011
//assignment 10

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
using std::getline;
using std::setw;

int menuoption;

class myTunes{
      
      public:
             
             myTunes(string n, string art, string alb, int mm, int ss){
                       setSongName(n);
                       setSongArtist(art);
                       setSongAlbum(alb);
                       setSongMinutes(mm);
                       setSongSeconds(ss);
                      
             }
             
             void setSongName(string n){
                  SongName = n;
                  }
             void setSongArtist(string art){
                  SongArtist = art;
                  }
             void setSongAlbum(string alb){
                  SongAlbum = alb;
                  }
             void setSongMinutes(int mm){
                  SongMinutes = mm;
                  }
             void setSongSeconds(int ss){
                  SongSeconds = ss;
                  }
             string getSongName(){
                    return SongName;
                    }
             string getSongArtist(){
                    return SongArtist;
                    }
             string getSongAlbum(){
                    return SongAlbum;
                    }
             int getMinutes(){
                 return SongMinutes;
                 }
             int getSeconds(){
                 return SongSeconds;
                 }
      private:
              string SongName, SongArtist, SongAlbum;
              int SongMinutes, SongSeconds;


};
int menu(){
    cout<<"1) Song Name"<<endl;
    cout<<"2) Artist Name"<<endl;
    cout<<"3) Album Name"<<endl;
    cout<<"4) Track Time"<<endl;
    cout<<"5) Search for a song name" <<endl;
    cout<<"6) Search for an artist name"<<endl;
    cout<<"7) Search for an album name"<<endl;
    cout<<"8) Quit"<<endl;
    cin >> menuoption;
}

int main() 
{
    ifstream inFile;
    inFile.open("library.txt");
    string songname, songartist, songalbum;
    int songminutes, songseconds;
    int counter = 0;
    char letter;
    int text[25];
    string myTunes[25];
    string insert;
    int input;
  
  //inFile.getline >> songname >> songartist >> songalbum >> songminutes >> songseconds; 
   while(getline(inFile, songname))
   {
   myTunes[counter] = songname;
   
   songartist = songname.substr( songname.find(" ")+1, songname.length()-1 );
   letter= songartist;
   input = letter;
   text[counter] = input;
   counter++;
    
    for( int i = 0; i < 25; i++)
        cout << songname[i];
}
           
inFile.close();// close the text file (names2.txt)

system("PAUSE");
return 0; 
}


I Keep getting the error Cannot convert std::string to char with the line ( letter = songartist;) What can i do to get around this?
Was This Post Helpful? 0
  • +
  • -

#6 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: Pretty much creating itunes! Arrays, getline, and classes

Posted 18 April 2011 - 03:22 PM

that is because letter is of type char, and songartist is of type std::string.
If you want a single letter then you cna type
letter = songartist[0];


else you want to make letter an std::string variable
Was This Post Helpful? 1
  • +
  • -

#7 JaKWaC  Icon User is offline

  • D.I.C Head

Reputation: 76
  • View blog
  • Posts: 232
  • Joined: 15-November 10

Re: Pretty much creating itunes! Arrays, getline, and classes

Posted 18 April 2011 - 03:28 PM

sk1v3r beat me.

This post has been edited by JaKWaC: 18 April 2011 - 03:28 PM

Was This Post Helpful? 0
  • +
  • -

#8 collegejunkie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 05-February 11

Re: Pretty much creating itunes! Arrays, getline, and classes

Posted 18 April 2011 - 03:32 PM

This is what I get as my output! But I have more than one song on the same line! How do I get it to seperate the songs? and it cuts off the album and the time!

Lying_From_You Linkin_ParBuilding_A_Mystery Sarah_Wisdom Delerium Karma 4 4What_
I've_Done Marie_DigbLost_My_Faith Seal Human_One Metallica S&M(Disc_2)Swing,_Swi
ng The_All_AmerLet_It_Be The_Beatles 1 3Stricken Disturbed Ten_ThFeel_Like_I_Do
Drowning_POcean_Avenue Yellowcard OSlow_Turning Keith_Urban One_Of_Us Joan_Osbor
ne ReDon't_Drink_The_Water DavWaiting_For_The_End LinkiFull_Of_Grace Sarah_McLac
What_I've_Done Linkin_ParSabotage Beastie_Boys BeaYoung_Forever Jay-Z_Feat.Breat
h Breaking_Benjamin Empire_State_Of_Mind Jay-This_Town O.A.R. All_SideA_Lot_To_D
rink_About JimmCareless_Whisper Seether If_You_Leave Nada_Surf ThThe_Boys_Of_Sum
mer The_AtPress any key to continue . . .
Was This Post Helpful? 0
  • +
  • -

#9 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: Pretty much creating itunes! Arrays, getline, and classes

Posted 18 April 2011 - 05:33 PM

can you post the code that you are working with now?
Was This Post Helpful? 1
  • +
  • -

#10 collegejunkie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 05-February 11

Re: Pretty much creating itunes! Arrays, getline, and classes

Posted 18 April 2011 - 05:36 PM

// Viers, Ethan 
// April 12, 2011
//assignment 10

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
using std::getline;
using std::setw;

int menuoption;

class myTunes{
      
      public:
             
             myTunes(string n, string art, string alb, int mm, int ss){
                       setSongName(n);
                       setSongArtist(art);
                       setSongAlbum(alb);
                       setSongMinutes(mm);
                       setSongSeconds(ss);
                      
             }
             
             void setSongName(string n){
                  SongName = n;
                  }
             void setSongArtist(string art){
                  SongArtist = art;
                  }
             void setSongAlbum(string alb){
                  SongAlbum = alb;
                  }
             void setSongMinutes(int mm){
                  SongMinutes = mm;
                  }
             void setSongSeconds(int ss){
                  SongSeconds = ss;
                  }
             string getSongName(){
                    return SongName;
                    }
             string getSongArtist(){
                    return SongArtist;
                    }
             string getSongAlbum(){
                    return SongAlbum;
                    }
             int getMinutes(){
                 return SongMinutes;
                 }
             int getSeconds(){
                 return SongSeconds;
                 }
      private:
              string SongName, SongArtist, SongAlbum;
              int SongMinutes, SongSeconds;


};
int menu(){
    cout<<"1) Song Name"<<endl;
    cout<<"2) Artist Name"<<endl;
    cout<<"3) Album Name"<<endl;
    cout<<"4) Track Time"<<endl;
    cout<<"5) Search for a song name" <<endl;
    cout<<"6) Search for an artist name"<<endl;
    cout<<"7) Search for an album name"<<endl;
    cout<<"8) Quit"<<endl;
    cin >> menuoption;
}

int main() 
{
    ifstream inFile;
    inFile.open("library.txt");
    string songname, songartist, songalbum;
    int songminutes, songseconds;
    int counter = 0;
    char letter;
    int text[25];
    string myTunes[25];
    string insert;
    int input;
  
  //inFile.getline >> songname >> songartist >> songalbum >> songminutes >> songseconds; 
   while(getline(inFile, songname))
   {
   myTunes[counter] = songname;
   
   songartist = songname.substr( songname.find(" ")+1, songname.length()-1 );
   songalbum = songartist.substr( songartist.find(" ")+1, songartist.length()-1 );
   letter= songartist[0], songalbum[0]; 
   input = letter;
   text[counter] = input;
   counter++;
    
    for( int i = 0; i < 25; i++)
        cout << songname[i];
}
           
inFile.close();// close the text file (names2.txt)

system("PAUSE");
return 0; 
}


Was This Post Helpful? 0
  • +
  • -

#11 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: Pretty much creating itunes! Arrays, getline, and classes

Posted 18 April 2011 - 05:43 PM

letter = songartist[0], songalbum[0];


why are you trying to assign two char's to one? this will not work as you intend

Also,why are you outputting this:
cout << songname[0];



songname is not an array, do you mean to use myTunes?

Also, what does your file look like that you are opening?
Was This Post Helpful? 1
  • +
  • -

#12 collegejunkie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 05-February 11

Re: Pretty much creating itunes! Arrays, getline, and classes

Posted 24 April 2011 - 06:28 PM

This is what my file looks like.

Lying_From_You Linkin_Park Meteora 2 55
Building_A_Mystery Sarah_McLachlan Surfacing 4 7
Wisdom Delerium Karma 4 49

How do I get it to find the first line print it out and then print out the second line? and so on...
Right now I just get them all printed out and they aren't on seperate lines!
Was This Post Helpful? 0
  • +
  • -

#13 collegejunkie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 05-February 11

Re: Pretty much creating itunes! Arrays, getline, and classes

Posted 24 April 2011 - 07:00 PM

If I use cin.ignore at the bottom it will print them out on seperate line but I have to keep pressing enter to get all the lines printed out! How do I get it to print them out automatically on seperate lines?
Was This Post Helpful? 0
  • +
  • -

#14 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 207
  • View blog
  • Posts: 550
  • Joined: 12-July 09

Re: Pretty much creating itunes! Arrays, getline, and classes

Posted 25 April 2011 - 01:49 AM

Your assignment says that you have to write a display() function for your class. Just add a \n character when you output in this function.
Was This Post Helpful? 0
  • +
  • -

#15 janotte  Icon User is online

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: Pretty much creating itunes! Arrays, getline, and classes

Posted 25 April 2011 - 01:58 AM

View PostHezekiah, on 25 April 2011 - 06:49 PM, said:

Your assignment says that you have to write a display() function for your class. Just add a \n character when you output in this function.

Or, since you are writing in C++, you could use std::endl instead of '\n'
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1