For my project I have to follow this outline
Objectives
To learn to code, compile and run a program containing an array of structures.
Assignment
Plan and code a modular program utilizing an array of structures for the following problem:
The results of a survey of the households in Woodland Hills have been made available. Each record contains a four-character identification code, the annual income for the household, and the number of members of the household. Write a program that inputs the information from a file and then presents a MENU of options available to the user. The options should include:
1. Print all of the input data with appropriate column headings.
2. Calculate the average household income and list the identification codes and income of each household
whose income is greater than the average.
3. Determine the percentage of households having an income below the poverty level. The formula for determining the poverty level is P = $8000.00 +$500.00 * (M- 2) where M is the number of members of the household.
4. Print all of the input data sorted by household income.
5. Calculate and print the median household income. The median is the middle value of a sorted list. If the list contains an even number of entries, the median is the average of the two middle values.
6. exit loop
Input
Input the record information into an array of structures. Use the data file below. Display a well-formatted menu of choices for the user. BE SURE TO ADEQUATELY TEST EACH CHOICE ON YOUR MENU. Do NOT test the options in order. Create the data file below in text editor or Notepad.
Data File
WHVC 34000.00 5
AAAA 10500.00 8
BURB 23500.00 2
CCCC 15000.00 4
DATA 8000.00 3
EEEE 36000.00 5
FADE 8500.00 4
GATE 25000.00 1
HILO 3000.00 1
JURY 100000.00 5
KNEL 80000 4
LIST 41000.00 3
MEMM 5000.00 2
PQRS 18000.00 2
RELM 27500.00 4
SOLD 22100.00 2
Output
Output the appropriate information for the user. Print out each of these options to a data file to hand in to your instructor.
What I have so far is listed below. What I am having trouble doing is Making sure when I input, into the code when it is running, option one that it says in that original format no matter what I choose before option one.
E.G. I choose option 4 first than choose option one. I want it to be in the original format the file is in when I choose option one
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
//LAB8
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct household{
char idcode[4];
float annualincome;
int members;
};
void printall(household households[]);
void printaboveavg(household households[]);
void printbelowpoverty(household households[]);
void printallsortedincome(household households[]);
void printmedian(household households[]);
int main() {
ifstream inFile;
household households[16];
int i=0;
bool tru;
inFile.open ("C:\\Users\\Student\\Desktop\\8v2input.txt");
if (inFile.fail()) {
cout << "households.txt is not found"<<endl;
return 1;
}
while(!inFile.eof()) {
inFile>>households[i].idcode;
inFile>>households[i].annualincome;
inFile>>households[i].members;
i++;
}
int choice;
do {
tru=true;
cout <<endl<< "OPTIONS ARE:" << endl;
cout << "---------------" << endl;
cout << "1. Print All" << endl;
cout << "2. Print Above Avg" << endl;
cout << "3. Print Below Poverty" << endl;
cout << "4. Print All Sorted by Income" << endl;
cout << "5. Print median Income" << endl;
cout << "6. exit"<< endl;
cout << "Enter Your Choice: ";
cin >> choice;
switch (choice) {
case 1:
printall(households);
break;
case 2:
printaboveavg(households);
break;
case 3:
printbelowpoverty(households);
break;
case 4:
printallsortedincome(households);
break;
case 5:
printmedian(households);
break;
case 6:
cout<<"goodbye"<< endl;
tru=false;
break;
default:
cout << "Wrong choice";
}
}while (tru);
inFile.close();
return 0;
}
void printall(household households[]) {
cout<<"CODE\tINCOME\tMEMBERS"<<endl;
for(int i=0; i < 16; i++) {
cout<<households[i].idcode<<"\t"<< households[i].annualincome<<"\t" << households[i].members<<endl;
}
}
void printaboveavg(household households[]) {
float avgincome = 0;
for(int i=0; i < 16; i++) {
avgincome += households[i].annualincome;
}
avgincome /= 16;
cout<<"CODE\tINCOME\tMEMBERS"<<endl;
for(int i=0; i < 16; i++) {
if(households[i].annualincome >= avgincome)
cout<<households[i].idcode<<"\t"<< households[i].annualincome<<"\t" << households[i].members<<endl;
}
}
void printbelowpoverty(household households[]) {
cout<<"CODE\tINCOME\tMEMBERS"<<endl;
for(int i=0; i < 16; i++) {
float P= 8000.00 + 500.00 * (households[i].members-2);
if(households[i].annualincome < P)
cout<<households[i].idcode<<"\t"<< households[i].annualincome<<"\t" << households[i].members<<endl;
}
}
void printallsortedincome(household households[]) {
for(int i=0; i < 16; i++) {
for(int j=i+1; j < 16; j++) {
if(households[i].annualincome > households[j].annualincome) {
household temp;
temp = households[i];
households[i] = households[j];
households[j] = temp;
}
}
}
cout<<"CODE\tINCOME\tMEMBERS"<<endl;
for(int i=0; i < 16; i++) {
cout<<households[i].idcode<<"\t"<< households[i].annualincome<<"\t" << households[i].members<<endl;
}
}
void printmedian(household households[]) {
for(int i=0; i < 16; i++) {
for(int j=i+1; j < 16; j++) {
if(households[i].annualincome > households[j].annualincome) {
household temp;
temp = households[i];
households[i] = households[j];
households[j] = temp;
}
}
}
float median = (households[7].annualincome + households[8].annualincome)/2;
cout<<"Median: " << median;
}
|