Skip to content

Commit 3e26f8d

Browse files
committed
fixed c++11 functions
1 parent aa60d13 commit 3e26f8d

File tree

5 files changed

+32
-14
lines changed

5 files changed

+32
-14
lines changed

cpp_module_06/ex00/ScalarConverter.cpp

+21-7
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,16 @@ void ScalarConverter::convert(std::string str)
3535
{
3636
try
3737
{
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;
3948
}
4049
catch (std::exception &e)
4150
{
@@ -50,7 +59,8 @@ void ScalarConverter::convert(std::string str)
5059
{
5160
try
5261
{
53-
d = std::stod(str);
62+
std::stringstream ss(str);
63+
ss >> d;
5464
}
5565
catch (std::exception &e)
5666
{
@@ -61,9 +71,9 @@ void ScalarConverter::convert(std::string str)
6171
c = static_cast<char>(d);
6272
i = static_cast<int>(d);
6373
}
64-
else if (str.length() == 1)
74+
else if (str.length() == 1 && !std::isdigit(str.at(0)))
6575
{
66-
c = str[0];
76+
c = str.at(0);
6777
f = static_cast<float>(c);
6878
d = static_cast<double>(c);
6979
i = static_cast<int>(c);
@@ -72,7 +82,11 @@ void ScalarConverter::convert(std::string str)
7282
{
7383
try
7484
{
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+
7690
}
7791
catch (std::exception &e)
7892
{
@@ -81,7 +95,7 @@ void ScalarConverter::convert(std::string str)
8195
}
8296
f = static_cast<float>(i);
8397
c = static_cast<char>(i);
84-
d = static_cast<double>(d);
98+
d = static_cast<double>(i);
8599
}
86100

87101
if (print_c == false)
@@ -100,4 +114,4 @@ void ScalarConverter::convert(std::string str)
100114
std::cout << "Double is " << std::fixed << std::setprecision(1) << d << std::endl;
101115

102116

103-
}
117+
}

cpp_module_06/ex00/ScalarConverter.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <iostream>
55
#include <string>
66
#include <exception>
7+
#include <sstream>
78
#include <limits>
89
#include <iomanip>
910

cpp_module_06/ex02/Base.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ Base::~Base()
55

66
Base *generate(void)
77
{
8-
static int i;
9-
if (i % 3 == 0)
10-
return (i++, new A());
11-
else if (i % 3 == 1)
12-
return (i++, new B());
13-
else if (i % 3 == 2)
14-
return (i++, new C());
8+
int i = std::rand() % 3;
9+
if (i == 0)
10+
return (new A());
11+
else if (i == 1)
12+
return (new B());
13+
else if (i == 2)
14+
return (new C());
1515
return (NULL);
1616
}
1717
void identify(Base* p)

cpp_module_06/ex02/Base.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <iostream>
55
#include <exception>
6+
#include <ctime>
67

78
class Base
89
{

cpp_module_06/ex02/main.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
int main(void)
44
{
5+
std::srand(std::time(NULL));
6+
57
Base *type1 = generate();
68
Base *type2 = generate();
79
Base *type3 = generate();

0 commit comments

Comments
 (0)