#include <iostream>
using namespace std;
float numero = 0;
float numerador = 0;
float denominador = 1;
int main() {
cout<<"|----------------------------------------------|";
cout<<'\n';
cout<<"| Conversor de decimal a fraccionario. |";
cout<<'\n';
cout<<"|----------------------------------------------|";
cout<<'\n';
cout<<"Introduce el numero que deseas convertir a fraccionario";
cin>> numero;
cout<<'\n';
numerador = numero;
if (numerador - (int)numerador != 0)
{
numerador = numerador*10;
denominador = denominador*10;
}
else
{
cout<<"Numerador:";
cout<<numerador;
cout<<'\n';
cout<<"Denominador:";
cout<<denominador;
cout<<'\n';
}
return 0;
}
Hi! I am making this little project to help me with my homework of maths. The objective is to create a fraction out of a decimal number. I tried to check if the number has decimals, and if it has then multiply it for ten and increasy another variable (denominador) by ten. But when I tried running it I only when a numerador of 1 and a denominador of 1. I have tried revising it and I have it shown to a friend, but sadly he knows better with Python that with C++. So my question here is, where is the problem? PS: sorry for the Spanish and my bad English, I am not native and my language is not the best. EDIT: I think that is a floating point error, and I have searching through the forums and even though the are plenty of topics about it I can not find a possible solution, aside from doing int and rounding down, but I dont know how it could work or if it would work. Somebody?