Kass

todDbB.cpp

Oct 14th, 2012
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.11 KB | None | 0 0
  1. #include <fstream>
  2. using std::fstream;
  3.  
  4. #include <iostream>
  5. using std::cout;
  6. using std::endl;
  7. using std::cin;
  8. using std::ios;
  9.  
  10. #include <cctype>
  11. using std::toupper;
  12.  
  13. struct tod
  14. {
  15.   int hour;
  16.   int minute;
  17.   int second;
  18.   char descr[32];
  19. };
  20.  
  21. void coutTod(const tod&);
  22. void coutTod(int n, const tod t[]); // n = number of items in the array
  23.  
  24. int main()
  25. {
  26.   // initialize tod array to 0
  27.   tod theTime[5] = {0};
  28.   fstream fin;
  29.   char userIn = 'X';
  30.  
  31.   // collect user input and check for input validity
  32.   cout << "Do you want to read the times from the database? [Y/N]: ";
  33.   while (userIn != 'Y' && userIn != 'N')
  34.   {
  35.     cin >> userIn;
  36.     userIn = toupper(userIn); // convert user input to uppercase
  37.     if (userIn != 'Y' && userIn != 'N') cout << "Invalid input, enter again: ";
  38.   }
  39.  
  40.   // if user selected Y, open the database file, check for file validity,
  41.   // read from the file, and then close the file.
  42.   if(userIn == 'Y')
  43.   {
  44.     fin.open("todDb.dat", ios::in | ios::binary);
  45.     if (!fin)
  46.     {
  47.       cout << "Error: Invalid file" << endl;
  48.       return 0;
  49.     }
  50.     fin.read((char*)theTime, sizeof(tod)*5);
  51.     fin.close(); // close the input file
  52.   }
  53.  
  54.   // promt user to change info
  55.   while(1)
  56.   {
  57.     cout << "This is the data for the 5 records" << endl;
  58.     coutTod(5, theTime);
  59.     userIn = 'X';
  60.    
  61.     // see if user wants to change data
  62.     cout << "Do you want to change any of the data? [Y/N]: ";
  63.     while(userIn != 'Y' && userIn != 'N')
  64.     {
  65.     cin >> userIn;
  66.     userIn = toupper(userIn);
  67.     if (userIn != 'Y' && userIn != 'N') // if input is invalid, prompt for input again
  68.       cout << "Invalid input, enter again: ";
  69.     }
  70.    
  71.     // if yes, change data
  72.     if (userIn == 'Y')
  73.     {
  74.       int i;
  75.      
  76.       // get which one to change
  77.       cout << "Which time of day would you like to change? [1-5]: ";
  78.       while(1)
  79.       {
  80.         cin >> i;
  81.         i--;
  82.         if (i < 0 || i > 4) // if it's not within range, prompt for input again
  83.           cout << "Invalid input, enter again: ";
  84.         else break;
  85.       }
  86.      
  87.       // get info to change theTime
  88.       cout << "Enter the hour, minute, second, and description, all separated by spaces." << endl;
  89.       cin >> theTime[i].hour >> theTime[i].minute >> theTime[i].second;
  90.       cin.ignore(1, ' ');
  91.       cin.getline(theTime[i].descr, 32);
  92.     }
  93.    
  94.     // otherwise, break the loop
  95.     else break;
  96.   }
  97.  
  98.  
  99.  
  100.   fstream fout;
  101.   fout.open("todDb.dat", ios::out|ios::binary); // open the database file for output,
  102.   fout.write((char*)theTime, sizeof(tod)*5); // write the array to it,
  103.   fout.close(); // then close the file.
  104. }
  105.  
  106. // prints elements of a tod object
  107. void coutTod(const tod& t)
  108. {  
  109.   cout << t.descr << " is "
  110.        << t.hour << (t.minute < 10 ? ":0" : ":")
  111.        << t.minute << (t.second < 10 ? ":0" : ":")
  112.        << t.second << endl;
  113. }
  114.  
  115. // takes tod objects in an array and sends them to be printed
  116. void coutTod(int n, const tod t[]) // n = number of items in the array
  117. {
  118.     for (int i=0;i < n;i++)
  119.     {
  120.       cout << (i + 1) << ". ";
  121.     coutTod(t[i]);
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment