Advertisement
dragonlord119

Assignment2

Mar 18th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.48 KB | None | 0 0
  1. // DataFileManagement1.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "stdlib.h"
  6. #include "stdio.h"
  7. #include "string.h"
  8. #include "ctype.h"
  9. #include "math.h"
  10. #include "time.h"
  11.  
  12. FILE * infoPointer;
  13.  
  14. typedef struct data
  15.  
  16. {
  17.  
  18. char firstName[50];
  19. char lastName[50];
  20. char companyName[50];
  21. char address[50];
  22. char city[50];
  23. char county[50];
  24. char state[50];
  25. char zip[50];
  26. char phone1[50];
  27. char phone2[50];
  28. char email[50];
  29. char website[50];
  30.  
  31. };
  32.  
  33. struct data personData[500];
  34.  
  35. void OpenFile();
  36. void CloseFile();
  37. void ChooseData();
  38. void SetData(char string[], int option);
  39.  
  40. int main()
  41.  
  42. {
  43.  
  44. OpenFile();
  45. ChooseData();
  46. return 0;
  47.  
  48. }
  49.  
  50. void OpenFile()
  51.  
  52. {
  53.  
  54. infoPointer = fopen("C:\\Users\\CraigBreathnach\\Desktop\\GY350 College\\CT103 Computer Programming\\Semester 2\\Week 5\\DataFile1.txt", "r");
  55.  
  56. if (infoPointer == NULL)
  57.  
  58. {
  59.  
  60. printf("Error! The file was not read correctly. Please check that the file path is correct.\n\n");
  61.  
  62. }
  63.  
  64. else
  65.  
  66. {
  67.  
  68. printf("Success! The file was read correctly. The file path is correct.\n\n");
  69.  
  70.  
  71. }
  72.  
  73. char lines[501];
  74.  
  75. for (int i = 0; i <= 1; i++)
  76.  
  77. {
  78.  
  79. fgets(lines, 87, infoPointer);
  80.  
  81. }
  82.  
  83. char delimiter[2] = ",";
  84. char *token;
  85. int j = 0;
  86.  
  87. while (!feof(infoPointer))
  88.  
  89. {
  90.  
  91. fgets(lines, 501, infoPointer);
  92. //replaceChar(lines, '\"', ' ');
  93.  
  94. if ((token = (strtok(lines, delimiter))) != NULL)
  95.  
  96. {
  97.  
  98. strcpy(personData[j].firstName, token);
  99.  
  100. }
  101.  
  102. if ((token = (strtok(NULL, delimiter))) != NULL)
  103.  
  104. {
  105.  
  106. strcpy(personData[j].lastName, token);
  107.  
  108. }
  109.  
  110. if ((token = (strtok(NULL, delimiter))) != NULL)
  111.  
  112. {
  113.  
  114. strcpy(personData[j].companyName, token);
  115.  
  116. }
  117.  
  118. if ((token = (strtok(NULL, delimiter))) != NULL)
  119.  
  120. {
  121.  
  122. strcpy(personData[j].address, token);
  123.  
  124. }
  125.  
  126. if ((token = (strtok(NULL, delimiter))) != NULL)
  127.  
  128. {
  129.  
  130. strcpy(personData[j].city, token);
  131.  
  132. }
  133.  
  134. if ((token = (strtok(NULL, delimiter))) != NULL)
  135.  
  136. {
  137.  
  138. strcpy(personData[j].county, token);
  139.  
  140. }
  141.  
  142. if ((token = (strtok(NULL, delimiter))) != NULL)
  143.  
  144. {
  145.  
  146. strcpy(personData[j].state, token);
  147.  
  148. }
  149.  
  150. if ((token = (strtok(NULL, delimiter))) != NULL)
  151.  
  152. {
  153.  
  154. strcpy(personData[j].zip, token);
  155.  
  156. }
  157.  
  158. if ((token = (strtok(NULL, delimiter))) != NULL)
  159.  
  160. {
  161.  
  162. strcpy(personData[j].phone1, token);
  163.  
  164. }
  165.  
  166. if ((token = (strtok(NULL, delimiter))) != NULL)
  167.  
  168. {
  169.  
  170. strcpy(personData[j].phone2, token);
  171.  
  172. }
  173.  
  174. if ((token = (strtok(NULL, delimiter))) != NULL)
  175.  
  176. {
  177.  
  178. strcpy(personData[j].email, token);
  179.  
  180. }
  181.  
  182. if ((token = (strtok(NULL, delimiter))) != NULL)
  183.  
  184. {
  185.  
  186. strcpy(personData[j].website, token);
  187.  
  188. }
  189.  
  190. j++;
  191.  
  192. }
  193.  
  194. CloseFile();
  195.  
  196. }
  197.  
  198. void CloseFile()
  199.  
  200. {
  201.  
  202. fclose(infoPointer);
  203.  
  204. }
  205.  
  206. void ChooseData()
  207.  
  208. {
  209.  
  210. int chooseData = 0;
  211. char searchTerm[50];
  212.  
  213. printf("Please choose an option to display the required data : \n1. Search By Name. \n2. Search by Company. \n3. Search by City. \n4. Search by County. \n5. Search by State. \n6. Search by ZIP. \n7. Exit Program. \n\n");
  214. scanf("%d", &chooseData);
  215. printf("\n");
  216.  
  217. switch (chooseData)
  218.  
  219. {
  220.  
  221. case 1:
  222.  
  223. printf("You selected option number 1. \nPlease input a Persons Name to be searched for.\n\nPerson Search Term (No more than 50 characters) : \n\n");
  224. scanf("%s", searchTerm);
  225. SetData(searchTerm, chooseData);
  226. break;
  227.  
  228. case 2:
  229.  
  230. printf("You selected option number 2. \nPlease input a Company Name to be searched for.\n\nCompany Search Term (No more than 50 characters) : \n\n");
  231. scanf("%s", searchTerm);
  232. SetData(searchTerm, chooseData);
  233. break;
  234.  
  235. case 3:
  236.  
  237. printf("You selected option number 3. \nPlease input a City Name to be searched for.\n\nCity Search Term (No more than 50 characters) : \n\n");
  238. scanf("%s", searchTerm);
  239. SetData(searchTerm, chooseData);
  240. break;
  241.  
  242. case 4:
  243.  
  244. printf("You selected option number 4. \nPlease input a County Name to be searched for.\n\nCounty Search Term (No more than 50 characters) : \n\n");
  245. scanf("%s", searchTerm);
  246. SetData(searchTerm, chooseData);
  247. break;
  248.  
  249. case 5:
  250.  
  251. printf("You selected option number 5. \nPlease input a State Name to be searched for.\n\nState Search Term (No more than 50 characters) : \n\n");
  252. scanf("%s", searchTerm);
  253. SetData(searchTerm, chooseData);
  254. break;
  255.  
  256. case 6:
  257.  
  258. printf("You selected option number 6. \nPlease input a ZIP Code to be searched for.\n\nZIP Search Term (No more than 50 characters) : \n\n");
  259. scanf("%s", searchTerm);
  260. SetData(searchTerm, chooseData);
  261. break;
  262.  
  263. default:
  264.  
  265. printf("You selected option number 7. The program will now end. Goodbye. \n\n");
  266. exit;
  267.  
  268. }
  269.  
  270. }
  271.  
  272. void SetData(char string[], int option)
  273.  
  274. {
  275.  
  276.  
  277. if (option == 1)
  278.  
  279. {
  280.  
  281. for (int k = 0; k <= 499; k++)
  282.  
  283. {
  284.  
  285. if ((strstr(_strupr(personData[k].firstName), _strupr(string))) || (strstr(_strupr(personData[k].lastName), _strupr(string))))
  286.  
  287. {
  288.  
  289.  
  290. printf("Search Term : %s \n\nFirst Name : %s \nLast Name : %s \nCompany Name : %s \nAddress : %s \nCity : %s \nCounty : %s \nState : %s \nZip : %s \nPhone 1 : %s \nPhone 2 : %s \nEmail : %s \nWebsite : %s \n",
  291. string, personData[k].firstName, personData[k].lastName, personData[k].companyName, personData[k].address, personData[k].city, personData[k].county,
  292. personData[k].state, personData[k].zip, personData[k].phone1, personData[k].phone2, personData[k].email, personData[k].website);
  293.  
  294. }
  295.  
  296. }
  297.  
  298. }
  299.  
  300. else if (option == 2)
  301.  
  302. {
  303.  
  304. for (int l = 0; l <= 499; l++)
  305.  
  306. {
  307.  
  308. if (strstr(_strupr(personData[l].companyName), _strupr(string)))
  309.  
  310. {
  311.  
  312. printf("Search Term : %s \n\nFirst Name : %s \nLast Name : %s \nCompany Name : %s \nAddress : %s \nCity : %s \nCounty : %s \nState : %s \nZip : %s \nPhone 1 : %s \nPhone 2 : %s \nEmail : %s \nWebsite : %s \n",
  313. string, personData[l].firstName, personData[l].lastName, personData[l].companyName, personData[l].address, personData[l].city, personData[l].county,
  314. personData[l].state, personData[l].zip, personData[l].phone1, personData[l].phone2, personData[l].email, personData[l].website);
  315.  
  316. }
  317.  
  318. }
  319.  
  320. }
  321.  
  322. else if (option == 3)
  323.  
  324. {
  325.  
  326. for (int m = 0; m <= 499; m++)
  327.  
  328. {
  329.  
  330. if (strstr(_strupr(personData[m].city), _strupr(string)))
  331.  
  332. {
  333.  
  334. printf("Search Term : %s \n\nFirst Name : %s \nLast Name : %s \nCompany Name : %s \nAddress : %s \nCity : %s \nCounty : %s \nState : %s \nZip : %s \nPhone 1 : %s \nPhone 2 : %s \nEmail : %s \nWebsite : %s \n",
  335. string, personData[m].firstName, personData[m].lastName, personData[m].companyName, personData[m].address, personData[m].city, personData[m].county,
  336. personData[m].state, personData[m].zip, personData[m].phone1, personData[m].phone2, personData[m].email, personData[m].website);
  337.  
  338. }
  339.  
  340. }
  341.  
  342. }
  343.  
  344. else if (option == 4)
  345.  
  346. {
  347.  
  348. for (int n = 0; n <= 499; n++)
  349.  
  350. {
  351.  
  352. if (strstr(_strupr(personData[n].county), _strupr(string)))
  353.  
  354. {
  355.  
  356. printf("Search Term : %s \n\nFirst Name : %s \nLast Name : %s \nCompany Name : %s \nAddress : %s \nCity : %s \nCounty : %s \nState : %s \nZip : %s \nPhone 1 : %s \nPhone 2 : %s \nEmail : %s \nWebsite : %s \n",
  357. string, personData[n].firstName, personData[n].lastName, personData[n].companyName, personData[n].address, personData[n].city, personData[n].county,
  358. personData[n].state, personData[n].zip, personData[n].phone1, personData[n].phone2, personData[n].email, personData[n].website);
  359.  
  360. }
  361.  
  362. }
  363.  
  364. }
  365.  
  366. else if (option == 5)
  367.  
  368. {
  369.  
  370. for (int o = 0; o <= 499; o++)
  371.  
  372. {
  373.  
  374. if (strstr(_strupr(personData[o].state), _strupr(string)))
  375.  
  376. {
  377.  
  378. printf("Search Term : %s \n\nFirst Name : %s \nLast Name : %s \nCompany Name : %s \nAddress : %s \nCity : %s \nCounty : %s \nState : %s \nZip : %s \nPhone 1 : %s \nPhone 2 : %s \nEmail : %s \nWebsite : %s \n",
  379. string, personData[o].firstName, personData[o].lastName, personData[o].companyName, personData[o].address, personData[o].city, personData[o].county,
  380. personData[o].state, personData[o].zip, personData[o].phone1, personData[o].phone2, personData[o].email, personData[o].website);
  381.  
  382. }
  383.  
  384. }
  385.  
  386. }
  387.  
  388. else if (option == 6)
  389.  
  390. {
  391.  
  392. for (int p = 0; p <= 499; p++)
  393.  
  394. {
  395.  
  396. if (strstr(_strupr(personData[p].zip), _strupr(string)))
  397.  
  398. {
  399.  
  400. printf("Search Term : %s \n\nFirst Name : %s \nLast Name : %s \nCompany Name : %s \nAddress : %s \nCity : %s \nCounty : %s \nState : %s \nZip : %s \nPhone 1 : %s \nPhone 2 : %s \nEmail : %s \nWebsite : %s \n",
  401. string, personData[p].firstName, personData[p].lastName, personData[p].companyName, personData[p].address, personData[p].city, personData[p].county,
  402. personData[p].state, personData[p].zip, personData[p].phone1, personData[p].phone2, personData[p].email, personData[p].website);
  403.  
  404. }
  405.  
  406. }
  407.  
  408. }
  409.  
  410. else
  411.  
  412. {
  413.  
  414. printf("Error! The program will now close. \n\n");
  415. exit;
  416.  
  417. }
  418.  
  419. ChooseData();
  420.  
  421. }
  422.  
  423. /*
  424.  
  425. fgetc used to replace a character when reading charater by character.
  426.  
  427. For Replacing Characters :
  428.  
  429. void replaceChar(char *string, char oldC, char newC)
  430.  
  431. {
  432.  
  433. int i = 0;
  434.  
  435. while(*(string + i) != '\0')
  436.  
  437. {
  438.  
  439. if (*(string + i) == oldC)
  440.  
  441. {
  442.  
  443. *(string + i) = newC;
  444.  
  445. }
  446.  
  447. i++;
  448.  
  449. }
  450.  
  451. use arrow -> to allocate structure pointer to value without dereferencing.
  452. e.g. *(string + 1) = pointer[0].pointer;
  453. now is (string + 1)->pointer[0].pointer;
  454.  
  455. char ** B (A double pointer; a pointer address pointing to another pointer address.);
  456.  
  457.  
  458.  
  459. }
  460.  
  461. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement