1+ #include < iostream>
2+ #include < fstream>
3+ #include < string>
4+ #include < ctime>
5+ #include < iomanip>
6+ #include < sstream>
7+ #include < filesystem>
8+
9+ using namespace std ;
10+ namespace fs = std::filesystem;
11+
12+ struct Student {
13+ string name;
14+ string email;
15+ string umid;
16+ string cardID;
17+ };
18+
19+ string getCurrentTime () {
20+ std::time_t now = std::time (nullptr );
21+ std::tm* localTime = std::localtime (&now);
22+
23+ std::ostringstream oss;
24+ oss << std::put_time (localTime, " %Y-%m-%d %H:%M:%S" );
25+ return oss.str ();
26+ }
27+
28+ Student parseStudent (string input) {
29+ // Example input: %B6008475938410415^HAMMERBERG/J^2806120JACKMH?;6008475938410415=2806120=816877446?
30+ // Name: J HAMMERBERG
31+ 32+ // UMID: 59384104
33+ // Card ID: 6008475938410415
34+ Student student;
35+ size_t start, end;
36+ // Get the card ID
37+ start = input.find (" %" ) + 2 ;
38+ end = input.find (" ^" );
39+ student.cardID = input.substr (start, end - start);
40+ // Get the last name
41+ start = input.find (" ^" ) + 1 ;
42+ end = input.find (" /" );
43+ string lastName = input.substr (start, end - start);
44+ // Get the first name
45+ start = end + 1 ;
46+ end = input.find (" ^" , start);
47+ // Concatenate the first and last name to get the full name
48+ student.name = input.substr (start, end - start) + " " + lastName;
49+ // Get the UMID
50+ start = input.find (" ^" ) - 10 ;
51+ end = input.find (" ^" ) - 2 ;
52+ student.umid = input.substr (start, end - start);
53+ // Get the email
54+ start = input.find (" ^" , input.find (" /" )) + 8 ; // Find the next ^ after the the / character
55+ end = input.find (" ?;" );
56+ student.email = input.substr (start, end - start) + " @umich.edu" ;
57+ return student;
58+ }
59+
60+ int main () {
61+ string header = " Time,Name,Email,UMID,Card ID" ;
62+ string filename = (fs::current_path () / " Festifall Cardreader.csv" ).string ();
63+ ifstream infile (filename);
64+ bool headerMatches = false ;
65+
66+ if (infile.is_open ()) {
67+ string firstLine;
68+ getline (infile, firstLine);
69+ if (firstLine == header) {
70+ headerMatches = true ;
71+ }
72+ infile.close ();
73+ }
74+
75+ ofstream file;
76+ if (headerMatches) {
77+ file.open (filename, ios::app);
78+ cout << " Existing file found" << endl;
79+ file.close ();
80+ } else {
81+ file.open (filename);
82+ file << header << endl;
83+ cout << " New file created" << endl;
84+ file.close ();
85+ }
86+
87+ while (true ) {
88+ string input;
89+ cout << " Type 'exit' to stop" << endl;
90+ cout << " Enter student info: " ;
91+ cin >> input;
92+ if (input == " exit" || input == " Exit" || input == " EXIT" ) {
93+ break ;
94+ }
95+ Student student = parseStudent (input);
96+ string time = getCurrentTime ();
97+ file.open (filename, ios::app);
98+ if (!file.is_open ()) {
99+ cout << " Error opening file" << endl;
100+ return 1 ;
101+ }
102+ file << time << " ," << student.name << " ," << student.email << " ," << student.umid << " ," << student.cardID << endl;
103+ cout << time << " , " << student.name << " , " << student.email << " , " << student.umid << " , " << student.cardID << endl;
104+ file.close ();
105+ }
106+ return 0 ;
107+ }
0 commit comments