Skip to content

Commit 930c6be

Browse files
committed
chore: Refactor BitcoinExchange loadDataBase to handle bad input values and improve error handling
1 parent 13f75a5 commit 930c6be

2 files changed

Lines changed: 15 additions & 17 deletions

File tree

module 09/ex00/BitcoinExchange.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -120,20 +120,19 @@ void BitcoinExchange::loadDataBase(const std::string &filename)
120120
if (!isValidDate(date))
121121
throw std::runtime_error("Error at line " + intToString(lineCount) + ": invalid date => " + date);
122122

123-
try
124-
{
125-
if (valueStr.find_first_not_of("0123456789.-") != std::string::npos)
126-
throw std::runtime_error("Error at line " + intToString(lineCount) + ": invalid value => " + valueStr);
127-
float value = atof(valueStr.c_str());
128-
129-
if (value < 0)
130-
throw std::runtime_error("Error at line " + intToString(lineCount) + ": negative value => " + valueStr);
131-
_database[date] = value;
132-
}
133-
catch (const std::exception &e)
134-
{
123+
std::stringstream ss2(valueStr);
124+
float value;
125+
ss2 >> value;
126+
127+
if (!ss2.eof())
135128
throw std::runtime_error("Error at line " + intToString(lineCount) + ": invalid value => " + valueStr);
136-
}
129+
130+
131+
if (value < 0)
132+
throw std::runtime_error("Error at line " + intToString(lineCount) + ": negative value => " + valueStr);
133+
134+
_database[date] = value;
135+
137136
}
138137
if (_database.empty())
139138
throw std::runtime_error("Error: empty database");

module 09/ex02/PmergeMe.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,8 @@ void PmergeMe::sortDeque()
241241
int idx = insertionOrder[i];
242242
if (idx > 0 && idx < (int)pendChain.size())
243243
{
244-
int elementToInsert = pendChain[idx];
245-
std::deque<int>::iterator pos = std::lower_bound(result.begin(), result.end(), elementToInsert);
246-
result.insert(pos, elementToInsert);
244+
std::deque<int>::iterator pos = std::lower_bound(result.begin(), result.end(), pendChain[idx]);
245+
result.insert(pos, pendChain[idx]);
247246
}
248247
}
249248
}
@@ -275,7 +274,7 @@ void PmergeMe::sort()
275274
// {
276275
// if (_vec[i] < _vec[i - 1])
277276
// {
278-
// std::cerr << "Error: std::vector is not sorted" << std::endl;
277+
// std::cerr << "Error: std::vector is no sorted" << std::endl;
279278
// return;
280279
// }
281280
// }

0 commit comments

Comments
 (0)