Question: C++ I need help, I have a functional program but cannot read the columns in properly to from the input text. Here is my code.

C++ I need help, I have a functional program but cannot read the columns in properly to from the input text.

Here is my code.

#include  #include  #include  #include  using namespace std; struct Movie { string releaseDate; string moveiName; double prodCost; double grossProfit; }; void readData(ifstream& in, Movie movie[], int count) { string movieName; double prodCost; string releaseDate; double grossProfit; in.open("movies.txt"); for (int i = 0; i < count; i++) { in >> releaseDate >> movieName >> prodCost >> grossProfit; movie[i].releaseDate = releaseDate; movie[i].moveiName = movieName; movie[i].prodCost = prodCost; movie[i].grossProfit = grossProfit; } } void sortByTitle(Movie movie[], int size) { string temp; string tempDate; double tempProdCost; double tempGrossProfit; for (int i = 0; i < size; i++) { for (int j = i + 1; j < size; j++) { if ((movie[i].moveiName).compare(movie[j].moveiName) > 0) { temp = movie[i].moveiName; movie[i].moveiName = movie[j].moveiName; movie[j].moveiName = temp; tempDate = movie[i].releaseDate; movie[i].releaseDate = movie[j].releaseDate; movie[j].releaseDate = tempDate; tempProdCost = movie[i].prodCost; movie[i].prodCost = movie[j].prodCost; movie[j].prodCost = tempProdCost; tempGrossProfit = movie[i].grossProfit; movie[i].grossProfit = movie[j].grossProfit; movie[j].grossProfit = tempGrossProfit; } } } } void sortByGrossProfit(Movie movie[], int size) { string temp; string tempDate; double tempProdCost; double tempGrossProfit; for (int i = 0; i < size; i++) { for (int j = i + 1; j < size; j++) { if (movie[i].grossProfit > movie[j].grossProfit) { temp = movie[i].moveiName; movie[i].moveiName = movie[j].moveiName; movie[j].moveiName = temp; tempDate = movie[i].releaseDate; movie[i].releaseDate = movie[j].releaseDate; movie[j].releaseDate = tempDate; tempProdCost = movie[i].prodCost; movie[i].prodCost = movie[j].prodCost; movie[j].prodCost = tempProdCost; tempGrossProfit = movie[i].grossProfit; movie[i].grossProfit = movie[j].grossProfit; movie[j].grossProfit = tempGrossProfit; } } } } int main() { int size = 0; string dateOfRelease; string movieName; double productionCost; double grossProfit; ifstream input; input.open("movies.txt"); while (input >> dateOfRelease >> movieName >> productionCost >> grossProfit) { size++; } input.close(); Movie* movie = new Movie[size]; readData(input, movie, size); sortByTitle(movie, size); cout << "Movies sorted alphabetically by Movie Name" << endl; cout << "Movie Name\tRelease Date\tProduction cost\tGross profit" << endl; cout << "----------\t------------\t---------------\t------------" << endl; for (int i = 0; i < size; i++) { cout << setw(10) << left << movie[i].moveiName << "\t" << movie[i].releaseDate << "\t" << movie[i].prodCost << "\t\t" << movie[i].grossProfit << endl; } sortByGrossProfit(movie, size); cout << " Movies sorted by gross profit" << endl; cout << "Movie Name\tProduction cost\tGross profit\tRelease Date" << endl; cout << "----------\t------------\t---------------\t------------" << endl; for (int i = 0; i < size; i++) { cout << setw(10) << left << movie[i].moveiName << "\t" << movie[i].prodCost << "\t\t" << movie[i].grossProfit << "\t" << movie[i].releaseDate << endl; } return 0; }

Here is the input text:

