Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- #include <clocale>
- #include <Windows.h>
- using namespace std;
- int main() {
- setlocale(LC_ALL, "Russian");
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- char answ;
- do {
- char temp;
- string word, word_check, direction;
- char** chars = new char* [10];
- for (int i = 0; i < 10; i++) {
- chars[i] = new char[10];
- }
- ifstream file;
- cout << "Enter your word >>";
- cin >> word;
- file.open("C:\\Users\\yarlo\\Desktop\\laba\\input.txt");
- int g, d, i_coord, j_coord = 0;
- int has_found = 0;
- for (int i = 0; i < 10; i++) {
- for (int j = 0; j < 10; j++) {
- if (file.peek() != '\n')
- {
- file.get(temp);
- chars[i][j] = temp;
- }
- else
- {
- file.ignore(1);
- j--;
- }
- }
- }
- if (!file.is_open())
- cout << "Error. Can't open your file" << endl;
- for (int i = 0; i < 10; i++) {
- for (int j = 0; j < 10; j++) {
- if (chars[i][j] == word[0]) {
- for (d = i; d > 0 && word_check.length() <= word.length() && has_found == 0; d--) {
- word_check += chars[d][j];
- if (word_check == word) {
- has_found = 1;
- i_coord = i;
- j_coord = j;
- direction = "up";
- }
- }
- word_check.clear();
- for (d = i; d < 10 && word_check.length() <= word.length() && has_found == 0; d++) {
- word_check += chars[d][j];
- if (word_check == word) {
- has_found = 1;
- i_coord = i;
- j_coord = j;
- direction = "down";
- }
- }
- word_check.clear();
- for (d = j; d < 10 && word_check.length() <= word.length() && has_found == 0; d++) {
- word_check += chars[i][d];
- if (word_check == word) {
- has_found = 1;
- i_coord = i;
- j_coord = j;
- direction = "right";
- }
- }
- word_check.clear();
- for (d = i; d > 0 && word_check.length() <= word.length() && has_found == 0; d--) {
- word_check += chars[i][d];
- if (word_check == word) {
- has_found = 1;
- i_coord = i;
- j_coord = j;
- direction = "left";
- }
- }
- word_check.clear();
- }
- }
- }
- file.close();
- ofstream file_output;
- file_output.open("C:\\Users\\yarlo\\Desktop\\laba\\output.txt");
- if (has_found == 1) {
- file_output << "\nWord has been found";
- file_output << "\nCoords>>" << i_coord + 1 << ' ' << j_coord + 1;
- file_output << "\nDirection>>" << direction;
- }
- else {
- file_output << "\nWord has not been found";
- }
- for (int i = 0; i < 10; i++) {
- delete[] chars[i];
- }
- delete[] chars;
- file_output.close();
- cout << "\nDo you want to repeat?[y/n]>> ";
- cin >> answ;
- while (cin.fail() || cin.peek() != '\n' || (answ != 'y' && answ != 'Y' && answ != 'n' && answ != 'N')) {
- cout << "Invalid answer";
- cin.clear();
- cin.ignore(10000, '\n');
- cout << "\nDo you want to repeat?[y/n]>> ";
- cin >> answ;
- }
- } while (answ == 'y' || answ == 'Y');
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment