Question: hello i need a little help. Am using linux (putty) compiler, i need help on how i can fix my code to read from the
hello i need a little help.
Am using linux ("putty") compiler, i need help on how i can fix my code to read from the input file. read my program below:
//Name: //Class: Programming II //Due: September 15, 2017 //This program will sort an array of movies, calculate the // production cost and percent profit and then sort the final resurt.
//MGM_movies.cpp #include
using namespace std;
struct movie { string release_date; string title; string budget; string box; double b; double bo; };
void delete_chars(string & st, char ch) {
int i = st.find(ch); while(i > -1) { st.replace(i, 1, ""); i = st.find(ch); }
}
int main() { freopen("input.movies","r",stdin); //ifstream fin; //fin.open("movie.txt"); //int data;
//for(int i = 0; i<26; i++){ //fin>>data; //cout< string st, re, tit; string bud, bx; int d, x; movie data[26]; //imports file into struct fields for(int i = 0; i < 26; i++) { getline(cin, st); //to get the data re = st.substr(0, 12); //gets the release date. tit = st.substr(20, 39); //gets the title of the movie bud = st.substr(60, 18); // bx = st.substr(85, 21); delete_chars(bud, '$'); delete_chars(bud, ','); delete_chars(bx, '$'); delete_chars(bx, ','); data[i].release_date = re; data[i].title = tit; data[i].budget = bud; data[i].box = bx; } cout << "The list of films unsorted" << endl; cout << "-----------------------------------------------------------------------------------------" << endl; cout << endl; //prints the file from struct fields for(int j = 0; j < 26; j++) { cout << data[j].release_date << " " << data[j].title << " " << data[j].budget << " " << data[j].box << endl; } cout << "-----------------------------------------------------------------------------------------" << endl; int size = 26; //converts strings into floating point numbers //and then copies values into another struct field for(int f = 1 ; f < size; f++) { data[f].b = atof(data[f].budget.c_str()); data[f].bo = atof(data[f].box.c_str()); } //Sorts file by film title int swaps = 1; size = 26; while(swaps) { swaps = 0; for(int x = 1; x < size - 1; x++) { if(data[x].title > data[x + 1].title) { swap(data[x], data[x + 1]); swaps = 1; } } } //Prints sorted fields cout << setprecision(2) << fixed; cout << endl; cout << "Sorted by title" << endl; cout << "-----------------------------------------------------------------------------------------" << endl; cout << endl; cout << data[0]. title << setw(15) << right << data[0].release_date << setw(20) << right << data[0].budget << setw(15) << right << data[0].box << endl; for(int y = 1; y < 26; y++) { cout << data[y].title << setw(15) << right << data[y].release_date << setw(20) << right << data[y].b << setw(15) << right << data[y].bo << endl; } cout << "-----------------------------------------------------------------------------------------" << endl; //sorts fields by percent profit swaps = 1; while(swaps) { swaps = 0; for(int e = 1; e < size - 1; e++) { if(data[e].bo / data[e].b < data[e + 1].bo / data[e + 1].b) { swap(data[e], data[e + 1]); swaps = 1; } } } //prints sorted fields cout << endl; cout << "Sorted by profit" << endl; cout << "-----------------------------------------------------------------------------------------" << endl; cout << endl; cout << data[0]. title << setw(15) << right << data[0].release_date << setw(20) << right << data[0].budget << setw(15) << right << data[0].box << endl; for(int y = 1; y < 26; y++) { cout << data[y].title << data[y].release_date << setw(20) << right << data[y].b << setw(15) << right << data[y].bo << endl; } cout << "-----------------------------------------------------------------------------------------" << endl; } //input file movie.txt 11111111112222222222333333333344444444445555555555666666666677777777778888888888999999999900000 12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234 Release Date Movie Production Budget Worldwide Box Office Dec 15, 1939 Gone with the Wind $3,900,000 $390,525,192 Jun 4, 1982 Poltergeist $10,700,000 $121,706,019 Feb 9, 2001 Hannibal $87,000,000 $350,100,000 Mar 23, 2001 Heartbreakers $38,000,000 $57,753,825 Aug 3, 2001 Original Sin $26,000,000 $16,521,410 Jun 14, 2002 Windtalkers $115,000,000 $77,628,265 Mar 14, 2003 Agent Cody Banks $25,000,000 $58,240,458 Apr 15, 2005 The Amityville Horror $18,500,000 $108,800,304 Mar 16, 2007 Premonition $20,000,000 $81,461,343 Feb 22, 2008 Charlie Bartlett $12,000,000 $5,295,909 Dec 25, 2008 Valkyrie $90,000,000 $203,902,107 Jun 12, 2009 The Taking of Pelham 123 $110,000,000 $152,364,370 Jul 8, 2011 Zookeeper $80,000,000 $170,805,525 Dec 20, 2011 The Girl with the Dragon Tattoo $90,000,000 $239,373,970 Mar 16, 2012 21 Jump Street $42,000,000 $202,812,429 Aug 8, 2012 Hope Springs $30,000,000 $115,849,781 Nov 8, 2012 Skyfall $200,000,000 $1,110,526,981 Dec 14, 2012 The Hobbit: An Unexpected Journey $250,000,000 $1,017,003,568 Jan 25, 2013 Hansel & Gretel: Witch Hunters $50,000,000 $214,949,716 Mar 27, 2013 G.I. Joe: Retaliation $140,000,000 $371,923,060 Oct 18, 2013 Carrie $30,000,000 $82,409,520 Jun 13, 2014 22 Jump Street $50,000,000 $331,333,876 Dec 17, 2014 The Hobbit: The Battle of the 5 Armies $250,000,000 $955,119,788 Feb 20, 2015 Hot Tub Time Machine 2 $14,000,000 $12,452,601 May 8, 2015 Hot Pursuit $35,000,000 $45,680,201
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
