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

What is(are) your specific question(s)? The use of functions is a large topic. For example:

Functions do something to something and return something. The name of the function is the key to keeping track of it all. You need to declare the function before you use it. This is often done outside main with a prototype declaration either as a stand alone function or as a method of a class/struct. You can either define the function at the same time as you declare it or define it later. If the function is free standing the prototype often precedes main() and the definition often follows main(). If the function is a class/struct method, then it can be defined before main() with the class, or if you use modular development, it can be defined in the header file (frequently done with methods you want to inline) or in the cpp file (more often that the other way). You need to decide which of these options is best for you.

The prototype declaration and definitions must follow certain syntax requirements. If you don't know what that involves let us know.

In your case, you could use a function to display the menu, a function to read the file and load the arrays. A function to calculate the averages. A function to display results. Etc. Etc. You should work on just one function at a time. Decide what action you want to do to what objects and what type of a result you want to achieve. Try to write function that does that to replace a specific section of your code, and then post specific question(s) so we can help you better. If you don't know how to use/write functions in general, try to demonstrate your knowledge so we don't spend a lot of time telling you things you already know.

Thank u Lerner for ur time to look to my project.
the thing that i don't know hiow to do it is the whole (Function) how to write it and to put in the the program. the teacher didn't explain the function that good so i didn't understand the functions good.
That program worked without functions but i don't know how to do it in functions, so if u can help to do this program with functions.

Please can some one help me. Because i can't figure out how to do it in functions.

Can som eone help me please

Thank u

functions are just small sections of code that perform a specific task. For example, you might have a function called menu() that will display a menu, prompt the user for the menu item and return the menu item number that was selected. Below is an illustration of how to write the menu and use it in main().

int menu()
{
   int menu_item = 0;
   // display menu is not shown here
   printf("Enter menu item number");
   scanf("%d", &menu_item);
   return menu_item;
}

int main()
{
   int menu_item;
   menu_item = menu();
   // blabla

   return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.