-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNote_Taker.cpp
More file actions
42 lines (34 loc) · 1.09 KB
/
Note_Taker.cpp
File metadata and controls
42 lines (34 loc) · 1.09 KB
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
#include <iostream>
#include <fstream>
#include <regex>
using namespace std;
const string PROMPT_FILENAME = "Enter the name of the text file to be saved (e.g. readme.txt): ";
const string PROMPT_TEXT = "Enter your text (type 'exit' to save and quit): ";
const string ERROR_FILENAME = "Error: Invalid filename. Please enter a valid filename without spaces or special characters.";
const string ERROR_OPEN_FILE = "Error: Failed to create file.";
int main()
{
string filename, line;
cout << PROMPT_FILENAME;
getline(cin, filename);
// Validate filename
if (!regex_match(filename, regex("^[a-zA-Z0-9_\\.]+$"))) {
cout << ERROR_FILENAME << endl;
return 1;
}
ofstream file(filename);
if (!file) {
cout << ERROR_OPEN_FILE << endl;
return 1;
}
cout << PROMPT_TEXT;
do {
getline(cin, line);
if (line != "exit") {
file << line << endl;
}
} while (line != "exit");
file.close();
cout << "File saved in current directory. Exiting..." << endl;
return 0;
}