1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
#include <iostream>
using std::endl;
using std::cout;
using std::cin;
#include <fstream>
using std::ofstream;
using std::ifstream;
using std::ios;
// struct def
struct tod
{
int hour; // hour, 0-23
int minute; // minute, 0-59
int second; // second, 0-59
char descr[32]; // the description of the time of day
};
int main()
{
// Declarations
ifstream fin;
ofstream fout;
char answer;
char viewObj;
int objChange;
// Intialize array
tod theTime[5] =
{
{0, 0, 0, "Midnight"}, // 0
{0, 0, 0, "Midnight"}, // 1
{0, 0, 0, "Midnight"}, // 2
{0, 0, 0, "Midnight"}, // 3
{0, 0, 0, "Midnight"} // 4
};
cout << "Overwrite the default values with those from todDB.dat?" << endl;
cout << "Choice [Y or N]: ";
cin >> viewObj;
if ((viewObj == 'Y') || (viewObj == 'y'))
{
// Open file, check for errors read and initialize array
fin.open("todDb.dat", ios::in|ios::binary);
if (!fin.good())
cout << "Error, cannot open file! Data stays default." << endl;
else
{
for (int i = 0; i < 5; i++)
fin.read((char*)&theTime[i], sizeof(tod));
}
fin.close();
cout << endl;
}
cout << "The current (default) times are: " << endl;
for (int i = 0; i < 5; i++)
cout << theTime[i].descr << " is " << theTime[i].hour << ':' << theTime[i].minute << ':' << theTime[i].second << endl;
while(1)
{
cout << "Would you like to make any changes to the times? (Y or N) ";
cin >> answer;
cout << endl;
if ((answer == 'N') || (answer == 'n'))
break;
else if ((answer == 'Y') || (answer == 'y'))
{
cout << "Which object would you like to change?" << endl;
cout << "These are the current times:" << endl;
for (int k = 0; k < 5; k++)
cout << k << "." << theTime[k].descr << " is " << theTime[k].hour << ':' << theTime[k].minute << ':' << theTime[k].second << endl;
cout << endl;
cout << "Select an object [0-4]: ";
cin >> objChange;
if ((objChange < 0) || (objChange > 4))
continue;
else
{
cout << "Enter the hour, minute, seconds, and description to change" << endl;
cin >> theTime[objChange].hour >> theTime[objChange].minute >> theTime[objChange].second;
cin.ignore(1000, 10);
cin.getline(theTime[objChange].descr, 32);
}
}
else
cout << "Invalid answer, try again!" << endl;
}
cout << "Writing new times to data file and exiting program..." << endl;
fout.open("todDb.dat", ios::out|ios::binary);
for (int j = 0; j < 5; j++)
fout.write((char*)&theTime[j], sizeof(tod));
fout.close();
return 0;
}
|