Advertisement
Kelton2

Untitled

Jul 21st, 2015
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.29 KB | None | 0 0
  1. marketingsoftware.cpp (main code):
  2. #include <iostream>
  3. #include <fstream>
  4. #include <vector>
  5. #include <string>
  6. using namespace std;
  7. #include "person.h"
  8.  
  9. Person::Person() {
  10. age = 0;
  11. gender = "default";
  12. yearlyIncome = 0;
  13. return;
  14. }
  15.  
  16. void Person::Print() {
  17. cout << "Age = " << this->age
  18. << ", gender = " << this->gender
  19. << ", yearly income = " << this->yearlyIncome
  20. << endl;
  21. return;
  22. }
  23.  
  24. void Person::SetData(int age, string gender, int yearlyIncome) {
  25. this->age = age;
  26. this->gender = gender;
  27. this->yearlyIncome = yearlyIncome;
  28. return;
  29. }
  30.  
  31. int Person::GetAge() {
  32. return this->age;
  33. }
  34.  
  35. bool ReadPeopleFromFile(int argc, char* argv[], vector<Person> &people) {
  36. Person tmpPrsn;
  37. ifstream inFS;
  38. int tmpAge = 0;
  39. string tmpGender = "";
  40. int tmpYI = 0;
  41.  
  42. if (argc != 2) {
  43. cout << "\nUsage: [EXECUTABLE FILE] [TEXT DATA FILE], e.g. myprog.exe dev_people.txt" << endl;
  44. return true; // indicates error
  45. }
  46.  
  47. cout << "Opening file " << argv[1] << ".\n";
  48. inFS.open(argv[1]); // Try to open file
  49. if (!inFS.is_open()) {
  50. cout << "Could not open file " << argv[1] << ".\n";
  51. return true; // indicates error
  52. }
  53.  
  54. while (!inFS.eof()) {
  55. inFS >> tmpAge;
  56. inFS >> tmpGender;
  57. inFS >> tmpYI;
  58. tmpPrsn.SetData(tmpAge, tmpGender, tmpYI);
  59. tmpPrsn.Print();
  60. people.push_back(tmpPrsn);
  61. }
  62. inFS.close();
  63. cout << "Finished reading file." << endl;
  64.  
  65. return false; // indicates no error
  66. }
  67.  
  68. void GetUserInput(int &ageLowerRange, int&ageUpperRange, string gender2) {
  69. cout << "Enter gender (male, female, or any):";
  70. cin >> gender2;
  71. cout<<"\nEnter lower range of age: ";
  72. cin >> ageLowerRange;
  73.  
  74. cout << "Enter upper range of age: ";
  75. cin >> ageUpperRange;
  76.  
  77. return;
  78. }
  79. vector<Person> GetPeopleInAgeRange(vector<Person> ppl, int lowerRange, int upperRange) {
  80. unsigned int i = 0;
  81.  
  82. vector<Person> pplInAgeRange;
  83. int age = 0;
  84. for (i = 0; i < ppl.size(); ++i) {
  85. age = ppl.at(i).GetAge();
  86. if ((age >= lowerRange) && (age <= upperRange)) {
  87. pplInAgeRange.push_back(ppl.at(i));
  88. }
  89. }
  90.  
  91. return pplInAgeRange;
  92. }
  93.  
  94. int main(int argc, char* argv[]) {
  95. vector<Person> ptntlCstmrs;
  96. bool hadError = false;
  97. int ageLowerRange = 0;
  98. int ageUpperRange = 0;
  99. string gender = "";
  100.  
  101. hadError = ReadPeopleFromFile(argc, argv, ptntlCstmrs);
  102. if( hadError ) {
  103. return 1; // indicates error
  104. }
  105.  
  106. GetUserInput(ageLowerRange, ageUpperRange, gender);
  107. ptntlCstmrs = GetPeopleInAgeRange(ptntlCstmrs, ageLowerRange, ageUpperRange);
  108.  
  109. // FIXME Add the function GetPeopleWithSpecificGender
  110. //FIXME Addthefunction GetPeopleInIncomeRange
  111.  
  112. cout << "\nNumber of potential customers = "<<ptntlCstmrs.size() << endl;
  113.  
  114. return 0;
  115. }
  116. data read from text file:
  117. 20 male 25000
  118. 25 male 45000
  119. 23 male 30000
  120. 16 male 7000
  121. 30 male 55000
  122. 22 female 27000
  123. 26 female 44000
  124. 21 female 37000
  125. 18 female 17000
  126. 29 female 62000
  127. person.cpp (compiled with main file):
  128. #include <string>
  129. using namespace std;
  130. class Person {
  131. public:
  132. Person();
  133. void Print();
  134. void SetData(int a); // FIXME Also set gender and yearly income
  135. int GetAge();
  136. private:
  137. int age;
  138. string gender;
  139. int yearlyIncome;
  140. };
  141. person.h:
  142. class Person {
  143. public:
  144. Person();
  145. void Print();
  146. void SetData(int age, string gender, int yearlyIncome);
  147. int GetAge();
  148. // string gender;
  149. private:
  150. int age;
  151. string gender;
  152. int yearlyIncome;
  153. };
  154. description of what I need done (taken from assignment instructions):
  155. "3. Allow the user to select the potential customer’s gender: “male”, “female”, or “any”. The
  156. program should now output only potential customers with the user­specified gender and age.
  157. Update the GetUserInput function to prompt the user and store the user’s gender selection.
  158. Also, create a function GetPeopleWithSpecificGender that returns only people with the
  159. user­specified gender.
  160. Debugging suggestion: Use a function to print main’s vector of Persons so that you can see who
  161. is in the vector after each function call. This technique may help debug the newly created
  162. function GetPeopleWithSpecificGender.
  163. "
  164. "
  165. 4. In addition to age and gender, allow the user to select the lower and upper range of a
  166. customer’s yearly income.
  167. Update the GetUserInput function to prompt the user and store the user’s specified range.
  168. Also, create a function GetPeopleInIncomeRange that returns only people with the user­specified
  169. yearly income.
  170. "
  171. "
  172. The main should now look like the following code:
  173. intmain(intargc,char*argv[]){
  174. vector<Person>people;
  175. boolhadError=false;
  176. intageLowerRange=0;
  177. intageUpperRange=0;
  178. stringgender="";
  179. intyILowerRange=0;
  180. intyIUpperRange=0;
  181. hadError=ReadPeopleFromFile(argc,argv,people);
  182. if(hadError){
  183. return1;//indicateserror
  184. }
  185. GetUserInput(ageLowerRange,ageUpperRange,gender,yILowerRange,
  186. yIUpperRange);
  187. people=GetPeopleInAgeRange(people,ageLowerRange,ageUpperRange);
  188. people=GetPeopleWithSpecificGender(people,gender);
  189. people=GetPeopleInIncomeRange(people,yILowerRange,yIUpperRange);
  190. cout<<"\nNumberofpotentialcustomers="<<people.size()<<endl;
  191. return0;
  192. }"
  193. (sorry if the above code pasted badly, i'm too lazy to fix all the spaces, blame whoever made the pdf)
  194. "Here is an example program execution with people.txt (user input is highlighted here for clarity):
  195. Openingfilepeople.txt.
  196. Age=20,gender=male,yearlyincome=25000
  197. Age=25,gender=male,yearlyincome=45000
  198. Age=23,gender=male,yearlyincome=30000
  199. Age=16,gender=male,yearlyincome=7000
  200. Age=30,gender=male,yearlyincome=55000
  201. Age=22,gender=female,yearlyincome=27000
  202. Age=26,gender=female,yearlyincome=44000
  203. Age=21,gender=female,yearlyincome=37000
  204. Age=18,gender=female,yearlyincome=17000
  205. Age=29,gender=female,yearlyincome=62000
  206. Finishedreadingfile.
  207. Enterlowerrangeofage:24
  208. Enterupperrangeofage:30
  209. Entergender(male,female,orany):any
  210. Enterlowerrangeofyearlyincome:43000
  211. Enterupperrangeofyearlyincome:57000
  212. Numberofpotentialcustomers=3"
  213. same there.... what the hell is going on with the spaces :|
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement