1
#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?

Atomillo
  • 11
  • 2
  • Can you provide a sample input, the result you're trying to get for it and the actual (wrong) result? – Mureinik Sep 28 '17 at 16:50
  • How could I check the floating point? And hwo can I upload a photo? Im a bit newbie – Atomillo Sep 28 '17 at 16:53
  • Don't upload photos. Copy text from editor or console to clipboard, then paste into your question. – Thomas Matthews Sep 28 '17 at 16:55
  • |----------------------------------------------| | Conversor de decimal a fraccionario. | |----------------------------------------------| Introduce el numero que deseas convertir a fraccionario1,98 Numerador:1 Denominador:1 -------------------------------- Process exited after 7.139 seconds with return value 0 Presione una tecla para continuar . . . – Atomillo Sep 28 '17 at 16:57
  • I have seen the possible duplicate but I dont know how to make a workaround for the program. Can someone help on this? – Atomillo Sep 29 '17 at 17:26

0 Answers0