I must make a program (using functions) which will enter the number (integer) desired on the screen and this converts it into Roman numerals. What happens is that when calling the functions, they do not execute as expected.
It is possible that I am using them in the wrong way, so I would appreciate if you could explain to me how I can adjust my program so that it runs correctly.
#include <iostream>
using namespace std;
int numero;
int unidades (int){
int unidades;
unidades = numero % 10; // 1
numero /= 10; // 389
switch(unidades){
case 1: cout<<"I";break;
case 2: cout<<"II";break;
case 3: cout<<"III";break;
case 4: cout<<"IV";break;
case 5: cout<<"V";break;
case 6: cout<<"VI";break;
case 7: cout<<"VII";break;
case 8: cout<<"VIII";break;
case 9: cout<<"IX";break;
}
}
int decenas (int){
int decenas;
decenas = numero % 10; // 9
numero /= 10; // 38
switch(decenas){
case 1: cout<<"X";break;
case 2: cout<<"XX";break;
case 3: cout<<"XXX";break;
case 4: cout<<"XL";break;
case 5: cout<<"L";break;
case 6: cout<<"LX";break;
case 7: cout<<"LXX";break;
case 8: cout<<"LXXX";break;
case 9: cout<<"XC";break;
}
}
int centenas (int){
int centenas;
centenas = numero % 10; // 8
numero /= 10; // 3
switch(centenas){
case 1: cout<<"C";break;
case 2: cout<<"CC";break;
case 3: cout<<"CCC";break;
case 4: cout<<"CD";break;
case 5: cout<<"D";break;
case 6: cout<<"DC";break;
case 7: cout<<"DCC";break;
case 8: cout<<"DCCC";break;
case 9: cout<<"CM";break;
}
}
int millar (int){
int millar;
millar = numero; // 3
switch(millar){
case 1: cout<<"M";break;
case 2: cout<<"MM";break;
case 3: cout<<"MMM";break;
}
}
int main (){
int numero;
cout<<"Ingrese un numero entero para convertilo en romano: "<<endl;
cin>>numero; // 3891
unidades(numero);
decenas(numero);
centenas(numero);
millar(numero);
return 0;
}
When executing the program, it asks me for the number and after entering it, it ends. It doesn't do anything that's in the functions.
Dear several problems:
I pass you a modified version, there are many improvements still to be done.
Observations:
numero
is not the same as the local variablenumero
.Functions are declared (you indicate their existence and do not define their behavior) as follows:
Parameters are declared in order with their type and optionally their name.
Example of a function that receives two integers and returns an integer:
You can omit the name of the parameters, this expression is equivalent to the previous one.
Example of a function that does not receive parameters and does not return anything:
As you can see,
void
it means that it returns nothing.The functions are defined as follows:
The parameters are indicated in an ordered way with their type and their name (you cannot omit it as in the declaration). And if the function has a return type other than
void
, you must return a value of the same type withreturn
.Example of a function that receives two integers and returns an integer:
Example of a function that receives an integer and returns nothing:
Assuming you don't want to return anything, the code looks something like this:
There is still a problem. In the functions you modify the parameter
numero
:I understand that what you want is to modify the variable
numero
of the functionmain
. But the parameter only contains a copy of your variable.To solve it, I have two options:
Pass parameters by reference, so the original variable is affected:
The downside of this solution is that it doesn't work if you want to call the function with a literal:
Do your functions without having to change the value of the original variable.
You can test the program with the latest fixes here .