I have been searching for hours an issue similar to the one I'm experiencing right now, but I can't find anything, nor here nor in other websites.
#include <iostream>
using namespace std;
int main()
{
double Numero=0, ParteDecimale=0, Controllo=0, Controllo2=0;
int ParteIntera=0;
string Var1="1", Var0="0", NumeroFinale;
cout<<"\n\n\tBenvenuto, questo programma ti consente di convertire la parte\n"
"\tdecimale di un numero in sistema binario. Per iniziare, inserisci un numero: ";
cin>>Numero;
cout<<Numero<<" Numero\n\n"<<endl; //
ParteIntera=Numero; //casting implicito, in modo da poter isolare esclusivamente la parte decimale del numero inserito.
cout<<ParteIntera<<" ParteIntera\n\n"<<endl; //
ParteDecimale=Numero-ParteIntera; //Isolamento della parte decimale
cout<<ParteDecimale<<" ParteDecimale\n\n"<<endl; //
Controllo2=ParteDecimale;
do{
ParteDecimale=ParteDecimale*2; //Moltiplicazione della parte decimale per due
cout<<"\n\nParteDecimaleCiclo "<<ParteDecimale<<endl; //
if(ParteDecimale<1){
NumeroFinale=NumeroFinale+Var0;
cout<<"\n\nNumeroFinaleMin1 "<<NumeroFinale<<endl; //
}
else{
NumeroFinale=NumeroFinale+Var1;
cout<<NumeroFinale<<" NumeroFinaleMagg1\n\n"<<endl; //
ParteDecimale-=1;
cout<<ParteDecimale<<" ParteDecimaleMagg1\n\n"<<endl;
}
Controllo=ParteDecimale;
cout<< "\n\nCONTROLLO " <<Controllo; //
cout<<"\n\nNUMERO "<<Numero; //
}
while(Controllo!=Controllo2);
cout<<NumeroFinale<<endl;
}
Sorry if the code is messy, but when I saw that it was not working as expected I decided to output every value to follow along with the process, and that's the weird thing.. The code acts exactly as expected and the results are all right, it just refuses to exit the do-while loop even if the condition is satisfied.
For example:
-I input "12.2", the program store the number inside the "ParteIntera" variable (which is an int, I'm doing that to remove the decimal part);
-It stores "Numero-ParteIntera" inside "ParteDecimale" (Numero is the input I entered at the beginning), and at this point "ParteDecimale" is equal to 0.2;
-The value of "ParteDecimale" is stored inside "Controllo2";
-Here starts the Do-While loop, that basically does "ParteDecimale*2", so it's "0.4";
-It checks if ParteDecimale is bigger or smaller than 1, and based on this it executes the istructions inside the if statements;
-The value of "ParteDecimale" that is obtained from the if statement is then stored inside "Controllo" (In this case it's 0.4);
-The condition is checked, in this case "Controllo" and "Controllo2" are still different so the loop starts over;
At a certain point ParteDecimale will be 0.6, so when the loop starts ParteDecimale become 1.2 (0.6*2). Since it's bigger than 1, the else statement is executed and ParteDecimale become 0.2 (1.2-1). At this point "Controllo" and "Controllo2" are both 0.2, so the condition inside the do-while is not satisfied anymore and the loop should end.
BUT
I can't understand why, it is not ending. If you run the code you will see (you have to quickly click somewhere inside the cmd to temporarily stop the loop) that all the steps are right, probably it's a stupid mistake or something I can't see, but right now I don't know where to bang my head anymore.
Thanks for reading this very long post, any help is appreciated, and sorry for my bad english!