Kass

todDb.cpp

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