40 lines
962 B
C++
40 lines
962 B
C++
#pragma once
|
|
#include <string>
|
|
#include <stdexcept>
|
|
|
|
class ConverType
|
|
{
|
|
public:
|
|
static int TryToI(const std::string& input){
|
|
int ret = INT_MIN;
|
|
try {
|
|
ret = stoi(input);
|
|
return ret;
|
|
}
|
|
catch (const std::invalid_argument& e) {
|
|
printf("input is not number...error:%s\n", e.what());
|
|
}
|
|
catch (const std::out_of_range& e) {
|
|
printf("input number is out of int range...error:%s\n", e.what());
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
static float TryToF(const std::string& input) {
|
|
float ret = -1.0;
|
|
try {
|
|
ret = stof(input);
|
|
return ret;
|
|
}
|
|
catch (const std::invalid_argument& e) {
|
|
printf("input is not float...error:%s\n", e.what());
|
|
}
|
|
catch (const std::out_of_range& e) {
|
|
printf("input number is out of float range...error:%s\n", e.what());
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
};
|
|
|