Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <time.h>
- using namespace std;
- const int max_num = 8;
- const string intro = "Count the number of railway wagons";
- const string error_word = "Input is incorrect. Try again!";
- const string answer_word = "answer";
- const string next_word = "next";
- const string switch_word = "switch";
- const string previous_word = "previous";
- const string light_on = "The light is on";
- const string light_off = "The light is off";
- const string Continue = "Continue? (Y/N)>";
- const string action_select = "Input an action (next/previous/switch/answer)>";
- const string correct_answer = "The answer is correct";
- const string incorrect_answer = "The answer is incorrect";
- const char EOLN = '/n';
- const char space = ' ';
- const char yes_lit = 'Y';
- const char no_lit = 'N';
- void _message(string message)
- {
- cout << message << endl;
- }
- bool space_check()
- {
- if ((cin.rdbuf()->in_avail()) > 1)
- {
- char c = EOLN;
- cin.get(c);
- if (c == space) return space_check();
- else return false;
- }
- else return true;
- }
- void clean()
- {
- cin.clear();
- cin.ignore(cin.rdbuf()->in_avail());
- }
- int main()
- {
- srand(time(NULL));
- char k = yes_lit;
- _message(intro);
- while (k == yes_lit)
- {
- bool n = true;
- int b = rand() % max_num + 1;
- vector <bool> a(max_num);
- for (int i = 0; i < b; i++)
- {
- if (rand() < 16000) a[i] = true;
- else a[i] = false;
- }
- for (int i = 0; n; )
- {
- if (a[i]) _message(light_on);
- else _message(light_off);
- string c;
- for (bool g = true; g == true; c.clear())
- {
- cout << action_select;
- clean;
- cin >> c;
- if (space_check())
- {
- if (c == next_word)
- {
- i++;
- g = false;
- }
- else if (c == previous_word)
- {
- i--;
- g = false;
- }
- else if (c == switch_word)
- {
- a[i] = !a[i];
- g = false;
- }
- else if (c == answer_word)
- {
- int temp;
- cout << "Answer is>";
- clean();
- while (!(cin >> temp) || !space_check())
- {
- clean();
- _message(error_word);
- cout << "Answer is>";
- }
- if (temp == b) _message(correct_answer);
- else _message(incorrect_answer);
- g = false;
- n = false;
- }
- else _message(error_word);
- }
- else _message(error_word);
- clean();
- }
- if (i == b) i = 0;
- if (i < 0) i = b;
- }
- k = EOLN;
- cout << Continue;
- cin >> k;
- while (!(k == yes_lit || k == no_lit) || !space_check())
- {
- clean();
- _message(error_word);
- cout << Continue;
- cin >> k;
- }
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement