Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need this code to be broken down into 3 functions for option onetwo and three.

ID: 3617879 • Letter: I

Question

I need this code to be broken down into 3 functions for option onetwo and three. Im having a lot of troubles with it!!!!The originalquestion(asked by another user can befound......www.cramster.com/answers-mar-10/computer-science/structured-data-file-io-design-and-implement-a-program-to-explore-life-expectancy_806611.aspx.....Please Help me!!!! Right now my program is accepting and countinghow many entries there are from the file but it is not saving it!!so I can not search for the country name since the file is notbeing officially saved and i can make a new folder but nothing isbeing wrote in it because like i have said already the input filein not being saved into the program and i don't know how to fixthis.

THIS IS WHAT I HAVE SO FAR:



 #include <iostream>#include <fstream>using namespace std;void mainmenu();int menu();struct life_exp{string country;int year;double both;double male;double female;} life[500], temp;bool readData(ifstream&, int, life_exp*);void search(string, int, life_exp*);int newfile(ofstream&, int);int main(){ifstream inFile;ofstream outFile;string key;int count,option;bool input=false;option=menu();while(option !=4){if(option==1){cout<<"1. Load data from file"<<endl;readData(inFile, count, life);input=true;}if(option==2){if(input==false){cout<<"Data has not been loaded. ";cout<<"Please load data from file first (select option 1).  ";}if (input==true){cout<<"2. Search data by country"<<endl;search(key, count, life);}}if(option==3){cout<<"3. Write data to file, sorted by country name"<<endl;newfile(outFile, count);}option=menu();}return 0;}bool readData(ifstream &inFile, int count, life_exp life[]){char filename[30];cout<<"Please enter the data filename: ";cin >> filename;inFile.open(filename);if(inFile.fail()){cout<<"Error in opening "<<filename<<endl<<endl;}else{count=0;inFile>>life[count].country;while(inFile){inFile>>life[count].year;inFile>>life[count].both;inFile>>life[count].male;inFile>>life[count].female;count++;inFile>>life[count].country;}cout<<count<<" entries read successfully "<<endl<<endl;inFile.close();}}void search(string key, int count, life_exp life[]){cout<<"Please enter a country name: ";cin>>key;for(int x=0; x<count; x++){if(life[x].country==key){cout<<key<<" "<<life[x].year<<endl;cout<<"Life exp. both sexes: "<<life[x].both<<endl;cout<<"Life exp. male sexes: "<<life[x].male<<endl;cout<<"Life exp. female: "<<life[x].female<<endl<<endl;return;}}cout<< key<<" is not found in database"<<endl;return;}int newfile(ofstream &outFile, int count){ofstream out;char file[30];int i, j;cout<<"Please enter the filename: ";cin >> file;outFile.open(file);for(i=0;i<count-1;i++)for(j=i;j<count;j++)if(life[i].country>life[j].country){temp=life[i];life[i]=life[j];life[j]=temp;}for(i=0;i<count;i++){outFile<<life[i].country<<"  "<<life[i].year<<"  "<<life[i].both<<"  ";outFile<<life[i].male<<"  "<<life[i].female<<endl;}cout<<count<<" entries written successfully "<<endl<<endl;outFile.close();}int menu(){ifstream in;ofstream out;int option;mainmenu();cin>>option;while(option<1 or option>4){cout<<"Error: please select a value with in the range (1-4)"<<endl;mainmenu();cin>>option;}return option;}void mainmenu(){cout<<"Main menu:"<<endl;cout<<"1. Load data from file"<<endl;cout<<"2. Search data by country"<<endl;cout<<"3. Write data to file, sorted by country name"<<endl;cout<<"4. Exit"<<endl;cout<< "Your selection: ";}

Explanation / Answer

please rate - thanks you lost a & in readData so count had the wrong value #include #include using namespace std; void mainmenu(); int menu(); struct life_exp { string country; int year; double both; double male; double female; } life[500], temp; bool readData(ifstream&, int&,life_exp*); void search(string, int, life_exp*); int newfile(ofstream&, int); int main() { ifstream inFile; ofstream outFile; string key; int count,option; bool input=false; option=menu(); while(option !=4) { if(option==1) { cout
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote