Guest User

Untitled

a guest
Mar 17th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. // Function prototypes
  8. bool validFilename(const string filename);
  9. void readData(Roster[] &MyRoster, const int lines, ifstream &fp);
  10. void sortData();
  11. int isPie();
  12. int isCake();
  13. void sortArray();
  14. void printReport();
  15.  
  16. // Structs
  17.  
  18. struct Roster {
  19. string LName;
  20. string FName;
  21. char Cheesecake;
  22. };
  23. struct Roster * MyRoster;
  24.  
  25. // Illegal characters for file names, according to the NTFS specification
  26. // Characters ranging from dec 1 through 31 inclusive are not listed here
  27. // for brevity.
  28. const char illegalchars[] = { '"', '*', ':', '<', '>', '?', '\\', '/', '|' };
  29.  
  30. int main (void){
  31.  
  32. char filename[80];
  33. ifstream file;
  34.  
  35. // Some bool variables for the do while input loop
  36. bool isvalid = true; // Will change to false if failing the first loop
  37. bool isopened = false;
  38. bool failedopen = false;
  39.  
  40. do {
  41. if ( failedopen ){
  42. cout << "There was an error opening the file." << endl;
  43. }
  44. isvalid = true; // Reset for each loop
  45.  
  46. cout << "Enter filename: ";
  47. cin.width(80);
  48. cin >> filename;
  49.  
  50. isvalid = validFilename(filename);
  51.  
  52. // If the filename is valid, try to open the file
  53. if(isvalid){
  54. file.open(filename);
  55.  
  56. if ( file.fail() ){
  57. // File was not opened
  58. isopened = false;
  59. }
  60. else {
  61. // File was opened;
  62. isopened = true;
  63. }
  64. }
  65. } while ( !isvalid || !isopened );
  66.  
  67. // Count the number of lines to determine how much space to allocate for array
  68. int lines;
  69. while ( file.getline() ){
  70. lines++;
  71. }
  72.  
  73. // Close file
  74. file.close();
  75.  
  76. // Dynamically allocate an array with exactly the amount of storage required
  77. // to hold the records.
  78. MyRoster = new Roster[lines];
  79.  
  80. // Open the file and read it into the array
  81. file.open(filename, 'r');
  82. readData(MyRoster, file, lines);
  83.  
  84. // Sort the array by last name in ascending order
  85.  
  86. // Output the array records
  87.  
  88. // Output the summary
  89.  
  90. // Output the number of participants
  91.  
  92. // Check that the votes for either cheesecake or pie add up to the total number of votes
  93.  
  94.  
  95. }
  96.  
  97. /* Function to valid filename
  98. *
  99. * Arguments: filename string
  100. *
  101. * Returns false if any character in the filename string matches any chracter
  102. * in the illegalchars array, or if any character in the filename is an
  103. * ASCII character between 1 and 31.
  104. */
  105. bool validFilename(const string filename){
  106. for (int i = 0 ; i < filename.length() ; i++){
  107. // First check against array
  108. for (int j = 0 ; j < 9 ; j++){
  109. if( filename.at(i) == illegalchars[j] ){
  110. return false;
  111. }
  112. }
  113. // Then check to see if the character is an ASCII character between 1 and 31
  114. if ( (int) filename > 0 && (int) filename < 32 ){
  115. return false;
  116. }
  117. }
  118. // Since nothing matched, string is good.
  119. return true;
  120. }
  121.  
  122. void readData(const Roster[] &MyRoster, const int lines, ifstream &fp){
  123. for(int i=0 ; i < lines - 1; i++){
  124. fin >> MyRoster[i].LName;
  125. fin >> MyRoster[i].FName;
  126. fin >> MyRoster[i].Cheesecake;
  127. }
  128. }
Add Comment
Please, Sign In to add comment