Design and implement a program to explore life expectancydata stored in a file (
ID: 3617791 • Letter: D
Question
Design and implement a program to explore life expectancydata stored in a file(data.txt).
Description of the data file (data.txt must not be changed):
• Source: U.S. Census Bureau
• Format of the table:
Country Year Life_exp_both_sexes Life_exp_male Life_exp_female
Life_exp: Life expectancy
• The format of the table has been simplified so that allcountry names consist of only a single
word.
• You may assume that the number of countries in the data filenever exceeds 500.
Main menu: In the beginning, the program must prompt the user toselect an option from the
menus below (1 – 4). If the user selects an invalid option,the program prints an error message
and displays the main menu again.
Main menu:
1. Load data from file
2. Search data by country
3. Write data to file, sorted by country name
4. Exit
Your selection: 1
Option 1: If the user selects 1, then the program asks the user toenter the filename of the data
file. If the file exists, then the program loads the data into anarray of structured data (struct)
and prints the number of entries; otherwise, it prints out an errormessage. After that, the
program displays the main menu again.
1. Load data from file
Please enter the data filename: wrong.txt
Error in opening wrong.txt
Main menu:
1. Load data from file
2. Search data by country
3. Write data to file, sorted by country name
4. Exit
Your selection: 1
1. Load data from file
Please enter the data filename: data.txt
222 entries read successfully
Main menu:
1. Load data from file
2. Search data by country
3. Write data to file, sorted by country name
4. Exit
Your selection: 2
Option 2: If the user selects 2:
• If the data has been read by the program, then the programasks the user to enter a country
name. If the input country name is found (use the binary searchalgorithm), the program
displays the corresponding data; otherwise, it prints out a messagesaying that the country is
not found.
• If the data has not been read by the program, then theprogram asks the user to load data
from file first.
• After that, the program is back to the main menu.
Suppose the user has not selected option 1 (has not read the datafrom file):
2. Search data by country
Please enter a country name: Canada
Data has not been loaded.
Please load data from file first (select option 1).
Main menu:
1. Load data from file
2. Search data by country
3. Write data to file, sorted by country name
4. Exit
Your selection:
Suppose the user has selected option 1 (has read the data fromfile):
2. Search data by country
Please enter a country name: Canada
Canada 2007
Life exp. both sexes: 80.3
Life exp. male: 77
Life exp. female: 83.9
Main menu:
1. Load data from file
2. Search data by country
3. Write data to file, sorted by country name
4. Exit
Your selection: 3
Option 3: If the user selects 3:
• If the data has been read by the program, then the programasks the user to enter the output
filename and write a list of countries to the file (sorted bycountry name).
• If the data has not been read by the program, then theprogram asks the user to load data
from file first.
• After that, the program is back to the main menu.
3. Write data to file, sorted by country name
Please enter the filename: sorted.txt
222 entries written successfully
Main menu:
1. Load data from file
2. Search data by country
3. Write data to file, sorted by country name
4. Exit
Your selection: 4
Option 4: If the user selects 4, then the program terminates.
Here is the txt data:
Canada 2007 80.3 77 83.9
Italy2007 79.9 77 83.1
CzechRepublic2007 76.4 73.1 79.9
Niger2007 44 44 44
Belgium2007 78.9 75.8 82.2
Gambia,The 2007 54.5 52.7 56.5
Here is the txt data:
Canada 2007 80.3 77 83.9
Italy2007 79.9 77 83.1
CzechRepublic2007 76.4 73.1 79.9
Niger2007 44 44 44
Belgium2007 78.9 75.8 82.2
Gambia,The 2007 54.5 52.7 56.5
Explanation / Answer
x.Xlor="red">pleaserate - thanks #include #include using namespace std; struct life_exp { string name; int year; double both; double male; double female; } life[500],temp; int menu(); int main() { ifstream in; ofstream out; char filename[30]; string key; int low,max,mid; int i,j,count,choice; bool input=false,found,sorted=false; for(;;) {choice=menu(); switch(choice) {case 1:coutfilename; in.open(filename); if(in.fail()) {coutlife[count].male; in>>life[count].female; count++; in>>life[count].name; } coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.