Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- using std::ifstream;
- using std::ofstream;
- #include <iostream>
- using std::cout;
- using std::endl;
- using std::cin;
- 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()
- {
- ifstream fin;
- fin.open("todDb.txt");
- /*if (!fin)
- {
- cout << "Invalid file" << endl;
- return 0;
- }*/
- tod theTime[5];
- // read text file
- for (int i = 0; i < 5; i++)
- {
- fin >> theTime[i].hour >> theTime[i].minute >> theTime[i].second;
- fin.ignore(1, ' ');
- fin.getline(theTime[i].descr, 32);
- }
- fin.close();
- // promt user to change info
- while(1)
- {
- coutTod(5, theTime);
- char yorn;
- // see if user wants to change data
- cout << "Do you want to change any of the data? [Y/N]: ";
- while(1)
- {
- cin >> yorn;
- if (yorn != 'Y' && yorn != 'N') // if input is invalid, prompt for input again
- cout << "Invalid input, enter again: ";
- else break; // break loop if input is valid
- }
- // if yes, change data
- if (yorn == 'Y')
- {
- int i;
- // get which one to change
- cout << "Which TOD would you like to change? [0, 1, 2, 3, or 4]: ";
- while(1)
- {
- cin >> 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, 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;
- }
- ofstream fout;
- fout.open("todDb.txt");
- for (int i = 0; i < 5; i++)
- fout << theTime[i].hour << ' ' << theTime[i].minute << ' ' << theTime[i].second << ' '
- << theTime[i].descr << endl;
- }
- // 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++)
- coutTod(t[i]);
- }
Advertisement
Add Comment
Please, Sign In to add comment