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;
}

Recommended Answers

All 3 Replies

I dont seem to understand what exactly do you need help with. As far as I know. If you managed to write out all the above code. Changing it into the movie thingy wouldn't be much of a problem.

I dont seem to understand what exactly do you need help with. As far as I know. If you managed to write out all the above code. Changing it into the movie thingy wouldn't be much of a problem.

I didnt write this program. I was given it as an example but I need to alter it so that it has I guess four arrays two that are int and two string. And I was also wondering if the new file that I am bringing in will load correctly if it has commas in it. This program runs and sorts the age and names below...I need to load and sort the movie one below..

Nancy Lambert 24
Tom Haskins 33
Helen Devon 39
James Clifford 43
Vivian Frederick 22
Charles Stanton 19
Javier Garza 30
Michelle Kenton 27
Kelly Pressley 29
Greg Franklin 18
Vivian Hanson 22
Zoe Bradley 33
Michael Jefferson 20
Larry Paulsen 44
Ida Lukens 37
Ursula Adams 49
Charles Lambert 24


You've Got Mail,1998,109,DVD
Birthday Girl,2002,129,VHS
Get Shorty,1995,117,DVD
Spy Game,2001,120,DVD
About a Boy,2002,117,DVD
Fargo,1995,121,VHS
Absolute Power,1997,109,VHS
Dragonheart,1996,124,DVD
Gone With The Wind,1939,168,DVD
Affliction,1998,114,DVD
Fifty (50) First Dates,2004,110,DVD
Ronin,1998,124,VHS
All About the Benjamins,2002,108,VHS
Chicago,2002,120,DVD
Hart's War,2002,108,VHS
Analyze This,1999,100,VHS
George of the Jungle,1997,104,VHS
Cookie's Fortune,1999,100,VHS
The Bachelor,1999,118,DVD
Back to the Future,1985,103,VHS
Matrix,1999,114,DVD
Almost Famous,2000,129,DVD
Truth About Cats & Dogs,1996,108,VHS
Day After Tomorrow,2004,124,DVD
Conspiracy Theory,1997,121,VHS
Abandon,2002,110,VHS
Blue Streak,1999,108,DVD
Anger Management,2003,120,VHS
Bourne Identity,2002,128,DVD
Alien Resurrection,1997,127,VHS
Bowfinger,1999,99,VHS
Bridget Jones’s Diary,2001,119,DVD
Dog Park,1999,105,VHS
Cast Away,2000,132,DVD
Seabiscuit,2003,108,DVD
Emma,1996,118,DVD
Mumford,1999,106,VHS
The American President,1995,112,DVD
Chicken Run,2000,97,VHS
About Schmidt,2002,124,DVD
Master and Commander: The Far Side of the World,2003,130,DVD
Connie and Carla,2004,111,DVD
Twister,1996,150,DVD
As Good As It Gets,1997,109,DVD
Count of Monte Cristo,2002,122,DVD
Daredevil,2003,118,DVD
Jackie Brown,1997,119,VHS
Die Hard With a Vengeance,1995,120,DVD
Selena,1997,118,DVD
Eddie,1996,107,VHS
Terminator 3: Rise of the Machines,2003,117,DVD
Catch Me If You Can,2002,113,VHS
Apollo 13,1995,145,DVD
Enemy at the Gates,2001,128,DVD
Babe,1995,123,VHS
Enemy of the State,1998,113,VHS
Cold Mountain,2003,146,DVD
Theory of Flight,1998,116,DVD
Dirty Dancing,1987,109,VHS
Erin Brockovich,2000,110,DVD
Ladykillers,2004,108,DVD
Bandits,2001,119,DVD
Ever After: A Cinderella Story,1998,116,DVD
Chain Reaction,1996,104,DVD
Face/Off,1997,108,VHS
City of Angels,1998,108,VHS
Ace Ventura: When Nature Calls,1995,129,VHS
Father of the Bride II,1995,107,VHS
Barbershop,2002,100,DVD
Devil's Own,1997,123,VHS
Cider House Rules,1999,105,VHS
Wedding Singer,1998,119,VHS
Krippendorf's Tribe,1998,112,VHS
Finding Nemo,2003,118,DVD
Air Force One,1997,118,DVD
Disney's The Kid,2000,100,VHS
For Love of the Game,1999,123,DVD
Three Kings,1999,122,DVD
G.I. Jane,1997,126,DVD
Bad Boys II,2003,106,DVD
Gone Fishin',1997,101,VHS
Joe Somebody,2001,105,VHS
Pirates of the Caribbean: The Curse of the Black Pearl,2003,120,DVD
Good Will Hunting,1997,118,DVD
Jaws,1975,115,VHS
Happy Texas,1999,108,VHS
Knight's Tale,2001,104,DVD
Hard Rain,1998,105,DVD
Flubber,1997,100,VHS
Little Mermaid,1989,99,DVD
E.T. The Extra-Terrestrial,1982,149,DVD
Phenomenon,1996,108,VHS
Hope Floats,1998,112,VHS
Parent Trap,1998,100,DVD
Ice Age,2002,103,DVD
Drumline,2002,116,DVD
Independence Day,1996,128,VHS
Runaway Bride,1999,107,DVD
Out To Sea,1997,99,VHS
L.A. Confidential,1997,121,VHS
Behind Enemy Lines,2001,140,DVD
Inspector Gadget,1999,105,VHS
General's Daughter,1999,121,DVD
Last Samurai,2003,125,DVD
Entrapment,1999,119,DVD
I Am Sam,2001,113,DVD
Les Miserables,1998,118,DVD
Heat,1995,123,DVD
Maid in Manhattan,2002,109,DVD
Kiss of the Dragon,2001,120,DVD
Man on Fire,2004,124,DVD
Reindeer Games,2000,120,VHS
Divine Secrets of the Ya-Ya Sisterhood,2002,119,DVD
Mask of Zorro,1998,110,DVD
Ride With the Devil,1999,117,DVD
Italian Job,2003,116,DVD
McHale's Navy,1997,101,VHS
Stepmom,1998,110,DVD
Legally Blonde,2001,103,VHS
Jerry Maguire,1996,108,DVD
Gladiator,2000,154,DVD
Jurassic Park,1993,128,VHS
A.I. - Artificial Intelligence,2001,138,DVD
Fools Rush In,1997,108,VHS
Sweet Home Alabama,2002,104,DVD
Kate & Leopold,2001,104,DVD
Frequency,2000,105,VHS
Phone Booth,2003,111,DVD
Kissing A Fool,1998,117,DVD
Shakespeare in Love,1998,121,DVD
Moulin Rouge,2001,119,DVD
Heaven's Prisoners,1996,118,VHS
The Saint,1997,112,VHS
My Big Fat Greek Wedding,2002,114,DVD
Next Stop Wonderland,1998,107,VHS
Zoolander,2001,100,VHS
Nothing To Lose,1997,109,DVD
Ocean's Eleven,2001,118,DVD
Jackal,1997,122,DVD
Vegas Vacation,1997,104,VHS
Shanghai Knights,2003,109,VHS
October Sky,1999,103,VHS
Hard Day's Night,1964,101,VHS
Rabbit-Proof Fence,2002,114,VHS
Wonder Boys,2000,123,VHS
Grease,1978,107,VHS
Reign of Fire,2002,118,DVD
Open Range,2003,125,DVD
Matchmaker,1997,105,VHS
Remember the Titans,2000,113,DVD
Stuart Little,1999,102,VHS
Road to Perdition,2002,115,DVD
Out of Sight,1998,115,DVD
The Rock,1996,122,VHS
Secondhand Lions,2003,111,DVD
Michael,1996,110,DVD
Toy Story,1995,99,VHS
Whale Rider,2003,112,DVD
Long Kiss Goodnight,1996,111,VHS
Saving Private Ryan,1998,138,DVD
Meet the Parents,2000,102,DVD
Under the Tuscan Sun,2003,117,DVD
Thomas Crown Affair,1999,110,VHS
Serendipity,2001,107,DVD
U.S. Marshals,1998,134,DVD
Space Cowboys,2000,114,DVD
X-Men,2000,115,DVD
Raising Helen,2004,109,DVD
Spider-Man,2002,112,DVD
Wizard of Oz,1939,175,DVD
There's Something About Mary,1998,121,DVD
Ulee's Gold,1997,111,DVD
Patch Adams,1998,118,VHS
The Truth About Charlie,2002,113,DVD
High Fidelity,2000,111,DVD
Return of the Jedi,1983,131,DVD
Two Weeks Notice,2002,109,VHS
Kingpin,1996,100,VHS
Waking Ned Devine,1998,132,DVD
Sweet November,2001,117,DVD
Man In The Iron Mask,1998,117,DVD
Shrek,2001,110,DVD
Where the Heart Is,2000,118,DVD
Finding Forrester,2000,107,DVD
Life As a House,2001,109,VHS
Speed,1994,122,VHS
You Can Count on Me,2000,106,VHS
Notting Hill,1999,102,DVD

