Repository navigation
Expand file tree
/
Copy pathtext_editor.cpp
More file actions
95 lines (85 loc) · 2.48 KB
/
Copy pathtext_editor.cpp
File metadata and controls
95 lines (85 loc) · 2.48 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
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
#include <iostream>
#include <vector>
#include <string>
#include <limits>
//function to delete a line
//function to clear all text
// option to quit program
void viewText(const std::vector<std::string> &buffer){
for(int i = 0; i < buffer.size(); i++){
std:: cout << i+1 << ". ";
std::cout << buffer[i] << "\n";
}
}
void clearAll(std::vector<std::string> &buffer){
buffer.clear();
std::cout << "[All text cleared]\n";
}
void deleteLine(std::vector<std::string> &buffer){
if(buffer.empty()){
std::cout << "[nothing to delete]\n";
return;
}
std::cout << "Select which line you would like to delete: ";
int choice;
while(!(std::cin >> choice) || choice < 1 || choice > static_cast<int>(buffer.size())){
std::cout << "\n[invalid input]";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
buffer.erase(buffer.begin() + choice - 1);
}
void appendLine(std::vector<std::string> &buffer){
viewText(buffer);
std::cout << "\n:";
std::string text;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
getline(std::cin, text);
buffer.push_back(text);
}
void printMenu(const std::vector<std::string> &buffer){
std::cout << "Mini Text Editor\n";
std::cout << "----------------\n";
viewText(buffer);
std::cout << "----------------\n";
std::cout << "1. View text\n";
std::cout << "2. Append line\n";
std::cout << "3. Delete line\n";
std::cout << "4. Clear all text\n";
std::cout << "5. Quit\n";
std::cout << "Choice:";
}
int main(){
std::vector<std::string> buffer;
bool running = true;
int choice;
while (running){
printMenu(buffer);
while (!(std::cin >> choice) || !(choice < 6 && choice > 0)){
std::cout << "[invalid input]";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
//std::cout << choice;
}
switch(choice){
case 1:
viewText(buffer);
break;
case 2:
appendLine(buffer);
break;
case 3:
deleteLine(buffer);
break;
case 4:
clearAll(buffer);
break;
case 5:
return 0;
default:
viewText(buffer);
break;
}
}
return 0;
}