Reputation: 1
So I need to know how to identify a line of text and output what kind of data type it is, like if the line says 123
, it should be output as 123 int
.
Right now, my program only identifies boolean
, string
, and char
. How do I get it to tell me if it is an int
or a double
?
int main() {
string line;
string arr[30];
ifstream file("pp.txt");
if (file.is_open()){
for (int i = 0; i <= 4; i++) {
file >> arr[i];
cout << arr[i];
if (arr[i] == "true" || arr[i] == "false") {
cout << " boolean" << endl;
}
if (arr[i].length() == 1) {
cout << " character" << endl;
}
if (arr[i].length() > 1 && arr[i] != "true" && arr[i] != "false") {
cout << " string" << endl;
}
}
file.close();
}
else
cout << "Unable to open file";
system("pause");
}
Thanks
Upvotes: 0
Views: 1222
Reputation:
Use regex: http://www.cplusplus.com/reference/regex/
#include <regex>
std::string token = "true";
std::regex boolean_expr = std::regex("^false|true$");
std::regex float_expr = std::regex("^\d+\.\d+$");
std::regex integer_expr = std::regex("^\d+$");
...
if (std::regex_match(token, boolean_expr)) {
// matched a boolean, do something
}
else if (std::regex_match(token, float_expr)) {
// matched a float
}
else if (std::regex_match(token, integer_expr)) {
// matched an integer
}
...
Upvotes: 1