Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <fstream>
- #include <string>
- using namespace std;
- // Function prototypes
- bool validFilename(const string filename);
- void readData(Roster[] &MyRoster, const int lines, ifstream &fp);
- void sortData();
- int isPie();
- int isCake();
- void sortArray();
- void printReport();
- // Structs
- struct Roster {
- string LName;
- string FName;
- char Cheesecake;
- };
- struct Roster * MyRoster;
- // Illegal characters for file names, according to the NTFS specification
- // Characters ranging from dec 1 through 31 inclusive are not listed here
- // for brevity.
- const char illegalchars[] = { '"', '*', ':', '<', '>', '?', '\\', '/', '|' };
- int main (void){
- char filename[80];
- ifstream file;
- // Some bool variables for the do while input loop
- bool isvalid = true; // Will change to false if failing the first loop
- bool isopened = false;
- bool failedopen = false;
- do {
- if ( failedopen ){
- cout << "There was an error opening the file." << endl;
- }
- isvalid = true; // Reset for each loop
- cout << "Enter filename: ";
- cin.width(80);
- cin >> filename;
- isvalid = validFilename(filename);
- // If the filename is valid, try to open the file
- if(isvalid){
- file.open(filename);
- if ( file.fail() ){
- // File was not opened
- isopened = false;
- }
- else {
- // File was opened;
- isopened = true;
- }
- }
- } while ( !isvalid || !isopened );
- // Count the number of lines to determine how much space to allocate for array
- int lines;
- while ( file.getline() ){
- lines++;
- }
- // Close file
- file.close();
- // Dynamically allocate an array with exactly the amount of storage required
- // to hold the records.
- MyRoster = new Roster[lines];
- // Open the file and read it into the array
- file.open(filename, 'r');
- readData(MyRoster, file, lines);
- // Sort the array by last name in ascending order
- // Output the array records
- // Output the summary
- // Output the number of participants
- // Check that the votes for either cheesecake or pie add up to the total number of votes
- }
- /* Function to valid filename
- *
- * Arguments: filename string
- *
- * Returns false if any character in the filename string matches any chracter
- * in the illegalchars array, or if any character in the filename is an
- * ASCII character between 1 and 31.
- */
- bool validFilename(const string filename){
- for (int i = 0 ; i < filename.length() ; i++){
- // First check against array
- for (int j = 0 ; j < 9 ; j++){
- if( filename.at(i) == illegalchars[j] ){
- return false;
- }
- }
- // Then check to see if the character is an ASCII character between 1 and 31
- if ( (int) filename > 0 && (int) filename < 32 ){
- return false;
- }
- }
- // Since nothing matched, string is good.
- return true;
- }
- void readData(const Roster[] &MyRoster, const int lines, ifstream &fp){
- for(int i=0 ; i < lines - 1; i++){
- fin >> MyRoster[i].LName;
- fin >> MyRoster[i].FName;
- fin >> MyRoster[i].Cheesecake;
- }
- }
Add Comment
Please, Sign In to add comment