string scriviNumeroMinoreMille(int a)
{
vector<string> v_zero_to_nineteen(20);
vector<string> v_twenty_to_ninety(8);
v_zero_to_nineteen = {"zero", "uno", "due", "tre", "quattro", "cinque", "sei", "sette", "otto", "nove", "dieci", "undici",
"dodici", "tredici", "quattordici", "quindici", "sedici", "diciassette", "diciotto", "diciannove"};
v_twenty_to_ninety = {"venti", "trenta", "quaranta", "cinquanta", "sessanta", "settanta", "ottanta", "novanta"};
string risultato;
if(a == 0)
{
return risultato = "";
}
else if(a < 20 && a > 0)
{
return risultato = v_zero_to_nineteen[a];
}
else if(a == 20 || a == 30 || a == 40 || a == 50 || a == 60 || a == 70 || a == 80 || a == 90)
{
return risultato = v_twenty_to_ninety[(a/10)-2];
}
else if(a == 100)
{
return risultato = "cento";
}
int unita = 0;
int decine = 0;
int centinaia = 0;
if(a > 99)
{
centinaia = a/100;
risultato += (v_zero_to_nineteen[centinaia]);
risultato += "cento";
}
if(a > 19)
{
if(a-(centinaia*100)%10 == 0)
{
decine = (a-(centinaia*100))/10;
risultato += (v_twenty_to_ninety[decine-2]);
}
else
{
decine = (a-(centinaia*100)/10);
risultato += v_twenty_to_ninety[decine-2];
unita = (a-(centinaia*100)-(decine*10));
risultato += v_zero_to_nineteen[unita];
}
}
return risultato;
}
I made this function to check a number from 0 to 999 to convert a number in a string. The result would be for example if the input is 100, the output would be “one hundred” (it’s translated in italian)
I don’t understand why in debugging the arguments inside these two if clauses:
if(a > 99)
{
centinaia = a/100;
risultato += (v_zero_to_nineteen[centinaia]);
risultato += "cento";
}
if(a > 19)
{
if(a-(centinaia*100)%10 == 0)
{
decine = (a-(centinaia*100))/10;
risultato += (v_twenty_to_ninety[decine-2]);
}
else
{
decine = (a-(centinaia*100)/10);
risultato += v_twenty_to_ninety[decine-2];
unita = (a-(centinaia*100)-(decine*10));
risultato += v_zero_to_nineteen[unita];
}
}
are not considered in the function. Those two ifs are just skipped. I don’t understand why. If I put 123 as the input those ifs are skipped. But the condition is if(a > 99), 120 is more than 99. I don’t understand.
there are several errors in your code:
First the += operator of the string type takes a “const string&”, so instead of doing this:
you need to pass it like this:
also here
you are using “centinaia” which at this point is zero because you only made the calculation inside the
if(a > 99)if block and also the /10 should be outside the parentesis like this:So you need to move this
centinaia = a/100;outside the if, so all 3 if statement gets the calculated variable (same with the other two).this:
to this:
I made this changes to your method, It now look like this:
For the 123 input it outputs “unocentoventitre”, I dont know if thats correct since I dont speak italian xD
Hope it helps.
EDIT: forgot to remove/change back the lines I inserted for testing.