-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataManipulation.cpp
More file actions
146 lines (124 loc) · 4.08 KB
/
DataManipulation.cpp
File metadata and controls
146 lines (124 loc) · 4.08 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
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
// Winter Thomas and Ricardo Romanach
#include <iostream>
#include <string>
#include <fstream>
#include "DataManipulation.h"
// Reads all information from a file and puts it into a string
std::string DataManipulation::readStringFromFile(std::string fileName){
std::ifstream inputFile;
std::string fileText = "";
inputFile.open(fileName);
std::string currentLine;
if (inputFile.is_open()){
// Check if file is empty
if (inputFile.eof()){
std::cout << "\nFile is empty";
return "";
}
while (std::getline (inputFile, currentLine)){
fileText += currentLine + " ";
}
inputFile.close();
// Take off ending space that is added
fileText = fileText.substr(0, fileText.length() - 1);
}
else {
std::cout << "\nFile " << fileName << " failed to open";
return "";
}
return fileText;
}
// Takes a string and puts it into a file
void DataManipulation::writeStringToFile(std::string fileName, std::string stringToWrite){
std::ofstream outputFile;
outputFile.open(fileName);
if (outputFile.is_open()){
outputFile << stringToWrite;
outputFile.close();
}
else {
std::cout << "\nFile" << fileName << "failed to open\n";
}
}
// Asks if a user wants input from a file or from manual input
// then prompts for file name and reads returns its contents
// or prompts user for input and returns that
std::string DataManipulation::getUserInput(std::string manualCustomMessage, std::string fileCustomMessage){
std::string userInput = "";
std::cout << "\nPlease make a selection\n";
std::cout << "1. Get input from user\n";
std::cout << "2. Get input from file\n";
// Manual user input
if (getIntegerInput(1,2) == 1){
while (userInput.empty()){
std::cout << "\n" << manualCustomMessage << "\n> ";
getline(std::cin, userInput);
}
}
// Reading from a file
else {
while (userInput.empty()){
std::cout << "\n" << fileCustomMessage << "\n> ";
getline(std::cin, userInput);
userInput = readStringFromFile(userInput);
}
}
return userInput;
}
// Takes a user input and returns the string as an input
// Range values are inclusive.
int DataManipulation::getIntegerInput(int lowRange, int highRange){
std::string userInput;
int integerInput;
while (true){
std::cout << "> ";
getline(std::cin, userInput);
// Check that the user typed something
if (userInput.empty()){
std::cout << "\nPlease enter a valid option.\n";
continue;
}
// Convert the first character to an integer
try {
integerInput = std::stoi(userInput);
}
catch (const std::exception& e){
std::cout << "\nPlease enter a valid input.\n";
continue;
}
// Check to make sure the input is in range
if (!(lowRange <= integerInput && integerInput <= highRange)){
std::cout << "\nPlease enter a valid input\n";
continue;
}
return integerInput;
}
}
// Takes the first character of a user input and if its a letter
// Returns it as a capital letter
char DataManipulation::getLetterInput(){
std::string userInput;
char charInput;
while (true){
std::cout << "> ";
getline(std::cin, userInput);
// Check that the user typed something
if (userInput.empty()){
std::cout << "\nPlease enter a valid option.\n";
continue;
}
// Make sure its only a single character
if (userInput.length() > 1){
std::cout << "\nPlease only enter a single letter.\n";
continue;
}
// Grabs the character
charInput = userInput[0];
// Check if the character is a letter and return it capitalized
if (isalpha(charInput)){
return toupper(charInput);
}
// If not a letter
std::cout << "\nPlease enter a valid option.\n";
}
}