Jan 25, 1970 MASH $3,025,000 $81,600,000 Aug 5, 1983 The Star Chamber $8,000,000 $5,555,305 Oct 2, 1977 Julia $7,840,000 $20,714,400 May 25, 1979 Alien $11,000,000 $104,931,801 June 3, 1988 Big $18,000,000 $151,668,774 Dec 25, 1992 Hoffa $35,000,000 $29,302,121 Nov 1, 1996 Romeo + Juliet $14,500,000 $147,554,999 April 9, 1999 Never Been Kissed $25,000,000 $84,565,230 Dec 15, 1974 Young Frankenstein $2,780,000 $86,273,333 Dec 27, 1991 Naked Lunch $18,000,000 $2,641,357 May 17, 1974 Dirty Mary Crazy Larry $1,140,000 $28,401,735 March 2, 1979 Norma Rae $4,500,000 $22,228,000 Nov 26, 1997 Alien Resurrection $75,000,000 $161,295,658 Sept 23, 1970 Tora! Tora! Tora! $25,485,000 $29,548,291 June 21, 1991 Dying Young $26,000,000 $82,264,675 June 15, 1979 Butch and Sundance: The Early Days $9,000,000 $2,260,000 Aug 12, 1974 Harry and Tonto $980,000 $4,600,000 July 31, 1992 Buffy the Vampire Slayer $7,000,000 $16,624,456 March 19, 1982 Porky's $500,000 $111,289,673 July 17, 1981 Zorro, The Ga Blade $12,600,000 $5,100,000 June 19, 1998 The X-Files: Fight the Future $66,000,000 $189,176,423 Feb 15, 1985 Turk 182 $15,000,000 $3,594,997 Nov 16, 1990 Home Alone $18,000,000 $476,684,675 Nov 2, 1990 Frankenstein Unbound $11,500,000 $334,748 Nov 20, 1992 Home Alone 2: Lost in New York $20,000,000 $358,994,850 Oct 15, 1993 The Beverly Hillbillies $25,000,000 $57,405,220 July 2, 1996 Independence Day $75,000,000 $817,400,891 Dec 12, 1997 Home Alone 3 $32,000,000 $79,082,515 Sept 24, 1972 Sounder $900,000 $16,889,761 June 30, 1972 Conquest of the Planet of the Apes $1,700,000 $9,700,000 Dec 11, 1985 The Jewel of the Nile $25,000,000 $96,773,200 March 27, 1998 The Newton Boys $27,000,000 $10,452,012 July 20, 1984 Revenge of the Nerds $6,000,000 $60,400,000 July 10, 1987 Revenge of the Nerds II $10,000,000 $30,063,289 May 21, 1975 French Connection II $4,340,000 $12,484,444 Nov 22, 1991 For the Boys $40,000,000 $23,202,444 Dec 25, 1977 High Anxiety $4,015,000 $31,063,038 June 24, 1970 Myra Breckinridge $5,385,000 $4,000,000 Nov 24, 1993 Mrs. Doubtfire $25,000,000 $441,286,195 Sept 26, 1997 Soul Food $7,500,000 $43,700,855 Dec 23, 1990 Come See the Paradise $15,000,000 $947,306 Dec 11, 1987 Wall Street $15,000,000 $43,848,100 May 25, 1977 Star Wars $11,000,000 $775,398,007 May 19, 1999 Star Wars Episode I: The Phantom Menace $115,000,000 $1,027,044,677 Nov 5, 1982 The Man from Snowy River $3,000,000 $17,228,160 July 19, 1985 The Man with One Red Shoe $16,000,000 $8,645,411 Dec 17, 1999 Anna and the King $92,000,000 $113,996,937 June 19, 1981 The Cannonball Run $18,000,000 $72,179,579 Dec 8, 1989 The War of the Roses $50,000,000 $160,188,546 July 4, 1990 Die Hard 2 $70,000,000 $240,031,094 Nov 14, 1997 Anastasia $53,000,000 $139,804,348 July 27, 1973 The Last American Hero $2,130,000 $1,250,000 March 27, 1974 Conrack $2,370,000 $2,000,000 Sept 30, 1994 The Scout $20,000,000 $2,694,234 Dec 7, 1990 Edward Scissorhands $20,000,000 $86,024,005 June 17, 1970 Beyond the Valley of the Dolls $2,090,000 $9,000,000 Aug 15, 1980 Willie & Phil $5,500,000 $4,400,000 Feb 19, 1999 Office Space $10,000,000 $10,827,810 June 15, 1973 Battle for the Planet of the Apes $1,710,000 $8,844,595 May 21, 1971 Escape from the Planet of the Apes $2,060,000 $12,348,905 Oct 1, 1993 Freaked $12,000,000 $29,296 Dec 25, 1998 The Thin Red Line $52,000,000 $98,126,565 December 19, 1997 Titanic $200,000,000 $2,185,372,302 March 29, 1970 The Sicilian Clan $4,170,000 $1,000,000 Feb 18, 1983 The King of Comedy $19,000,000 $2,536,242 Feb 8, 1991 Sleeping with the Enemy $19,000,000 $174,999,005 June 20, 1980 Brubaker $9,000,000 $37,121,708 April 5, 1976 The Blue Bird $12,000,000 $3,500,000 Aug 15, 1986 The Fly $9,000,000 $60,629,159 April 2, 1970 Patton $12,625,000 $61,749,765 June 21, 1985 Cocoon $17,500,000 $85,313,124 Nov 23, 1988 Cocoon: The Return $17,500,000 $18,924,919 March 6, 1987 Raising Arizona $6,000,000 $22,847,564 Dec 21, 1988 Working Girl $28,600,000 $102,953,112 March 1, 1975 At Long Last Love $5,140,000 $2,500,000 Jan 21, 1970 The Only Game in Town $10,235,000 $1,500,000 Sept 26, 1975 The Rocky Horror Picture Show $1,400,000 $175,000,000 April 21, 1995 Kiss of Death $40,000,000 $14,942,422 May 23, 1973 Emperor of the North Pole $3,705,000 $2,000,000 Nov 7, 1979 The Rose $9,250,000 $29,174,648 Dec 13, 1972 The Poseidon Adventure $4,700,000 $93,300,000 Jan 17, 1986 The Boy in Blue $7,700,000 $275,000 Dec 18, 1973 Cinderella Liberty $2,465,000 $3,700,000 May 26, 1976 Mother, Jugs & Speed $3,220,000 $7,630,000 June 16, 1976 Silent Movie $4,055,000 $36,145,695 July 17, 1992 Man Trouble $30,000,000 $4,096,030 June 12, 1981 History of the World, Part I $10,000,000 $31,672,908 Dec 14, 1983 Silkwood $10,000,000 $35,615,609 July 18, 1986 Aliens $18,500,000 $131,060,248 July 6, 1988 License to Drive $8,000,000 $22,433,275 Dec 14, 1980 Tribute $8,000,000 $4,000,000 May 26, 1970 Beneath the Planet of the Apes $4,675,000 $18,999,718 Oct 21, 1977 Damnation Alley $8,000,000 $4,000,000 Dec 19, 1980 Nine to Five $10,000,000 $103,290,500 Jan 9, 1998 Firestorm $19,000,000 $8,165,212 April 1, 1976 The Duchess and the Dirtwater Fox $4,590,000 $4,120,000 April 10, 1992 FernGully: The Last Rainforest $22,000,000 $32,710,894 June 30, 1995 Mighty Morphin Power Rangers: The Movie $15,000,000 $66,433,194 June 26, 1998 Doctor Dolittle $70,500,000 $294,456,605 

Expected output with appropriate outputs sorted

Movies sorted alphabetically by Movie Name Movie Name Release Date Production cost Gross profit ---------- ------------ --------------- ------------

Movies sorted by gross profit Movie Name Production cost Gross profit Release Date ---------- ------------ --------------- ------------

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!