Fixed char without quotes

This commit is contained in:
aortigos
2026-03-18 15:16:11 +01:00
parent 6105b150f3
commit 570bee8104

View File

@@ -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<int>(c) << std::endl;
std::cout << std::fixed << std::setprecision(1);