Advertisement
Neveles

MAIN

Dec 8th, 2019
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | None | 0 0
  1. #include "BookInformation.h"
  2. #include "Array.h"
  3. #include "io.h"
  4.  
  5. using namespace std;
  6.  
  7. const string INVALID_OPEN = "Unable to open the file!";
  8. const string EMPTY_FILE = "File is empty!";
  9.  
  10. void ifAuthor(string &book, const unsigned int& nOfStr, unsigned int &index);
  11. void ifTitle(string& book, const unsigned int& nOfStr, unsigned int& index);
  12. void ifPublishingYear(string& book, const unsigned int& nOfStr, unsigned int& index);
  13. void ifDiscipline(string& book, const unsigned int& nOfStr, unsigned int& index);
  14. void ifNumberOfCopies(string& book, const unsigned int& nOfStr, unsigned int& index);
  15.  
  16. int main()
  17. {
  18.     setlocale(LC_ALL, "ru");
  19.  
  20.     ifstream in("Books.txt");
  21.     try
  22.     {
  23.         // проверка на открытие файла
  24.         if (!in)
  25.             throw INVALID_OPEN;
  26.        
  27.         // проверка не пуст ли файл
  28.         in.seekg(0, ios::end);
  29.         unsigned int size = in.tellg();
  30.         if (!size)
  31.             throw EMPTY_FILE;
  32.  
  33.         // инициализация массива
  34.         unsigned int nBooksArray = 100;
  35.         BookInformation* booksArray = new BookInformation[nBooksArray];
  36.  
  37.         // цикл считывания строк из файла
  38.         unsigned int nOfStr = 0;
  39.         while (!in.eof())
  40.         {
  41.             try
  42.             {
  43.                 string book;
  44.                 ++nOfStr;
  45.                 getline(in, book);
  46.  
  47.                 // проверка количества разделителей
  48.                 unsigned int nBars = 0;
  49.                 for (unsigned int i = 0; book[i] != '\0'; ++i)
  50.                 {
  51.                     if (book[i] == '|')
  52.                         ++nBars;
  53.                     if (nBars > 5)
  54.                         throw nOfStr;
  55.                 }
  56.                 if (nBars != 5)
  57.                     throw nOfStr;
  58.  
  59.                 unsigned int index = 0;
  60.                 ifAuthor(book, nOfStr, index);
  61.                 //ifTitle(book, nOfStr, index);
  62.                 //ifPublishingYear(book, nOfStr, index);
  63.                 //ifDiscipline(book, nOfStr, index);
  64.                 //ifNumberOfCopies(book, nOfStr, index);
  65.  
  66.             }
  67.             catch (const unsigned int& err)
  68.             {
  69.                 cerr << "There is an error in " << err << " string!" << endl;
  70.             }
  71.         }
  72.  
  73.         delete[] booksArray;
  74.     }
  75.  
  76.     catch (const string& error)
  77.     {
  78.         cerr << endl << error << endl;
  79.         return -1;
  80.     }
  81.  
  82.     return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement