From 570bee8104cec7078306800bac9d5e602833313e Mon Sep 17 00:00:00 2001 From: aortigos Date: Wed, 18 Mar 2026 15:16:11 +0100 Subject: [PATCH] Fixed char without quotes --- ex00/ScalarConverter/ScalarConverter.cpp | 35 ++++++++++++++++-------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/ex00/ScalarConverter/ScalarConverter.cpp b/ex00/ScalarConverter/ScalarConverter.cpp index f9b7197..657f11e 100644 --- a/ex00/ScalarConverter/ScalarConverter.cpp +++ b/ex00/ScalarConverter/ScalarConverter.cpp @@ -49,12 +49,12 @@ void ScalarConverter::convert(const std::string &literal) convertFromSpecialCase(literal); else if (isChar(literal)) convertFromChar(literal); - else if(isInt(literal)) - convertFromInt(literal); else if(isFloat(literal)) convertFromFloat(literal); else if (isDouble(literal)) convertFromDouble(literal); + else if(isInt(literal)) + convertFromInt(literal); else std::cout << "Error: invalid literal" << std::endl; } @@ -73,14 +73,20 @@ bool ScalarConverter::isChar(const std::string &literal) { char c; - if (literal.length() != 3) - return (false); - c = literal[1]; - if (literal[0] == '\'' && literal[2] == '\'' - && (c >= 32 && c < 127)) - { - return (true); - } + if (literal.length() == 3) + { + c = literal[1]; + if (literal[0] == '\'' && literal[2] == '\'' + && (c >= 32 && c < 127)) + { + return (true); + } + } else if (literal.length() == 1) + { + c = literal[0]; + if (c >= 32 && c < 127 && !std::isdigit(c)) + return (true); + } return (false); } @@ -101,6 +107,8 @@ bool ScalarConverter::isDouble(const std::string &literal) { char *end; + if (literal.find('.') == std::string::npos) + return (false); strtod(literal.c_str(), &end); if (*end != '\0') return (false); @@ -134,9 +142,12 @@ void ScalarConverter::convertFromSpecialCase(const std::string &literal) void ScalarConverter::convertFromChar(const std::string &literal) { - char c; + char c = 0; - c = literal[1]; + if (literal.length() == 3) + c = literal[1]; + else if (literal.length() == 1) + c = literal[0]; std::cout << "char: '" << c << "'" << std::endl; std::cout << "int: " << static_cast(c) << std::endl; std::cout << std::fixed << std::setprecision(1);