Hello everyone, i want to make a program which writes the number whose you input the reading. i made one which writes the reading of number but i cant do this one. can anyone help?
/*******************************************************************************
*******************************************************************************
** **
** programma 'numeri in lettere' **
** **
*******************************************************************************
** **
** questo programma legge un numero MINORE di 10.000 e lo scrive in lettere **
** **
*******************************************************************************
*******************************************************************************/
#include <iostream>
#include <string>
#include <assert.h>
usingnamespace std;
int numero,migliaia,centinaia,decine,unita;
/*******************************************************************************
**************************** funzione scompositrice ***************************
** NOME: scompositrice (funzione con variabile parametrica) **
** DESCRIZIONE: la procedura deframmenta un numero inferiore a 10.000, in **
** migliaia,centinaia,decine ed unità **
** PARAMETRI: si utilizza un parametro riferimento alla variabile 'numero' **
** RISULTATI: il valore nominale di migliaia,centinai,decine ed unità **
** NOTE: la funzione può essere migliorata **
*******************************************************************************/
int scompositrice(int& num)
{
migliaia=0;
centinaia=0;
decine=0;
unita=0;
migliaia=num/1000;
int c=num%1000;
centinaia=c/100;
int d=c%100;
decine=d/10;
unita=d%10;
return (c);
}
/************************************************* START PROGRAM **************************************************/
int main()
{
while (true)
{
numero=0;
cout<<"inserisci un numero intero da leggere, compreso tra 0 e 9999: "<<'\n';
cin>>numero;
if ((numero>9999)||(numero<0))
{
cout<<"numero fuori range. statte buenu. ciao";
break;
}
scompositrice(numero);
if (numero==0)
cout<<"zero"<<'\n';
else
{
switch (migliaia)
{
case 1:
cout<<"mille";
break;
case 2:
cout<<"duemila";
break;
case 3:
cout<<"tremila";
break;
case 4:
cout<<"quattromila";
break;
case 5:
cout<<"cinquemila";
break;
case 6:
cout<<"seimila";
break;
case 7:
cout<<"settemila";
break;
case 8:
cout<<"ottomila";
break;
case 9:
cout<<"novemila";
break;
default:
break;
}
switch (centinaia)
{
case 1:
cout<<"cento";
break;
case 2:
cout<<"duecento";
break;
case 3:
cout<<"trecento";
break;
case 4:
cout<<"quattrocento";
break;
case 5:
cout<<"cinquecento";
break;
case 6:
cout<<"seicento";
break;
case 7:
cout<<"settecento";
break;
case 8:
cout<<"ottocento";
break;
case 9:
cout<<"novecento";
break;
case 0:
default:
break;
}
switch (decine)
{
case 1:
{
switch (unita)
{
case 0:
cout<<"dieci";
break;
case 1:
cout<<"undici";
break;
case 2:
cout<<"dodici";
break;
case 3:
cout<<"tredici";
break;
case 4:
cout<<"quattordici";
break;
case 5:
cout<<"quindici";
break;
case 6:
cout<<"sedici";
break;
case 7:
cout<<"diciassette";
break;
case 8:
cout<<"diciotto";
break;
case 9:
cout<<"diciannove";
break;
default:
break;
}
break;
}
case 2:
{
if ((unita==1)||(unita==8))
{
cout<<"vent";
}
else
cout<<"venti";
}
break;
case 3:
{
if ((unita==1)||(unita==8))
{
cout<<"trent";
}
else
cout<<"trenta";
}
break;
case 4:
{
if ((unita==1)||(unita==8))
{
cout<<"quarant";
}
else
cout<<"quaranta";
}
break;
case 5:
{
if ((unita==1)||(unita==8))
{
cout<<"cinquant";
}
else
cout<<"cinquanta";
}
break;
case 6:
{
if ((unita==1)||(unita==8))
{
cout<<"sessant";
}
else
cout<<"sessanta";
}
break;
case 7:
{
if ((unita==1)||(unita==8))
{
cout<<"settant";
}
else
cout<<"settanta";
}
break;
case 8:
{
if ((unita==1)||(unita==8))
{
cout<<"ottant";
}
else
cout<<"ottanta";
}
break;
case 9:
{
if ((unita==1)||(unita==8))
{
cout<<"novant";
}
else
cout<<"novanta";
}
break;
case 0:
default:
break;
}
if ((decine!=1)&&(unita!=0))
{
switch (unita)
{
case 1:
cout<<"uno";
break;
case 2:
cout<<"due";
break;
case 3:
cout<<"tre";
break;
case 4:
cout<<"quattro";
break;
case 5:
cout<<"cinque";
break;
case 6:
cout<<"sei";
break;
case 7:
cout<<"sette";
break;
case 8:
cout<<"otto";
break;
case 9:
cout<<"nove";
break;
case 0:
cout<<"zero";
default:
break;
}
}
cout<<'\n';
}
}
return 0;
}
thanks for answer but i dont want a program converts numbers to letters. i want a program which gives the number when you input letters
sorry for my english again
however, what you wrote on your surce code, is just the opposite of what you say. that's why I entered my code. to do what you want, you should assign a value to each letter or assign the kind words ("hundred") at No. 100.
you could use a vector and a char "type casting" to force the char with int ... but I've never tried ..
but i dont know how can i break the letter into pieces. i mean when user input one hundred twenty-one how can i break it into pieces like "hundred" "twenty" and "one"
I gave you an answer but you didn't read carefully.
"one hundred twelve"
break this up into the constituent tokens and process them one at a time, grouping by threes:
"one" (a digit)-->+1
"hundred" (a power)-->*100
"twelve" (a pair of digits)-->+12
(1*100)+12 = 112
Another example:
"fourty-five thousand and seventy-three"
"fourty" (digit) --> +40
"five" (digit) --> +5
"thousand" (power)
We pause here to assemble our number (40 + 5 = 45) * 1000 = 45000. Add this 'group by three' number to our final total: 0 + 45000 = 45000.
Continuing:
"seventy" (digit) --> +70
"three" (digit) --> +3
That's the end, so assemble our 'group by three' number: 70 + 3 = 73. Add it to the final total: 45000 + 73 = 45073.