Struct in Functions

Thread Solved

Join Date: Dec 2005
Posts: 1
Reputation: C++ programer is an unknown quantity at this point 
Solved Threads: 0
C++ programer C++ programer is offline Offline
Newbie Poster

Struct in Functions

 
0
  #1
Dec 12th, 2005
Dear all;
i have a problem with functios. i have a program to write using struct in functions with arrays also. i have the program written in structes with array but not using the functions and i don't no who the do it with functions. if anybody acn help i will appreciate it very much. I've been having a big heduce from this problem becuse i don't know how to solve it and also because i only have 1 day to give it for the professor.

  1. include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <iomanip>
  5.  
  6. using namespace std;
  7.  
  8. struct Household
  9. {
  10. int Noperson;
  11. string letters;
  12. double income;
  13. };
  14.  
  15.  
  16. int main()
  17. {
  18. ifstream in;
  19. ofstream out;
  20. int choice_input,counter, counter2,counter3,index,size;
  21. double total,average,plevel,percentage;
  22. Household Survey [50];
  23. Household Survey2 [50];
  24. int temp;
  25. string ctemp;
  26.  
  27.  
  28. in.open("f:\\example.txt");
  29. out.open("f:\\Rfexample.txt");
  30.  
  31. if(!in)
  32. {
  33. cout<<"The file cannot be open."<<endl;
  34.  
  35. return 1;
  36. }
  37.  
  38. counter=0;
  39. while (!in.eof() && counter<50)
  40. {
  41. in>>Survey[counter].letters;
  42. in>>Survey[counter].income;
  43. in>>Survey[counter].Noperson;
  44.  
  45. counter++;
  46. }
  47.  
  48. for(size = 0; size < counter; size++)
  49. {
  50. Survey2[size].letters = Survey[size].letters;
  51. Survey2[size].income = Survey [size].income;
  52. Survey2[size].Noperson = Survey[size].Noperson;
  53. }
  54.  
  55. total = 0;
  56. for(size=0; size < counter; size++)
  57. {
  58. total=Survey[size].income + total;
  59. }
  60.  
  61.  
  62. average= total/counter;
  63.  
  64. for(size=0; size < counter-1; size++)
  65. {
  66. for( index = 0; index < counter - 1 - size;index++)
  67. {
  68. if(Survey2[index].income > Survey2[index+1].income)
  69. {
  70. temp = Survey2[index].income;
  71. Survey2[index].income = Survey2[index+1].income;
  72. Survey2[index+1].income = temp;
  73.  
  74. ctemp = Survey2[index].letters;
  75. Survey2[index].letters = Survey2[index+1].letters;
  76. Survey2[index+1].letters = ctemp;
  77.  
  78. temp = Survey2[index].Noperson;
  79. Survey2[index].Noperson = Survey2[index+1].Noperson;
  80. Survey2[index+1].Noperson = temp;
  81.  
  82.  
  83. }
  84.  
  85. }
  86. }
  87.  
  88.  
  89. out<< fixed << showpoint << setprecision(2);
  90.  
  91. do
  92. {
  93. cout<<"1. Print all of the input data \n";
  94. cout<<"2. Calculate the average household and find whose income is greater than average \n";
  95. cout<<"3. Find the percentage of household income \n";
  96. cout<<"4. Print all of the input data sorted \n";
  97. cout<<"5. Calculate and print the median household \n";
  98. cout<<"6. Quit \n";
  99. cout<<"Please select a choice: ";
  100. cin>>choice_input;
  101.  
  102.  
  103. if(choice_input==1)
  104. {
  105. for(counter2 = 0; counter2 < counter; counter2++)
  106. {
  107.  
  108. out << Survey[counter2].letters<<"\t";
  109. out << Survey[counter2].income<<"\t";
  110. out << Survey[counter2].Noperson<<"\t";
  111. out << endl;
  112.  
  113. cout << Survey[counter2].letters<<"\t";
  114. cout << Survey[counter2].income<<"\t";
  115. cout << Survey[counter2].Noperson<<"\t";
  116. cout << endl;
  117. }
  118. }
  119. if(choice_input==2)
  120. {
  121.  
  122. out<<"\nThe total is: "<<total;
  123. out<<"\nThe average is: "<<average;
  124. out<<endl;
  125.  
  126. cout<<"\nThe total is: "<<total;
  127. cout<<"\nThe average is: "<<average;
  128. cout<<endl;
  129.  
  130. for(size=0; size < counter; size++)
  131.  
  132. {
  133. if( Survey[size].income > average)
  134. {
  135. out <<"\nThe Above averge is:"<<endl;
  136. out << Survey[size].letters<<"\t";
  137. out << Survey[size].income<<"\t";
  138. out << Survey[size].Noperson<<"\t";
  139. out << endl;
  140.  
  141. cout <<"\nThe Above averge is:"<<endl;
  142. cout << Survey[size].letters<<"\t";
  143. cout << Survey[size].income<<"\t";
  144. cout << Survey[size].Noperson<<"\t";
  145. cout << endl;
  146. }
  147. }
  148.  
  149.  
  150.  
  151. }
  152. if(choice_input==3)
  153. {
  154. counter3=0;
  155.  
  156.  
  157. for(size=0; size < counter; size++)
  158. {
  159. plevel=8000.00 + 500.00 * (Survey[size].Noperson - 2);
  160.  
  161. if(Survey[size].income < plevel)
  162. {
  163. counter3++;
  164. }
  165.  
  166. }
  167. percentage= (static_cast<double>(counter3) / counter)*100;
  168. out<<"\nThe percentage is: "<<percentage<<endl;
  169.  
  170. cout<<"\nThe percentage is: "<<percentage<<endl;
  171.  
  172.  
  173. }
  174.  
  175. if(choice_input==4)
  176. {
  177. for(size = 0; size<counter; size++)
  178. {
  179.  
  180. out<<Survey2[size].letters<<"\t";
  181. out<<Survey2[size].income<<"\t";
  182. out<<Survey2[size].Noperson<<"\t";
  183. out << endl;
  184.  
  185. cout<<Survey2[size].letters<<"\t";
  186. cout<<Survey2[size].income<<"\t";
  187. cout<<Survey2[size].Noperson<<"\t";
  188. cout << endl;
  189. }
  190.  
  191. }
  192.  
  193. if(choice_input==5)
  194. {
  195. double median;
  196. median=(Survey2[8].income + Survey2[9].income)/2;
  197. out<<"\nThe median is: "<<median<<endl;
  198.  
  199. cout<<"\nThe median is: "<<median<<endl;
  200.  
  201. }
  202.  
  203.  
  204. if (choice_input <1 || choice_input>6) // to state that choice was wrong in the main menu.
  205. cout<<"\n\n You've entered a wrong choice! Please choose again. \n\n";
  206.  
  207. }while (choice_input !=6 );
  208.  
  209.  
  210.  
  211.  
  212.  
  213. in.close();
  214. out.close();
  215.  
  216. return 0;
  217. }
