Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- using std::fstream;
- #include <iostream>
- using std::cout;
- using std::endl;
- using std::cin;
- using std::ios;
- #include <cctype>
- using std::toupper;
- struct tod
- {
- int hour;
- int minute;
- int second;
- char descr[32];
- };
- void coutTod(const tod&);
- void coutTod(int n, const tod t[]); // n = number of items in the array
- int main()
- {
- // initialize tod array to 0
- tod theTime[5] = {0};
- fstream fin;
- char userIn = 'X';
- // collect user input and check for input validity
- cout << "Do you want to read the times from the database? [Y/N]: ";
- while (userIn != 'Y' && userIn != 'N')
- {
- cin >> userIn;
- userIn = toupper(userIn); // convert user input to uppercase
- if (userIn != 'Y' && userIn != 'N') cout << "Invalid input, enter again: ";
- }
- // if user selected Y, open the database file, check for file validity,
- // read from the file, and then close the file.
- if(userIn == 'Y')
- {
- fin.open("todDb.dat", ios::in | ios::binary);
- if (!fin)
- {
- cout << "Error: Invalid file" << endl;
- return 0;
- }
- fin.read((char*)theTime, sizeof(tod)*5);
- fin.close(); // close the input file
- }
- // promt user to change info
- while(1)
- {
- cout << "This is the data for the 5 records" << endl;
- coutTod(5, theTime);
- userIn = 'X';
- // see if user wants to change data
- cout << "Do you want to change any of the data? [Y/N]: ";
- while(userIn != 'Y' && userIn != 'N')
- {
- cin >> userIn;
- userIn = toupper(userIn);
- if (userIn != 'Y' && userIn != 'N') // if input is invalid, prompt for input again
- cout << "Invalid input, enter again: ";
- }
- // if yes, change data
- if (userIn == 'Y')
- {
- int i;
- // get which one to change
- cout << "Which time of day would you like to change? [1-5]: ";
- while(1)
- {
- cin >> i;
- i--;
- if (i < 0 || i > 4) // if it's not within range, prompt for input again
- cout << "Invalid input, enter again: ";
- else break;
- }
- // get info to change theTime
- cout << "Enter the hour, minute, second, and description, all separated by spaces." << endl;
- cin >> theTime[i].hour >> theTime[i].minute >> theTime[i].second;
- cin.ignore(1, ' ');
- cin.getline(theTime[i].descr, 32);
- }
- // otherwise, break the loop
- else break;
- }
- fstream fout;
- fout.open("todDb.dat", ios::out|ios::binary); // open the database file for output,
- fout.write((char*)theTime, sizeof(tod)*5); // write the array to it,
- fout.close(); // then close the file.
- }
- // prints elements of a tod object
- void coutTod(const tod& t)
- {
- cout << t.descr << " is "
- << t.hour << (t.minute < 10 ? ":0" : ":")
- << t.minute << (t.second < 10 ? ":0" : ":")
- << t.second << endl;
- }
- // takes tod objects in an array and sends them to be printed
- void coutTod(int n, const tod t[]) // n = number of items in the array
- {
- for (int i=0;i < n;i++)
- {
- cout << (i + 1) << ". ";
- coutTod(t[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment