I pretty new to coding and a lot of things are pretty foreign to me. I am writing a c++ program that is supposed to take a list of songs from a txt file and be able to shuffle, sort, and search for a song in the list. I have only started on the sorting part as of now, but I am having trouble finding out how to format the algorithm to work with my vector of songs.
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstdlib>
#include <time.h>
#include <stdlib.h>
#include <bits/stdc++.h>
#include <algorithm>
#include "song.h"
using namespace std;
// given to you
void processFile(vector<Song> &playlist);
// you should create
void shuffle(vector<Song> &playlist);
void bubbleSort(vector<Song> &playlist);
void displayPlaylist(vector<Song> playlist);
int binarySearch(vector<Song> &playlist, string songTitle);
int main()
{
vector<Song> playlist;
// sets up playlist
processFile(playlist);
cout << "\nInitial playlist: " << endl;
//displayPlaylist(playlist);
displayPlaylist(playlist);
cout << "Welcome to the playlist display manager." << endl << endl;
while(1)
{
int option;
cout << "0. Exit" << endl;
cout << "1. Sort Playlist" << endl;
cout << "2. Shuffle Playlist" << endl;
cout << "3. Search Playlist" << endl;
cout << "Which option would you like" << endl;
cin >> option;
if(option == 0)
{
break;
}
else if(option == 1)
{
bubbleSort(playlist);
displayPlaylist(playlist);
}
else if(option == 2)
{
}
else if(option == 3)
{
}
else
{
cout << "invalid response...try again" << endl;
}
}
return 0;
}
void processFile(vector<Song> &playlist)
{
ifstream infile;
string line;
infile.open("songs.txt");
if(infile.is_open())
{
cout << "Successful songs opening." << endl;
}
else
{
cout << "Couldn't locate file. Program closing." << endl;
exit(EXIT_FAILURE);
}
while(getline(infile, line))
{
// first line --> song
// second line --> artist
if(line != "")
{
string song, artist;
song = line;
getline(infile, artist);
Song temp(song, artist);
playlist.push_back(temp);
}
}
return;
}
void shuffle(vector<Song> &playlist)
{
}
void selectionSort(vector<Song> &playlist, int n)
{
}
void bubbleSort(vector<Song>& playlist)
{
int size;
size = playlist.size();
for(int i= 0; i < size - 1; i++)
{
int smallIndex = i;
for(int j = i + 1; j < size; j++)
{
if(&playlist[j] < &playlist[smallIndex])
{
smallIndex = j;
}
}
string song, artist;
Song temp(song, artist);
temp = playlist[i];
playlist[i] = playlist[smallIndex];
playlist[smallIndex] = temp;
}
}
//display songs
void displayPlaylist(vector<Song> playlist)
{
for(int i = 0; i < playlist.size(); i++)
{
cout << playlist[i].getTitle() << " - " << playlist[i].getArtist() << endl;
}
}
Here is what I have so far. I am supposed to use a function to sort the sort the songs. The vector uses a class that was given to me to help classify the line of songs in the txt file by song then artist (title being the first thing listed in the line) and I'm supposed to sort by the title. This is just the last algorithm I attempted. I'm not required to use selection sorting. Whenever I call the function and try to display the list, it comes out the same.
edit: Sorry, it just occurred that I should probably go ahead and show all my code even if it's not done.