Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <iomanip>
- using namespace std;
- const int MAXSIZE='80';
- // ********************************************************************
- // * Struct Name: MCCSR *
- // * Description: Stores the Last name, first name, and survey option *
- // * of each person surveyed. *
- // * Parameter Description: string Fname stores the first name, string*
- // * Lname stores the last name, and char PorC stores pie or *
- // * cheesecake. *
- // * Date: 03/30/2012 *
- // * Author: Reid Richardson *
- // ********************************************************************
- struct MCCSR
- {
- string Fname, Lname;
- char PorC;
- };
- int records(const char * filename);
- char * inputfile(char * filename);
- void sort(struct MCCSR *, int counter);
- void PrintSummary(int count, struct MCCSR *);
- // ********************************************************************
- // * Function Name: int main *
- // * Description: The main routine gathers all of the information and *
- // * allocates memory. It creates a struct to store the first name, *
- // * last name, and survey selection from each person. After doing *
- // * all of that, it calls on the PrintSummary function to print. *
- // * Parameter Description: none *
- // * Date: 03/30/2012 *
- // * Author: Reid Richardson *
- // ********************************************************************
- int main()
- {
- char * fn = new char[MAXSIZE];
- fn = inputfile(fn);
- int cn = 0;
- cn = records(fn);
- struct MCCSR * Survey;
- Survey = new struct MCCSR[cn];
- ifstream MyInputFile;
- MyInputFile.open(fn);
- for(int i=0; i < cn; i++)
- {
- MyInputFile >> Survey[i].Lname;
- MyInputFile >> Survey[i].Fname;
- MyInputFile >> Survey[i].PorC;
- }
- MyInputFile.close();
- PrintSummary(cn, Survey);
- sort(Survey, cn);
- return 0;
- }
- // ********************************************************************
- // * Function Name: int records *
- // * Description: counts the number of lines and returns the value *
- // * Parameter Description: const char * filename dereferences the *
- // * inputted file so the lines can be counted. *
- // * Date: 03/30/2012 *
- // * Author: Reid Richardson *
- // ********************************************************************
- int records(const char * filename)
- {
- int counter = 0;
- char line[MAXSIZE];
- ifstream MyInputFile;
- MyInputFile.open(filename);
- MyInputFile.getline(line, MAXSIZE);
- while(!MyInputFile.eof()) // keeps getting lines until the file ends
- {
- counter++;
- MyInputFile.getline(line, MAXSIZE);
- }
- MyInputFile.close();
- return counter; // returns the numeric value of the amount of lines
- }
- // ********************************************************************
- // * Function Name: char * inputfile *
- // * Description: Prompts the user to input a valid file and then *
- // * stores it for future use by other functions. *
- // * Parameter Description: char * filename is where the user inputs *
- // * the file. *
- // * Date: 03/30/2012 *
- // * Author: Reid Richardson *
- // ********************************************************************
- char * inputfile(char * filename)
- {
- ifstream MyInputFile;
- cout << "Please enter a valid file: ";
- cin >> filename;
- MyInputFile.open(filename);
- while(!MyInputFile.is_open())
- {
- cout << "Invalid File. Please try again: ";
- cin >> filename;
- MyInputFile.open(filename);
- }
- MyInputFile.close();
- return filename;
- }
- // ********************************************************************
- // * Function Name: Sort *
- // * Description: Alphabetically sorts last names *
- // * Parameter Description: struct MCCSR * sur is used with temp to *
- // * move data and int counter is used as a counter *
- // * Date: 03/30/2012 *
- // * Author: Originally written by Dr. David Gaitros, modified by *
- // * Reid Richardson
- // ********************************************************************
- void Sort(struct MCCSR * sur, int counter)
- {
- struct MCCSR temp;
- bool swapped = true;
- while(swapped)
- {
- swapped = false;
- for(int i=0; i < counter-1; i++)
- {
- if(sur[i].Lname > sur[i+1].Lname)
- {
- temp = sur[i];
- sur[i] = sur[i+1];
- sur[i+1] = temp;
- swapped = true;
- }
- }
- }
- }
- // ********************************************************************
- // * Function Name: void PrintSummary *
- // * Description: Prints out the last names, first names, survey *
- // * choices, for each person and displays the total amount of people *
- // * and the total amount of cheesecake/pie options. *
- // * Parameter Description: int counter is used to tally the amount of*
- // * people interviewed and struct MCCSR * sur is used to dereference *
- // * the main struct to have access to the information. *
- // * Date: 03/30/2012 *
- // * Author: Reid Richardson *
- // ********************************************************************
- void PrintSummary(int counter, struct MCCSR * sur)
- {
- cout << endl;
- cout << setw(30) << "The Cheesecake Report" << endl;
- cout << endl;
- cout << setw(5) << "Last Name" << setw(15) << "First Name" << setw(20) << "Cake or Pie" << endl;
- cout << endl;
- int cake=0;
- int pie=0;
- for(int count = 0; count < counter; count++)
- {
- cout << setw(15) << left << sur[count].Lname;
- cout << setw(20) << left << sur[count].Fname;
- if(sur[count].PorC == 'C')
- {
- cout << setw(10) << left << "Cake" << endl;
- cake++;
- }
- if(sur[count].PorC == 'P')
- {
- cout << setw(10) << left << "Pie" << endl;
- pie++;
- }
- }
- cout << "The number of records is: " << counter << endl;
- cout << "The number of people who think CheeseCake is a Pie is: " << pie << endl;
- cout << "The number of people who think CheeseCake is a Cake is: " << cake << endl;
- }
Add Comment
Please, Sign In to add comment