Main.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include <cstdlib>
  2. #include <fstream>
  3. #include <string>
  4. #include <cstdio>
  5. #include <iostream>
  6. #include "Address.h"
  7. #include "Date.h"
  8. #include "Student.h"
  9. using namespace std;
  10. const int NUM_STUDENTS = 50;
  11. int main(){
  12. // create arrays to store info
  13. Address * addresses[NUM_STUDENTS];
  14. Date * birthdays[NUM_STUDENTS];
  15. Date * graddays[NUM_STUDENTS];
  16. Student * students[NUM_STUDENTS];
  17. string names[NUM_STUDENTS];
  18. // create objects to read and write files
  19. ifstream fin ("students.dat");
  20. ofstream fullReport ("fullReport.txt");
  21. ofstream shortReport ("shortReport.txt");
  22. ofstream alphaReport ("alphaReport.txt");
  23. // create strings for everything and read the first line
  24. string lname = ""; getline(fin, lname, ',');
  25. string fname = ""; getline(fin, fname, ',');
  26. string line1 = ""; getline(fin, line1, ',');
  27. string line2 = ""; getline(fin, line2, ',');
  28. string city = ""; getline(fin, city, ',');
  29. string state = ""; getline(fin, state, ',');
  30. string zip = ""; getline(fin, zip, ',');
  31. string birthday = ""; getline(fin, birthday, ',');
  32. string gradday = ""; getline(fin, gradday, ',');
  33. string gpa = ""; getline(fin, gpa, ',');
  34. string credits = ""; getline(fin, credits);
  35. int i, j = 0;
  36. // for each student...
  37. for(i = 0; i < NUM_STUDENTS; i++){
  38. // ... read the information, ...
  39. getline(fin, lname, ',');
  40. getline(fin, fname, ',');
  41. getline(fin, line1, ',');
  42. getline(fin, line2, ',');
  43. getline(fin, city, ',');
  44. getline(fin, state, ',');
  45. getline(fin, zip, ',');
  46. getline(fin, birthday, ',');
  47. getline(fin, gradday, ',');
  48. getline(fin, gpa, ',');
  49. getline(fin, credits);
  50. // ... create objects with the information, ...
  51. addresses[i] = new Address(line1, line2, city, state, zip);
  52. birthdays[i] = new Date(birthday);
  53. graddays[i] = new Date(gradday);
  54. students[i] = new Student(fname, lname, birthdays[i],
  55. graddays[i], addresses[i], gpa, credits);
  56. names[i] = students[i]->getLname() + "," + students[i]->getFname();
  57. // ... write the information full report, ...
  58. fullReport << students[i]->getLname() << ","
  59. << students[i]->getFname() << ","
  60. << students[i]->getAddress().getLine1() << ","
  61. << students[i]->getAddress().getLine2() << ","
  62. << students[i]->getAddress().getCity() << ","
  63. << students[i]->getAddress().getState() << ","
  64. << students[i]->getAddress().getZip() << ","
  65. << students[i]->getBirthday().getDate() << ","
  66. << students[i]->getGradday().getDate() << ","
  67. << students[i]->getGpa() << ","
  68. << students[i]->getCredits() << endl;
  69. // ... and write the short report.
  70. shortReport << students[i]->getLname() << ","
  71. << students[i]->getFname() << endl;
  72. }
  73. // bubble sort
  74. string hold = ""; // temporary variable
  75. for(i = 0; i < NUM_STUDENTS - 1; i++){
  76. for(j = 0; j < NUM_STUDENTS - 1; j++){
  77. if(names[j].compare(names[j + 1]) > 0){
  78. hold = names[j];
  79. names[j] = names[j + 1];
  80. names[j + 1] = hold;
  81. }
  82. }
  83. }
  84. for(i = 0; i < NUM_STUDENTS; i++){
  85. alphaReport << names[i] << endl; // write the alphabetical report
  86. delete addresses[i]; // free memory
  87. delete birthdays[i]; // free memory
  88. delete graddays[i]; // free memory
  89. delete students[i]; // free memory
  90. }
  91. // close file streams
  92. fin.close();
  93. fullReport.close();
  94. shortReport.close();
  95. alphaReport.close();
  96. }