shushus

Спринт 3. Создаем альтернативу assert.

Dec 8th, 2022
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. void Assert(bool value, const string& hint) {
  9.     // Реализуйте тело функции Assert
  10.     if(value == false){
  11.         cout << boolalpha;
  12.         cout << "Assertion failed."s;
  13.         if (!hint.empty()) {
  14.             cout << " Hint: "s << hint;
  15.         }
  16.         cout << endl;
  17.         abort();
  18.     }
  19. }
  20.  
  21. int main() {
  22.     const string greeting = "Hello"s;
  23.     // Намеренная ошибка в условии, чтобы показать работу Assert
  24.     Assert(greeting.empty(), "Greeting must be non-empty"s);
  25.     // Следующая строка не должна выполниться, так как Assert аварийно завершит работу программы
  26.     cout << "This line will not be printed"s << endl;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment