@@ -35,7 +35,16 @@ void ScalarConverter::convert(std::string str)
35
35
{
36
36
try
37
37
{
38
- f = std::stof (str);
38
+ if (*(str.rbegin ()) == ' f' )
39
+ {
40
+ str.pop_back ();
41
+ if (*(str.rbegin ()) == ' f' )
42
+ throw std::exception ();
43
+ }
44
+ else
45
+ throw std::exception ();
46
+ std::stringstream ss (str);
47
+ ss >> f;
39
48
}
40
49
catch (std::exception &e)
41
50
{
@@ -50,7 +59,8 @@ void ScalarConverter::convert(std::string str)
50
59
{
51
60
try
52
61
{
53
- d = std::stod (str);
62
+ std::stringstream ss (str);
63
+ ss >> d;
54
64
}
55
65
catch (std::exception &e)
56
66
{
@@ -61,9 +71,9 @@ void ScalarConverter::convert(std::string str)
61
71
c = static_cast <char >(d);
62
72
i = static_cast <int >(d);
63
73
}
64
- else if (str.length () == 1 )
74
+ else if (str.length () == 1 && ! std::isdigit (str. at ( 0 )) )
65
75
{
66
- c = str[ 0 ] ;
76
+ c = str. at ( 0 ) ;
67
77
f = static_cast <float >(c);
68
78
d = static_cast <double >(c);
69
79
i = static_cast <int >(c);
@@ -72,7 +82,11 @@ void ScalarConverter::convert(std::string str)
72
82
{
73
83
try
74
84
{
75
- i = std::stoi (str);
85
+ if (!std::isdigit (str.at (0 )) || std::atol (str.c_str ()) > 2147483648 )
86
+ throw std::exception ();
87
+ std::stringstream ss (str);
88
+ ss >> i;
89
+
76
90
}
77
91
catch (std::exception &e)
78
92
{
@@ -81,7 +95,7 @@ void ScalarConverter::convert(std::string str)
81
95
}
82
96
f = static_cast <float >(i);
83
97
c = static_cast <char >(i);
84
- d = static_cast <double >(d );
98
+ d = static_cast <double >(i );
85
99
}
86
100
87
101
if (print_c == false )
@@ -100,4 +114,4 @@ void ScalarConverter::convert(std::string str)
100
114
std::cout << " Double is " << std::fixed << std::setprecision (1 ) << d << std::endl;
101
115
102
116
103
- }
117
+ }
0 commit comments