Hi
i am new guy in programing and i need ur help in this program if u can. my problem is that we have to write a program to read a file usinag a struct array and a function and the assigment is Print all of the input data ; Calculate the average household and find whose income is greater than average ; Find the percentage of income ; Print all of the input data sorted ; Calculate and print the median. i did all of that without using any function and i don't know how to do it with functions. Here is my code.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
struct Household
{
int Noperson;
string letters;
double income;
};
int main()
{
ifstream in;
ofstream out;
int choice_input,counter, counter2,counter3,index,size;
double total,average,plevel,percentage;
Household Survey [50];
Household Survey2 [50];
int temp;
string ctemp;
in.open("f:\\example.txt");
out.open("f:\\Rfexample.txt");
if(!in)
{
cout<<"The file cannot be open."<<endl;
return 1;
}
counter=0;
while (!in.eof() && counter<50)
{
in>>Survey[counter].letters;
in>>Survey[counter].income;
in>>Survey[counter].Noperson;
counter++;
}
for(size = 0; size < counter; size++)
{
Survey2[size].letters = Survey[size].letters;
Survey2[size].income = Survey [size].income;
Survey2[size].Noperson = Survey[size].Noperson;
}
total = 0;
for(size=0; size < counter; size++)
{
total=Survey[size].income + total;
}
average= total/counter;
for(size=0; size < counter-1; size++)
{
for( index = 0; index < counter - 1 - size;index++)
{
if(Survey2[index].income > Survey2[index+1].income)
{
temp = Survey2[index].income;
Survey2[index].income = Survey2[index+1].income;
Survey2[index+1].income = temp;
ctemp = Survey2[index].letters;
Survey2[index].letters = Survey2[index+1].letters;
Survey2[index+1].letters = ctemp;
temp = Survey2[index].Noperson;
Survey2[index].Noperson = Survey2[index+1].Noperson;
Survey2[index+1].Noperson = temp;
}
}
}
out<< fixed << showpoint << setprecision(2);
do
{
cout<<"1. Print all of the input data \n";
cout<<"2. Calculate the average household and find whose income is greater than average \n";
cout<<"3. Find the percentage of household income \n";
cout<<"4. Print all of the input data sorted \n";
cout<<"5. Calculate and print the median household \n";
cout<<"6. Quit \n";
cout<<"Please select a choice: ";
cin>>choice_input;
if(choice_input==1)
{
for(counter2 = 0; counter2 < counter; counter2++)
{
out << Survey[counter2].letters<<"\t";
out << Survey[counter2].income<<"\t";
out << Survey[counter2].Noperson<<"\t";
out << endl;
cout << Survey[counter2].letters<<"\t";
cout << Survey[counter2].income<<"\t";
cout << Survey[counter2].Noperson<<"\t";
cout << endl;
}
}
if(choice_input==2)
{
out<<"\nThe total is: "<<total;
out<<"\nThe average is: "<<average;
out<<endl;
cout<<"\nThe total is: "<<total;
cout<<"\nThe average is: "<<average;
cout<<endl;
for(size=0; size < counter; size++)
{
if( Survey[size].income > average)
{
out <<"\nThe Above averge is:"<<endl;
out << Survey[size].letters<<"\t";
out << Survey[size].income<<"\t";
out << Survey[size].Noperson<<"\t";
out << endl;
cout <<"\nThe Above averge is:"<<endl;
cout << Survey[size].letters<<"\t";
cout << Survey[size].income<<"\t";
cout << Survey[size].Noperson<<"\t";
cout << endl;
}
}
}
if(choice_input==3)
{
counter3=0;
for(size=0; size < counter; size++)
{
plevel=8000.00 + 500.00 * (Survey[size].Noperson - 2);
if(Survey[size].income < plevel)
{
counter3++;
}
}
percentage= (static_cast<double>(counter3) / counter)*100;
out<<"\nThe percentage is: "<<percentage<<endl;
cout<<"\nThe percentage is: "<<percentage<<endl;
}
if(choice_input==4)
{
for(size = 0; size<counter; size++)
{
out<<Survey2[size].letters<<"\t";
out<<Survey2[size].income<<"\t";
out<<Survey2[size].Noperson<<"\t";
out << endl;
cout<<Survey2[size].letters<<"\t";
cout<<Survey2[size].income<<"\t";
cout<<Survey2[size].Noperson<<"\t";
cout << endl;
}
}
if(choice_input==5)
{
double median;
median=(Survey2[8].income + Survey2[9].income)/2;
out<<"\nThe median is: "<<median<<endl;
cout<<"\nThe median is: "<<median<<endl;
}
if (choice_input <1 || choice_input>6) // to state that choice was wrong in the main menu.
cout<<"\n\n You've entered a wrong choice! Please choose again. \n\n";
}while (choice_input !=6 );
in.close();
out.close();
return 0;
}
please help because i really need ur help.......
THANK FOR UR HELP