For sure you won't be able to load the movie file in that way. Even for the names file it's a bad idea because one character in plus will make the read loop neverending because it relies on the fact that the lines are multiple of 3. For reading you should use something like that:

instead of:

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];
}

you should have a split function and 4 vectors (instead of array's) like that: 


// you can use any split function you want, this is made by me
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems)
{
    int last_pos = 0;
    int curr_pos = 0;
    while((curr_pos = s.find(delim, last_pos)) != std::string::npos)
    {
        std::string res = s.substr(last_pos, curr_pos - last_pos);
        elems.push_back(res);
        last_pos = curr_pos + 1;
    };
    elems.push_back(s.substr(last_pos, s.length()));
    return elems;
}



    vector<std::string> v_movies;
    vector<int> year;
    vector<int> inventory;
    vector<std::string> type;

......


// sequence for parsing movie files , use that instead of what you used up

    char c[256];
    while(dataIn.getline(c, 256))
    {

        vector<std::string> v;
        split(c, ',', v);
        if(v.size() > 1)     // avoid empty lines
        {
            ++sub;
            v_movies.push_back(v[0]);
            year.push_back(atoi(v[1].c_str()));
            inventory.push_back(atoi(v[2].c_str()));
            type.push_back(v[3]);
        }
    } 

....

after this sequence you will have 4 vectors, one with the names of the movie , one with the year, one with inventory number(i thought that's inventory or something similar) and onw with the storage type.

Now it's up to you to adapt the bubble_sort for the movies(it shouldn't be very hard).

Your post requires many modifications of the code, i made the base for you, the rest it's up to you because it's not the purpose of the site to give the whole solution but to provide ideas

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.