#include <iostream>
#include <iomanip>
#include <fstream>

const int NUM_SURVEY=3;
using namespace std;


struct Survey
{
  int IdentificationNumber;
  double annualIncome;
  int numberOfHouseMember;
};
void getData();
void calculateAverage();
void findPoverty();
void printReport();

int main()
{

 getData();
 calculateAverage();
 findPoverty();
 printReport();
 system("pause");
 return 0;
}
void getData()
{

Survey Households[NUM_SURVEY];
int i;

cout<<"------------Enter " << NUM_SURVEY<<" Household Data.-----------------\n";
cout<<endl;

for(i=0;i<NUM_SURVEY;i++)
{
   //Get Identification
   cout<<"Enter Family "<<(i+1)<<" data.\n"<<endl;
   cout<<"Enter Identification Number "<<": ";
   cin>>Households[i].IdentificationNumber;
   //Get annuall income
   cout<<"Enter Annual Income "<<": RM ";
   cin>>Households[i].annualIncome;
   //Get number of house member
   cout<<"Enter member of House "<<": ";
   cin>>Households[i].numberOfHouseMember;
   cout<<endl;
}
}
void calculateAverage()
{
     Survey Households[NUM_SURVEY];
     int i;
     cout<<"The average of Houshold income is:\n";
     cout<< fixed << showpoint <<setprecision(2);
for(i=0;i<NUM_SURVEY;i++)
{ 
   double average;
   average=Households[i].annualIncome/Households[i].numberOfHouseMember;
   cout<<"Average Household Income Family "<<(i+1)<<": RM"<<average;
   cout<<endl;
}
}
void findPoverty()
{
     Survey Households[NUM_SURVEY];
     int i;
     cout<<"\nThe poverty level is :\n";
     cout<< fixed << showpoint <<setprecision(2);
for(i=0;i<NUM_SURVEY;i++)
{
  double p;
  p=7000+850*(Households[i].numberOfHouseMember-2);
  cout<<"Percentage of poverty Family "<<(i+1)<<": RM"<<p<<endl;
}
}
void printReport()
{
Survey Households[NUM_SURVEY];
int i;



cout<<"\n----------------------Survey of Household Income Report----------------------\n\n";

cout<<setw(25)<<"Identification Number"
    <<setw(25)<<"Annual Income "
    <<setw(25)<<"Household Member\n";
cout<<"_____________________________________________________________________________"<<endl;
cout<< fixed << showpoint <<setprecision(2);
for(i=0;i<NUM_SURVEY;i++)
{
 cout<<setw(15)<<Households[i].IdentificationNumber
     <<setw(30)<<Households[i].annualIncome
     <<setw(20)<<Households[i].numberOfHouseMember<<endl;
}

cout<<endl;
for(i=0;i<NUM_SURVEY;i++)
{
  double p;
  p=7000+850*(Households[i].numberOfHouseMember-2);
  cout<<"Percentage of poverty Family "<<(i+1)<<": RM"<<p<<endl;
}
}

What is your Operating System?

If you're using Linux then you have the g++ compiler installed, just open the terminal and type g++ "codefile.cpp".

If you're using Windows then you can download Microsoft Visual C++ Express Edition for free which has a built-in C++ compiler, or you can download MinGW here.

Is there some confusion here? I thought that the OP was asking how the program could be made to save some data to a binary file, not how to compile it.

Admittedly, the OP could do more to explain what the issue is and what they would like to do...

yes i asked about how to save this to binary not to compile,..help about the command i should add to this program.

Oops I'm so sorry, I thought that you are asking about how to convert the code into a binary file (compiling it) :)

If you want to write binary data to a file use ostream::write function like the follows:

class Data
{
    int n;
    double d;
    string s;
};

// You can use the write function as follows:

ofstream file("example");
Data d;
file.write(reinterpret_cast<const char*>(&d), sizeof(d))

More information can be found at: ostream::write and reinterpret_cast

This article has been dead for over six months. Start a new discussion instead.