So that I can display based on the 8 different choices.
Code:
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
//Constants - This is used inside sort function to determine
//which field to sort on
const int MOVIE_TITLE = 0;
const int YEAR = 1;
const int MEDIA = 2;
const int LENGTH =3;
//Global Variables
string title[188];
string year[188];
string mediaType[188];
string length[188];
void Sort(const int col, int howMany, bool dir)
{
switch(col)
{
case MOVIE_TITLE:
{
string temp_title;
string temp_year;
string temp_mediaType;
string temp_length;
int limit = howMany;//The number of elements in the array to be searched
bool swapped = true;
int j = 0;
while(swapped)
{
swapped = false;//No swapp has taken place
//Propagate through array
for(int sub=0;sub<limit - 1; sub++)
{
//dir - asc, or desc order, nothing to do with files
if(dir)
{
//cout << "IN DIR IF"<<endl;cin.get();
//ASCENDING
if(title[sub] > title[sub+1])
{
//elements are out of order..swap them
//Write first to temp
temp_title = title[sub];
temp_year = year[sub];
temp_mediaType = mediaType[sub];
temp_length = length[sub];
//Overwrite first with second
title[sub] = title[sub+1];
year[sub] = year[sub+1];
mediaType[sub] = mediaType[sub+1];
length[sub] = length[sub+1];
//Overwrite second with temp
title[sub+1] = temp_title;
year[sub+1] = temp_year;
mediaType[sub+1] = temp_mediaType;
length[sub+1] = temp_length;
swapped = true;
}
}//End If Level 7
else
{
//cout << " DIR IS NOLONGER TRUE"<<endl;cin.get();
//DESCENDING
if(title[sub] < title[sub+1])
{
//cout << " IN IF1"<<endl;cin.get();
//elements are out of order..swap them
//Write first to temp
temp_title = title[sub];
temp_year = year[sub];
temp_mediaType = mediaType[sub];
temp_length = length[sub];
//Overwrite first with second
title[sub] = title[sub+1];
year[sub] = year[sub+1];
mediaType[sub] = mediaType[sub+1];
length[sub] = length[sub+1];
//Overwrite second with temp
title[sub+1] = temp_title;
year[sub+1] = temp_year;
mediaType[sub+1] = temp_mediaType;
length[sub+1] = temp_length;
swapped = true;
}
}//End Else Level 7
//}//End If Level 6
}//End For Level 5
}//End While Level 4
}//End Case Movie Level3
break;
case YEAR:
{
string temp_title;
string temp_year;
string temp_mediaType;
string temp_length;
int limit = howMany;//The number of elements in the array to be searched
bool swapped = true;
int j = 0;
while(swapped)
{
//cout << "In While"<<endl;
swapped = false;//No swapp has taken place
//Propagate through array
for(int sub=0;sub<limit - 1; sub++)
{
//dir - asc, or desc order, nothing to do with files
if(dir)
{
//cout << "IN DIR IF"<<endl;cin.get();
//ASCENDING
if(year[sub] > year[sub+1])
{
//elements are out of order..swap them
//Write first to temp
temp_title = title[sub];
temp_year = year[sub];
temp_mediaType = mediaType[sub];
temp_length = length[sub];
//Overwrite first with second
title[sub] = title[sub+1];
year[sub] = year[sub+1];
mediaType[sub] = mediaType[sub+1];
length[sub] = length[sub+1];
//Overwrite second with temp
title[sub+1] = temp_title;
year[sub+1] = temp_year;
mediaType[sub+1] = temp_mediaType;
length[sub+1] = temp_length;
swapped = true;
}
}//End If Level 7
else
{
//cout << " DIR IS NOLONGER TRUE"<<endl;cin.get();
//DESCENDING
if(year[sub] < year[sub+1])
{
//cout << " IN IF1"<<endl;cin.get();
//elements are out of order..swap them
//Write first to temp
temp_title = title[sub];
temp_year = year[sub];
temp_mediaType = mediaType[sub];
temp_length = length[sub];
//Overwrite first with second
title[sub] = title[sub+1];
year[sub] = year[sub+1];
mediaType[sub] = mediaType[sub+1];
length[sub] = length[sub+1];
//Overwrite second with temp
title[sub+1] = temp_title;
year[sub+1] = temp_year;
mediaType[sub+1] = temp_mediaType;
length[sub+1] = temp_length;
swapped = true;
}
}//End Else Level 7
//}//End If Level 6
}//End For Level 5
}//End While Level 4
}//End Case Movie Level3
break;
case MEDIA:
{
string temp_title;
string temp_year;
string temp_mediaType;
string temp_length;
int limit = howMany;//The number of elements in the array to be searched
bool swapped = true;
int j = 0;
while(swapped)
{
//cout << "In While"<<endl;
swapped = false;//No swapp has taken place
//Propagate through array
for(int sub=0;sub<limit - 1; sub++)
{
//dir - asc, or desc order, nothing to do with files
if(dir)
{
//cout << "IN DIR IF"<<endl;cin.get();
//ASCENDING
if(mediaType[sub] > mediaType[sub+1])
{
//elements are out of order..swap them
//Write first to temp
temp_title = title[sub];
temp_year = year[sub];
temp_mediaType = mediaType[sub];
temp_length = length[sub];
//Overwrite first with second
title[sub] = title[sub+1];
year[sub] = year[sub+1];
mediaType[sub] = mediaType[sub+1];
length[sub] = length[sub+1];
//Overwrite second with temp
title[sub+1] = temp_title;
year[sub+1] = temp_year;
mediaType[sub+1] = temp_mediaType;
length[sub+1] = temp_length;
swapped = true;
}
}//End If Level 7
else
{
//cout << " DIR IS NOLONGER TRUE"<<endl;cin.get();
//DESCENDING
if(mediaType[sub] < mediaType[sub+1])
{
//cout << " IN IF1"<<endl;cin.get();
//elements are out of order..swap them
//Write first to temp
temp_title = title[sub];
temp_year = year[sub];
temp_mediaType = mediaType[sub];
temp_length = length[sub];
//Overwrite first with second
title[sub] = title[sub+1];
year[sub] = year[sub+1];
mediaType[sub] = mediaType[sub+1];
length[sub] = length[sub+1];
//Overwrite second with temp
title[sub+1] = temp_title;
year[sub+1] = temp_year;
mediaType[sub+1] = temp_mediaType;
length[sub+1] = temp_length;
swapped = true;
}
}//End Else Level 7
//}//End If Level 6
}//End For Level 5
}//End While Level 4
}//End Case Movie Level3
break;
case LENGTH:
{
string temp_title;
string temp_year;
string temp_mediaType;
string temp_length;
int limit = howMany;//The number of elements in the array to be searched
bool swapped = true;
int j = 0;
while(swapped)
{
//cout << "In While"<<endl;
swapped = false;//No swapp has taken place
//Propagate through array
for(int sub=0;sub<limit - 1; sub++)
{
//dir - asc, or desc order, nothing to do with files
if(dir)
{
//cout << "IN DIR IF"<<endl;cin.get();
//ASCENDING
if(length[sub] > length[sub+1])
{
//elements are out of order..swap them
//Write first to temp
temp_title = title[sub];
temp_year = year[sub];
temp_mediaType = mediaType[sub];
temp_length = length[sub];
//Overwrite first with second
title[sub] = title[sub+1];
year[sub] = year[sub+1];
mediaType[sub] = mediaType[sub+1];
length[sub] = length[sub+1];
//Overwrite second with temp
title[sub+1] = temp_title;
year[sub+1] = temp_year;
mediaType[sub+1] = temp_mediaType;
length[sub+1] = temp_length;
swapped = true;
}
}//End If Level 7
else
{
//cout << " DIR IS NOLONGER TRUE"<<endl;cin.get();
//DESCENDING
if(length[sub] < length[sub+1])
{
//cout << " IN IF1"<<endl;cin.get();
//elements are out of order..swap them
//Write first to temp
temp_title = title[sub];
temp_year = year[sub];
temp_mediaType = mediaType[sub];
temp_length = length[sub];
//Overwrite first with second
title[sub] = title[sub+1];
year[sub] = year[sub+1];
mediaType[sub] = mediaType[sub+1];
length[sub] = length[sub+1];
//Overwrite second with temp
title[sub+1] = temp_title;
year[sub+1] = temp_year;
mediaType[sub+1] = temp_mediaType;
length[sub+1] = temp_length;
swapped = true;
}
}//End Else Level 7
//}//End If Level 6
}//End For Level 5
}//End While Level 4
}//End Case Movie Level3
break;
}//End Switch level2
}//End Sort
void selectionSort(const int, int, bool);
int main()
{
string record;
int recordcount = 0;
ifstream dataIn;
float A, B, C, D, E, F, G, H;
//Open file
dataIn.open("Movies.dat");
if(dataIn.fail())
{
cout << "Unable to open the data file." << endl;
return 99;
}
//Read a record
getline(dataIn,record);
//location in the record of field separator (,)
int firstHalf = 0;
int i;
recordcount = 0;
while(dataIn.eof() == false)
{
if(record == "")
{
break;
}
i = record.find(',',0);
title[recordcount] = record.substr(0,i);
firstHalf = i+ 1;
i = record.find(',',i+1);
year[recordcount] = record.substr(firstHalf, i - firstHalf);
firstHalf = i+ 1;
i = record.find(',',i+1);
length[recordcount] = record.substr(firstHalf, i - firstHalf);
firstHalf = i+ 1;
i = record.find(',',i+1);
mediaType[recordcount] = record.substr(firstHalf, i - firstHalf);
recordcount++; //point to next element
getline(dataIn,record);
}
//Menu to select which type of sort
cout << "Which way would you like to see these movies sorted?" << endl;
cout << "A. By title. (via Bubble sort (Ascending)) " << endl;
cout << "B. By year.(via Bubble sort (Ascending))" << endl;
cout << "C. By media.(via Bubble sort (Ascending))" << endl;
cout << "D. By length.(via Bubble sort (Asending))" << endl;
cout << "E. By title. (via selection sort (Decending)) " << endl;
cout << "F. By year.(via selection sort (Decending))" << endl;
cout << "G. By media.(via selection sort (Decending))" << endl;
cout << "H. By length.(via selection sort (Decending))" << endl;
cout << "Enter your choice here:" << endl;
cin >> int choice >> endl;
//Where we plug in choice
if (choice = A)
{
Sort(choice,188,true);
for (int i = 0; i < recordcount; i++)
{
if((i % 5) == 0)
{
cout << "Press [Enter] to continue";
cin.get();
}
cout << "title:"<<title[i]<<"\n";
cout << "year:"<<year[i]<<"\n";
cout << "media:"<<mediaType[i]<<"\n";
cout << "length:"<<length[i]<<"\n";
}
return 0;
}
}
}
void selectionSort(const int size, int col, bool dir)
{
int i;
int currentIndex;
//Array of numbers to sort
string smallest;
int smallestIndex;
//Perform selection sort
for(currentIndex = 0; currentIndex < size; currentIndex++)
{
//Look for the smallest number, and
//its index in the array.
smallest = title[currentIndex];
smallestIndex = currentIndex;
for(i = currentIndex; i < size; i++)
{
if(title[i] < smallest)
{
smallest = title[i];//Store the smallest number
smallestIndex = i;//store its index
}
}
//swap the current number with
//the smallest one
title[smallestIndex] = title[currentIndex];
title[currentIndex] = smallest;
}
}