Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ****************************************************
- // ****************************************************
- // * Created by: Andreas Adolfsson *
- // * Student ID: ada10j *
- // * Date: 3/30 *
- // ****************************************************
- // ****************************************************
- #define MAXSIZE 80
- #include <iostream>
- #include <string>
- #include <iomanip>
- #include <fstream>
- using namespace std;
- struct Roster {
- string LName;
- string FName;
- char Cheesecake;
- };
- string FileNameRequest();
- int countrecords(const string file);
- void InputData(const int lines, const string file,struct Roster * MyRoster);
- void print(const int lines, struct Roster * MyRoster);
- void Alphabetize(const int lines, struct Roster * MyRoster);
- int main(void)
- {
- struct Roster * MyRoster;
- int lines=0;
- string file;
- file = FileNameRequest(); // requests filename and assigns it to file
- lines = countrecords(file);
- system("cls");
- MyRoster = new struct Roster[lines];
- InputData(lines,file, MyRoster);
- Alphabetize(lines, MyRoster);
- print(lines, MyRoster);
- system("pause");
- return 0;
- }
- string FileNameRequest()
- {
- string FileName;
- cout << "\n\n\n\n\n\n\n\n\n\n\t\t\t"
- << "Please enter file name\n\t\t\t";
- cin >> FileName;
- ifstream rawData;
- rawData.open(FileName); // attempt to open file to read in data
- while (!rawData.is_open())// check file
- {
- system("cls");
- cout << "\n\n\n\n\n\n\n\n\n\n\t\t\t " // centers text
- << "Invalid File name\n"
- << "Please enter new file name\n\t\t\t";
- cin >> FileName;
- rawData.open(FileName); // attempt to open file to read in data
- }
- rawData.close();
- return (FileName);
- }
- int countrecords(const string file)
- {
- ifstream rawData;
- int recordCounter = 0;
- char holder[MAXSIZE];
- rawData.open(file);
- do
- {
- rawData.getline(holder, MAXSIZE);
- recordCounter++;
- }while (!rawData.eof());
- rawData.close();
- return (recordCounter-1); // accounts for the last space
- }
- void InputData(const int lines, const string file,struct Roster *MyRoster)
- {
- ifstream rawData;
- rawData.open(file);
- for(int i=0; i< lines; i++)
- {
- rawData >> MyRoster[i].LName;
- rawData >> MyRoster[i].FName;
- rawData >> MyRoster[i].Cheesecake;
- }
- rawData.close();
- }
- void print(const int lines, struct Roster *MyRoster)
- {
- int Piecount = 0, Cakecount = 0;
- cout << setw(18) << left << "Cheesecake Survey" << endl;
- cout << setw(18) << left << "Last Name" ;
- cout << setw(18) << left << "First Name" ;
- cout << setw(18) << left << "Cake or Pie" << endl;
- cout << "-----------------------------------------------" << endl;
- for(int num=0; num < lines; num++)
- {
- cout << setw(18) << left << MyRoster[num].LName;
- cout << setw(18) << left << MyRoster[num].FName;
- if(MyRoster[num].Cheesecake == 'P' || MyRoster[num].Cheesecake == 'p')
- {
- Piecount++;
- cout <<"Pie";
- }
- else
- {
- Cakecount++;
- cout <<"Cake";
- }
- }
- cout << endl << "Records = " << lines << endl << "Pie Believers = " << Piecount << endl << "Cake Believers = " << Cakecount << endl;
- }
- void Alphabetize(const int lines, struct Roster *MyRoster)
- {
- Roster swapper;
- bool change = true;
- while(change)
- {
- change = false;
- for(int loop = 0; loop< lines; loop++)
- {
- if(MyRoster[loop].LName > MyRoster[loop+1].LName)
- {
- swapper = MyRoster[loop];
- MyRoster[loop] = MyRoster[loop+1];
- MyRoster[loop] = swapper;
- change = true;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment