Advertisement
Mike_be

Pavel's code

Nov 5th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <time.h>
  5. using namespace std;
  6. const int max_num = 8;
  7. const string intro = "Count the number of railway wagons";
  8. const string error_word = "Input is incorrect. Try again!";
  9. const string answer_word = "answer";
  10. const string next_word = "next";
  11. const string switch_word = "switch";
  12. const string previous_word = "previous";
  13. const string light_on = "The light is on";
  14. const string light_off = "The light is off";
  15. const string Continue = "Continue? (Y/N)>";
  16. const string action_select = "Input an action (next/previous/switch/answer)>";
  17. const string correct_answer = "The answer is correct";
  18. const string incorrect_answer = "The answer is incorrect";
  19. const char EOLN = '/n';
  20. const char space = ' ';
  21. const char yes_lit = 'Y';
  22. const char no_lit = 'N';
  23. void _message(string message)
  24. {
  25.     cout << message << endl;
  26. }
  27. bool space_check()
  28. {
  29.     if ((cin.rdbuf()->in_avail()) > 1)
  30.     {
  31.         char c = EOLN;
  32.         cin.get(c);
  33.         if (c == space) return space_check();
  34.         else return false;
  35.     }
  36.     else return true;
  37. }
  38. void clean()
  39. {
  40.     cin.clear();
  41.     cin.ignore(cin.rdbuf()->in_avail());
  42. }
  43. int main()
  44. {
  45.     srand(time(NULL));
  46.     char k = yes_lit;
  47.     _message(intro);
  48.     while (k == yes_lit)
  49.     {
  50.         bool n = true;
  51.         int b = rand() % max_num + 1;
  52.         vector <bool> a(max_num);
  53.         for (int i = 0; i < b; i++)
  54.         {
  55.             if (rand() < 16000) a[i] = true;
  56.             else a[i] = false;
  57.         }
  58.         for (int i = 0; n; )
  59.         {
  60.             if (a[i]) _message(light_on);
  61.             else _message(light_off);
  62.             string c;
  63.             for (bool g = true; g == true; c.clear())
  64.             {
  65.                 cout << action_select;
  66.                 clean;
  67.                 cin >> c;
  68.                 if (space_check())
  69.                 {
  70.                     if (c == next_word)
  71.                     {
  72.                         i++;
  73.                         g = false;
  74.                     }
  75.                     else if (c == previous_word)
  76.                     {
  77.                         i--;
  78.                         g = false;
  79.                     }
  80.                     else if (c == switch_word)
  81.                     {
  82.                         a[i] = !a[i];
  83.                         g = false;
  84.                     }
  85.                     else if (c == answer_word)
  86.                     {
  87.                         int temp;
  88.                         cout << "Answer is>";
  89.                         clean();
  90.                         while (!(cin >> temp) || !space_check())
  91.                         {
  92.                             clean();
  93.                             _message(error_word);
  94.                             cout << "Answer is>";
  95.                         }
  96.                         if (temp == b)  _message(correct_answer);
  97.                         else _message(incorrect_answer);
  98.                         g = false;
  99.                         n = false;
  100.                     }
  101.                     else _message(error_word);
  102.                 }
  103.                 else _message(error_word);
  104.                 clean();
  105.             }
  106.             if (i == b) i = 0;
  107.             if (i < 0) i = b;
  108.         }
  109.         k = EOLN;
  110.         cout << Continue;
  111.         cin >> k;
  112.         while (!(k == yes_lit || k == no_lit) || !space_check())
  113.         {
  114.             clean();
  115.             _message(error_word);
  116.             cout << Continue;
  117.             cin >> k;
  118.         }
  119.     }
  120.     system("pause");
  121.     return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement