Linear Equations Solver  1.0
Using Gaussian elimination
Loading...
Searching...
No Matches
interaction.cpp
Go to the documentation of this file.
1
13#include "interaction.h"
14#include <iostream>
15#include <vector>
16#include <string>
17#include <filesystem>
18#include <limits>
19#include "utils.h"
20
21using namespace std;
22
25{
26 vector<string> in_files;
27 for (const auto &entry : filesystem::directory_iterator(filesystem::current_path()))
28 {
29 if (entry.is_regular_file())
30 {
31 string filename = entry.path().filename().string();
32 if (filename.size() >= 3 && filename.substr(filename.size() - 3) == ".in")
33 {
34 in_files.push_back(filename);
35 }
36 }
37 }
38
39 string selected_file;
40 if (in_files.empty())
41 {
42 cout << "No .in files found in the current directory." << endl;
43 return "";
44 }
45 else if (in_files.size() == 1)
46 {
47 selected_file = in_files[0];
48 cout << "Found one .in file: " << selected_file << ". Automatically selecting it." << endl;
49 }
50 else
51 {
52 cout << "Multiple .in files found. Please select one:" << endl;
53 for (size_t i = 0; i < in_files.size(); i++)
54 {
55 cout << i + 1 << ". " << in_files[i] << endl;
56 }
57 int file_choice;
58 // Improved input validation
59 while (true)
60 {
61 cout << "Enter the number of the file you want to use (1-" << in_files.size() << "): ";
62 cin >> file_choice;
63
64 if (cin.fail() || file_choice < 1 || file_choice > static_cast<int>(in_files.size()))
65 {
66 cin.clear(); // Clear error flags
67 cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Clear input buffer
68 cout << "Invalid input. Please enter a number between 1 and " << in_files.size() << "." << endl;
69 }
70 else
71 {
72 break;
73 }
74 }
75 selected_file = in_files[file_choice - 1];
76 }
77 cout << endl;
78 return selected_file;
79}
80
83{
84 char choice;
85 while (true)
86 {
87 cout << "\nDo you want to run the program again? (y/n): ";
88 cin >> choice;
89
90 if (choice == 'y' || choice == 'Y' || choice == 'n' || choice == 'N')
91 {
92 break;
93 }
94 else
95 {
96 cout << "Invalid input. Please enter 'y' or 'n'." << endl;
97 }
98 }
99 return choice;
100}
101
104{
105 cout << "\nPress Enter to exit...";
106 cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Clear input buffer
107 cin.get(); // Wait for Enter key
108}
string SelectInputFile()
Allows the user to select an input .in file from the current directory.Returns an empty string if no ...
char AskRunAgain()
Return char The user's choice ('y', 'Y', 'n', 'N').
void WaitForExit()
Waits for the user to press Enter before exiting.
User interaction functions.
Utility functions for matrix initialization and display.