Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************
- *Program Name: Cheesecake Survey *
- *Class: COP3014 *
- *Description: Inputs and sorts a file *
- *alphabetically, prints a summary of the *
- *survey. *
- *Date: 11/18/2011 *
- *Author: Ashley Suh *
- *Sources: Dr. Gaitros *
- ******************************************/
- #include <iostream>
- #include <iomanip>
- #include <fstream>
- #include <string>
- using namespace std;
- //Prototypes
- const int MAXSIZE=80;
- struct Roster
- {
- string Lname;
- string Fname;
- char Cheesecake;
- };
- string getfilename();
- int countrecords (string);
- void readsurvey (const int, const string, struct Roster *);
- void sort (struct Roster *, const int);
- void printsummary (const int, struct Roster *);
- int main (void)
- {
- //1. Get a valid file name
- string filename;
- filename = new char[MAXSIZE];
- filename = getfilename();
- //2. Count the # of records
- int recordcount = countrecords(filename);
- cout << "Got " << recordcount << " records" << endl;
- //3. Allocate the space for the records
- struct Roster * MySurvey = new struct Roster[recordcount];
- //4. Read the records into the structure
- readsurvey (recordcount, filename, MySurvey);
- //5. Sort the records
- sort (MySurvey, recordcount);
- //6. Print report
- printsummary (recordcount, MySurvey);
- system("pause");
- return 0;
- }
- /******************************************
- *Description: Function opening the file *
- *and checks whether or not the file exists*
- *Author: Ashley Suh *
- ******************************************/
- string getfilename ()
- {
- string filename;
- ifstream myfile;
- //Get file name
- cout << "Please enter a file name: ";
- cin >> filename;
- myfile.open(filename.c_str());
- //loop to check if file exists
- while (!myfile.is_open())
- {
- cout << "Enter it again: ";
- cin >> filename;
- myfile.open(filename.c_str());
- }
- myfile.close(); //close file
- return filename;
- }
- /******************************************
- *Description: counts the amount of records*
- *in the file. *
- *Author: Ashley Suh *
- ******************************************/
- int countrecords (const string fn)
- {
- //counts amount of records in file
- int count = 0;
- char inputline [MAXSIZE];
- ifstream myfile;
- myfile.open(fn);
- myfile.getline(inputline,80);
- //loop to count lines when file is not complete
- while (!myfile.eof())
- {
- count++;
- myfile.getline(inputline,80);
- }
- myfile.close();
- return count;
- }
- /******************************************
- *Description: reads in the file and *
- *creates a copy. *
- *Author: Ashley Suh *
- ******************************************/
- void readsurvey (const int rc, const string fn, struct Roster * ms)
- {
- //opens the file
- ifstream myfile;
- myfile.open(fn.c_str());
- //copies the file in MySurvey
- for (int index = 0; index < rc; index++)
- {
- myfile >> ms[index].Lname;
- myfile >> ms[index].Fname;
- myfile >> ms[index].Cheesecake;
- }
- //closes the file
- myfile.close();
- }
- /******************************************
- *Description: Sorts the file for names *
- *in alphabetical order. *
- *Author: Ashley Suh *
- ******************************************/
- void sort (struct Roster * ms, const int rc)
- {
- bool swapped = true;
- //while loop where sorting will continue
- while(swapped)
- {
- //if swapping is unnecesary, sets to false
- swapped = false;
- //swap places in file if not in alphabetical order
- for(int i = 0; i < rc - 1; i++)
- if (ms[i].Lname > ms[i+1].Lname)
- {
- //swaps
- Roster temp = ms[i];
- ms[i] = ms[i+1];
- ms[i+1] = temp;
- swapped = true;
- }
- }
- }
- /*******************************************
- *Description: Prints the records of the *
- *file, including the number of participants*
- *and amount of people who chose cake or pie*
- *Author: Ashley Suh *
- *******************************************/
- void printsummary (const int rc, struct Roster * ms)
- {
- int cake = 0;
- int pie = 0;
- //survey header
- cout << setw(15) << left << "Cheesecake Survey" << endl;
- cout << setw(15) << left << "Last Name" ;
- cout << setw(15) << left << "First Name" ;
- cout << setw(15) << left << "Cake or Pie" << endl;
- cout << "-----------------------------------------" << endl;
- //displays sorted file
- for(int i = 0; i < rc; i++)
- {
- cout << setw(15) << left << ms[i].Lname;
- cout << setw(15) << left << ms[i].Fname;
- if (ms[i].Cheesecake == 'C')
- {
- cake++; //counter for cake choices
- cout << setw(10) << left << "Cake" << endl;
- }
- else
- {
- pie++; //counter for pie choices
- cout << setw(10) << left << "Pie" << endl;
- }
- }
- cout << "Number of people who believe Cheesecake is a cake: " << cake << endl;
- cout << "Number of people who believe Cheesecake is a pie: " << pie << endl;
- cout << "Total number of survey participants: " << rc << endl;
- }
Add Comment
Please, Sign In to add comment