CAN ANYBODY HELP ME IN THIS PROBLEM
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 178
Reputation: jim mcnamara is on a distinguished road 
Solved Threads: 10
jim mcnamara jim mcnamara is offline Offline
Junior Poster

Re: Struct in Functions

 
0
  #2
Dec 12th, 2005
It's not clear to me what you want. But here is very basic code for structs.
  1. struct Household
  2. {
  3. int Noperson;
  4. char letters[20];
  5. double income;
  6. };
  7. /* find the maximum income */
  8. struct Household *foo(struct Household *src, int cnt)
  9. {
  10. struct Household *p=src;
  11. struct Household *retval=src;
  12. double max = -999999.
  13. int i=0;
  14. while(i<=cnt)
  15. {
  16. if(p->income > max)
  17. {
  18. max=p->income;
  19. retval=p;
  20. }
  21. cnt ++;
  22. p++;
  23. }
  24. return retval;
  25. }
  26.  
  27. int main()
  28. {
  29. struct Household arr[100]={0,NULL,0.}
  30. struct Household *p=arr;
  31. int cnt=0;
  32. int i=0;
  33. for (i=0;i<100; i++, p++)
  34. {
  35. p->Noperson=i;
  36. p->income= i*2.;
  37. strcpy(p-Letters,"Hi there");
  38. cnt++;
  39. }
  40. p=foo(arr,cnt);
  41. printf("Maximum income %f\n",p->income);
  42. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC