1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
#ifndef HELPERS_HPP
#define HELPERS_HPP
#include <random>
#include <string>
#include <iostream>
#include <fstream>
using std::cin;
using std::cout;
using std::endl;
inline int Random(const int& lowest, const int& highest)
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> dis(lowest, highest);
const int random_number = dis(gen);
return random_number;
}
inline void GenerateRandomNumbers(int arrayToFill[], const int& size)
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> dis(0, size);
for (auto i = 0; i < size; ++i)
{
const int random_number = dis(gen);
arrayToFill[i] = random_number;
}
}
inline int add(int num, char myChar)
{
return num + myChar;
}
inline int add(int num, int num2, int num3)
{
return num + num2 + num3;
}
inline int add(int num, int num2, int num3, int num4)
{
return num + num2 + num3 + num4;
}
inline int ReadInt(const char* prompt)
{
using std::cin;
using std::cout;
using std::endl;
int intVal;
cout << endl << prompt;
cin >> intVal;
while (!cin)
{
cin.clear();
cin.ignore(MAX_STREAM_SIZE, '\n');
cout << "Invalid integer. Please try again: ";
cout.flush();
cin >> intVal;
}
cin.ignore(MAX_STREAM_SIZE, '\n');
return intVal;
}
// Improved function for reading strings with user input
inline char* PromptInputNewCharArray(const char* prompt, long long maxLen)
{
char* inputStr = nullptr;
try
{
inputStr = new char[maxLen];
}
catch (const std::exception& ex)
{
delete[] inputStr;
inputStr = nullptr;
std::cerr << "Unable to allocate new char array: " << ex.what();
return inputStr;
}
cout << endl << prompt;
// Ensure the prompt is displayed immediately
cout.flush();
cin.get(inputStr, maxLen, '\n');
while (!cin) {
// Clear error flag
cin.clear();
// Ignore all characters until a newline
cin.ignore(MAX_STREAM_SIZE, '\n');
cout << endl << prompt;
// Flush again to ensure prompt is visible
cout.flush();
cin.get(inputStr, maxLen, '\n');
}
// Discard any characters remaining in the buffer, including the newline
cin.ignore(MAX_STREAM_SIZE, '\n');
return inputStr;
}
inline std::string PromptInputString(const char* prompt)
{
std::string line;
cout << endl << prompt;
// Ensure the prompt is displayed immediately
cout.flush();
std::getline(cin, line);
while (!cin) {
// Flush again to ensure prompt is visible
cout.flush();
// Clear error flag
cin.clear();
cout << endl << prompt;
std::getline(cin, line);
}
return line;
}
inline bool ReadFromFileToConsole(const char* fileName)
{
std::ifstream file(fileName);
if (!file.is_open())
{
std::cerr << "Could not open file: " << fileName;
return false;
}
std::string line;
while (getline(file, line))
{
std::cout << line << std::endl;
//does something with the reading of file
}
file.close();
return true;
}
inline bool WriteToFile(const char* fileName, const char* fileContents)
{
std::ofstream file(fileName);
if (!file.is_open())
{
std::cerr << "Could not open file: " << fileName;
return false;
}
file << fileContents;
file.close();
return true;
}
#endif
|