I have a sample program that shows how to load data into an array but I need to change it to sort movie titles into columns for movie title, length,year, media type from a file named movies.dat It asks first from where to load...then loads in no particular order...then sorts two different ways...then does a search for last names and finds file and outputs info pertaininng to that person. thats what this program does but need to change it to the movie one..Please help
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;
enum Columns {FIRST = 0, LAST = 1};
//Function prototypes
string toUpper(string &);
void bubbleSort(string [][2], int [], int);
void bubbleSort(string [][2], int [], int, int &, int &, int &);
int main()
{
//Declare vars
string Names[25][2]; //or FirstNames[25] and LastName[25]
int Ages[25];
int sub = 0;
int compares = 0;
int passes = 0;
int exchanges = 0;
ifstream dataIn; //declare an instance variable WITHOUT attaching an
//actual file
string filename;
cout << "Enter the data file name (and path): ";
getline(cin, filename);
//Open file
//dataIn.open("C:\\NamesAndAges.dat");
dataIn.open(filename.c_str());
if(dataIn.fail() == true) //same as if(dataIn.fail())
{
cout << "Unable to access the data file." << endl;
return 888;
}
//Load arrays with data from record
dataIn >> Names[sub][FIRST] >> Names[sub][LAST] >> Ages[sub];
while(dataIn.eof() == false) //same as while(!dataIn.eof())
{
sub = sub + 1;
dataIn >> Names[sub][FIRST] >> Names[sub][LAST] >> Ages[sub];
}
dataIn.close();
int maxElements = sub; //ACTUAL number of elements in the arrays
//that contain data
cout << "\n\nArray contents.." << endl << endl;
cout << "First Name Last Name Age" << endl;
cout << "------------------------------------" << endl;
//Display the arrays contents
for(sub = 0; sub < maxElements; sub++)
{
cout << left << setw(15) << Names[sub][0]
<< setw(15) << Names[sub][1]
<< right << setw(5) << Ages[sub] << endl;
}
//Serial search of the array looking for presence or absence of a
//last name value
//Prompt the user for the search argument (SA) - value that you are
//looking for
string lookFor; //the SA
bubbleSort(Names,Ages,maxElements,passes,compares,exchanges);
system("PAUSE");
cout << "\n\nSORTED Array contents.." << endl << endl;
cout << "First Name Last Name Age" << endl;
cout << "------------------------------------" << endl;
//Display the arrays contents
for(sub = 0; sub < maxElements; sub++)
{
cout << left << setw(15) << Names[sub][0]
<< setw(15) << Names[sub][1]
<< right << setw(5) << Ages[sub] << endl;
}
cout << endl << endl;
cout << "Passes: " << passes << " Compares: " << compares
<< " Exchanges: " << exchanges << endl << endl;
system("PAUSE");
//For binary search
int lower;
int upper;
int midpoint;
while(true)
{
compares = 0;
cout << "Enter a last name to look for (ENTER to quit): ";
getline(cin, lookFor);
if(lookFor[0] == '\0')
break; //quit
cout << endl << endl;
bool found = false;
for(sub = 0; sub < maxElements; sub++)
{
//cout << toUpper(lookFor) << endl;
//cout << toUpper(Names[sub][LAST]) << endl;
compares++;
if(toUpper(lookFor) == toUpper(Names[sub][LAST]))
{
//Found a match
found = true;
break; //exit the search loop
}
}
cout << endl << "SERIAL SEARCH RESULTS.." << endl << endl;
if(found == true)
{
cout << lookFor << " was FOUND in the array." << endl;
cout << Names[sub][FIRST] << " is " << Ages[sub] << " years old." << endl
<< "Comparisons: " << compares << endl << endl;
}
else
{
cout << lookFor << " was NOT FOUND in the array." << endl
<< "Comparisons: " << compares << endl << endl;
}
//END OF SERIAL SEARCH CODE
lower = 0;
upper = maxElements - 1;
compares = 0;
found = false;
while(lower <= upper)
{
midpoint = (lower + upper) / 2;
compares++;
if(lookFor == Names[midpoint][LAST])
{
found = true; //found a match
break;
}
else if (lookFor > Names[midpoint][LAST])
{
lower = midpoint + 1;
}
else
{
upper = midpoint - 1;
}
}//end binary search loop
cout << endl << "BINARY SEARCH RESULTS.." << endl << endl;
//BINARY SEARCH RESULTS
if(found == true)
{
cout << lookFor << " was FOUND in the array." << endl;
cout << Names[midpoint][FIRST] << " is " << Ages[midpoint] << " years old." << endl
<< "Comparisons: " << compares << endl << endl;
}
else
{
cout << lookFor << " was NOT FOUND in the array." << endl
<< "Comparisons: " << compares << endl << endl;
}
}//end infinite loop
return 0;
}
//Function definitions
string toUpper(string & name) //function header
{
int ltr;
string result = name; //make the two string vars the same length
for(ltr = 0; ltr < name.length(); ltr++)
{
if(name[ltr] >= 'a' && name[ltr] <= 'z')
{
result[ltr] = name[ltr] & 0xdf;
}
}
return result;
}
void bubbleSort(string names[][2], int ages[], int howManyElements)
{
int limit = howManyElements;
bool swapped = true; //Assume we have exchanged something
while(swapped == true)
{
//passes loop
swapped = false; //Assume the array is sorted
for(int sub=0; sub<limit - 1; sub++)
{
if(names[sub][LAST] > names[sub+1][LAST])
{
//names are out of order, so exchange them
string temp = names[sub][LAST];
names[sub][LAST] = names[sub+1][LAST];
names[sub+1][LAST] = temp;
swapped = true; //indicate that the swap has occurred
temp = names[sub][FIRST];
names[sub][FIRST] = names[sub+1][FIRST];
names[sub+1][FIRST] = temp;
int temp2 = ages[sub];
ages[sub] = ages[sub+1];
ages[sub+1] = temp2;
}
}//exlimit--changes loop
//limit--; //reduce the number of elements to compare for next pass
}// end passes loop
return;
}
void bubbleSort(string names[][2], int ages[], int howManyElements, int & pass, int & comp, int & exc)
{
int limit = howManyElements;
bool swapped = true; //Assume we have exchanged something
while(swapped == true)
{
//passes loop
swapped = false; //Assume the array is sorted
pass++;
for(int sub=0; sub<limit - 1; sub++)
{
comp++;
if(names[sub][LAST] > names[sub+1][LAST])
{
exc++;
//names are out of order, so exchange them
string temp = names[sub][LAST];
names[sub][LAST] = names[sub+1][LAST];
names[sub+1][LAST] = temp;
swapped = true; //indicate that the swap has occurred
temp = names[sub][FIRST];
names[sub][FIRST] = names[sub+1][FIRST];
names[sub+1][FIRST] = temp;
int temp2 = ages[sub];
ages[sub] = ages[sub+1];
ages[sub+1] = temp2;
}
}//exchanges loop
limit--; //reduce the number of elements to compare for next pass
}// end passes loop
return;
}