- #1
MostlyHarmless
- 345
- 15
Homework Statement
The problem statement is very long, but in a pinch, I'm supposed to take a text file and use 2 different classes, Song class, and Song_Library class to sort, print, etc..
I'm running into the problem(among others) of my output being really really dumb. And I think it's in my read_songs() function. But I'll post the entire code for you to see, because, what do I know, I'm just a math major! :)
The program is also supposed to sort the list by either title or length, which it's not, but I'll worry about that after I get it to read correctly. And it is supposed to shuffle the list, which you'll notice I have an empty method for that, it's to come later.
Generally I'd be emailing my GTA, but he seems to have gone to bed since it not due until tomorrow at midnight, but I need to get it done sooner than that. I've got other stuff to worry about tomorrow. :)
Homework Equations
The Attempt at a Solution
Code:
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
using namespace std;
class Song
{
public:
Song();
void set(string t, string g, string a, int y, int l);
void get(string& t, string& g, string& a, int& y, int& l);
void print();
string get_title();
int get_length();
private:
string Title;
string Genre;
int Year;
string Artist;
int Length;
};
//-----------------------------------------------------------
class Song_Library
{
public:
Song_Library();
Song_Library(string filename);
bool is_empty();
void sort_title();
void sort_length();
void shuffle();
void print();
private:
static const int SIZE = 20;
Song list[SIZE];
int count;
};
//------------------------------------------------------------
//Methods for Song class
//------------------------------------------------------------
Song::Song()
{
Title = "ttt";
Genre = "ggg";
Artist = "aaa";
Length = 0;
Year = 2013;
}
void Song::set(string t, string g, string a, int y, int l)
{
Title = t;
Genre = g;
Artist = a;
Length = l;
Year = y;
}
void Song::get(string &t, string &g, string &a, int &y, int &l)
{
t = Title;
g = Genre;
a = Artist;
l = Length;
y = Year;
}
string Song::get_title()
{
return Title;
}
int Song::get_length()
{
return Length;
}
void Song::print()
{
cout << setw(30) << Title
<< setw(10) << Genre
<< setw(30) << Artist
<< setw(10) << Length
<< setw(10) << Year << endl;
}
//Methods for Song_Library class
//---------------------------------------------------
Song_Library::Song_Library()
{
count = 0;
}
Song_Library::Song_Library(const string filename)
{
fstream infile;
infile.open("songs.txt");
string title;
string genre;
string artist;
string year;
string seconds;
string blank;
count = 0;
while (count < SIZE && !infile.eof())
{
// getline(infile, title);
Song get_title();
getline(infile, genre);
getline(infile, artist);
getline(infile, year);
// getline(infile, seconds);
Song get_length();
getline(infile, blank);
int iseconds = atoi(seconds.c_str());
int iyear = atoi(year.c_str());
list[count].set(title, genre, artist, iyear, iseconds);
count++;
}
}
bool Song_Library::is_empty()
{
if((count = 0))
return true;
else
return false;
}
void Song_Library::sort_title()
{
for (int index = 0; index < SIZE; index++)
{
// Find location of smallest value in unsorted part
int location = index;
for (int pos = index; pos < SIZE; pos++)
if (list[pos].get_title() < list[location].get_title())
location = pos;
// Swap value to end of sorted part
Song temp = list[index];
list[index] = list[location];
list[location] = temp;
}
}
void Song_Library::sort_length()
{
for (int index = 0; index < SIZE; index++)
{
int location = index;
for (int pos = index; pos < SIZE; pos++)
if (list[pos].get_length() < list[location].get_length())
location = pos;
Song temp = list[index];
list[index] = list[location];
list[location] = temp;
}
}
void Song_Library::shuffle()
{
}
void Song_Library::print()
{
cout << setw(30) << "Title"
<< setw(10) << "Genre"
<< setw(30) << "Artist"
<< setw(10) << "Seconds"
<< setw(10) << "Year" << endl;
for (int index = 0; index < SIZE; index++)
list[index].print();
cout << endl;
}
//---------------------------------------------------
void read_songs(Song list[], int size, int &count)
{
string title;
string genre;
string artist;
string year;
string seconds;
string blank;
count = 0;
fstream infile;
infile.open("songs.txt");
while (count < size && !infile.eof())
{
// getline(infile, title);
Song get_title();
getline(infile, genre);
getline(infile, artist);
getline(infile, year);
// getline(infile, seconds);
Song get_length();
getline(infile, blank);
int iseconds = atoi(seconds.c_str());
int iyear = atoi(year.c_str());
list[count].set(title, genre, artist, iyear, iseconds);
count++;
}
}
//---------------------------------------------------------
void print_menu(){
cout << "Please select one of the following options:\n";
cout << "0: for quiting\n";
cout << "1: for printing\n";
cout << "2: for sorting title\n";
cout << "3: for sorting length\n";
cout << "4: for shuffling\n";
cout << "Please type 0 or 1 or 2 or 3 or 4\n";
}
//----------------------------------------------------------
int get_option(){
char option;
string s;
cin.get(option);
getline(cin,s);
while (!( option >= '0' && option <= '4')){
cout << "Wrong option value\n";
print_menu();
cin.get(option);
getline(cin,s);
}
return option - '0';
}
//-----------------------------------------------------------
int main(int argc, char** argv)
{
// initialize library from a file
Song_Library lib("songs.txt");
// menu loop
int option = -1;
while ( option != 0 ){
print_menu();
option = get_option();
// handle the user commands
switch (option){
case 0:{
cout << "Have a good day. Program is terminating\n";
break;
}
case 1:{
if ( lib.is_empty()) {
cout << "Nothing to print for an empty library\n";
} else {
lib.print();
}
break;
}
case 2: {
if ( lib.is_empty()) {
cout << "No title to sort for an empty library\n";
} else {
lib.sort_title();
lib.print();
}
break;
}
case 3: {
if ( lib.is_empty()) {
cout << "No title to sort for an empty library\n";
} else {
lib.sort_length();
lib.print();
}
break;
}
case 4: {
if ( lib.is_empty()) {
cout << "No title to shuffle for an empty library\n";
} else {
lib.shuffle();
lib.print();
}
break;
}
}
}
return 0;
}
Here is the text file being read(This was given to us, don't judge the selection!)
Code:
Roar
Pop
Katy Perry
2013
270
Royals
Pop
Lorde
2013
201
Wrecking Ball
Pop
Miley Cyrus
2013
222
Rap God
Hip Hop
Eminem
2013
367
Wake Me Up!
Pop
Avicii
2013
273
Counting Stars
Rock
OneRepublic
2013
257
Hold On, We're Going Home
Hip Hop
Drake Featuring Majid Jordan
2013
227
The Fox
EDM
Ylvis
2013
213
Applause
Pop
Lady Gaga
2013
212