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;
- char * getfilename(char *);
- int countrecords(char *);
- void printsummary(const int,const struct Roster *, int &pc, int &cc);
- void readsurvey(const int,const char *,struct Roster *, int &pc, int &cc);
- void bubble(struct Roster * sv, int rc);
- struct Roster
- {
- string LName;
- string FName;
- char Cheesecake;
- };
- int main(void)
- {
- struct Roster * MySurvey;
- char * filename;
- filename = new char[MAXSIZE];
- int recordCount = 0, pieCount = 0, cakeCount = 0;
- filename = getfilename(filename); //step 1: Get a valid filename
- recordCount = countrecords(filename); //step 2: Count the number of records
- MySurvey = new struct Roster[recordCount]; //step 3: Allocate space for the records
- readsurvey(recordCount,filename,MySurvey, pieCount, cakeCount); //step 4: Read the records into the structure
- bubble(MySurvey, recordCount); //step 5: Bubblesort the records
- printsummary(recordCount, MySurvey, pieCount, cakeCount); //step 6: Print report
- return 0;
- }
- char * getfilename(char * fn)
- {
- //Get file to read
- ifstream myfile;
- cout << "Please enter a file name: ";
- fn = "myfile.txt";
- cout << endl;
- myfile.open(fn);
- while(!myfile.is_open())
- {
- cout << "Filename rejected. Try again:";
- cin >> fn;
- myfile.open(fn);
- }
- myfile.close();
- return fn;
- }
- int countrecords(char * fn)
- {
- int count = 0;
- char inputline[MAXSIZE];
- ifstream myfile;
- myfile.open(fn);
- myfile.getline(inputline,80);
- //Count records recursively
- while(!myfile.eof())
- {
- count++;
- myfile.getline(inputline,80);
- }
- myfile.close();
- return count;
- }
- void readsurvey(const int rc, const char * fn, struct Roster * sv, int &pc, int &cc)
- {
- ifstream myfile;
- myfile.open(fn);
- for (int index = 0; index <rc; index ++)
- {
- myfile >> sv[index].LName;
- myfile >> sv[index].FName;
- myfile >> sv[index].Cheesecake;
- //Tally votes
- if (sv[index].Cheesecake == 'P')
- pc++;
- if (sv[index].Cheesecake == 'C')
- cc++;
- }
- myfile.close();
- }
- void printsummary (const int rc, const struct Roster * sv, int &pc, int &cc)
- {
- string pref;
- for (int index = 0; index < rc; index ++) {
- if(sv[index].Cheesecake == 'P')
- pref = "Pie";
- else if(sv[index].Cheesecake == 'C')
- pref = "Cake";
- //Print left justified last and first names, and preference
- cout << left << setw(12) << sv[index].LName << " " <<
- setw(12) << sv[index].FName << " " <<
- setw(4) <<" "<< pref << endl;
- }
- //Print final statistics
- cout << "Number of records is: " << rc << endl;
- cout << "Number of pie-thusiasts is: " << pc << endl;
- cout << "Number of cake-cultists is: " << cc << endl;
- }
- //Standard Bubble Sort
- void bubble(struct Roster * sv,int rc)
- {
- //Temporary Roster container object
- struct Roster hold;
- for(int x = 0; x < rc; x++)
- {
- for(int y = 0; y < rc-1; y++)
- {
- //Compare LNames
- if(sv[y].LName > sv[y+1].LName)
- {
- //But manipulate the containers themselves,
- //otherwise a bug will occur where the last names
- //would be sorted but not the rest of the data.
- hold = sv[y+1];
- sv[y+1] = sv[y];
- sv[y] = hold;
- }
- }
- }
- }
- /* GOTTA SORT LIKA NIGGA. YESA.
- use 'rc' for total recordCount
- to change 'P' and 'C' to Pie and Cake;
- before previous functions of cout simply add an if statement saying something
- like 'if P then =Pie, if C then = Cake or whatever' */
Add Comment
Please, Sign In to add comment