Hi
Which of the following codes, CODE 1 and CODE 2, is more correct and understandable?
What's the need of converting "temp" into float again? Why don't we leave it as double? Or, just make it int?
CODE 1
Code:
// ref.cpp
// demonstrates passing by reference
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
void intfrac(float dumnumber, float& dumintpart, float& dumbfracpart);
float number, intpart = 0, fracpart = 0;
do {
cout << "Enter a real number: ";
cin >> number;
intfrac(number, intpart, fracpart);
cout << "Integer part is " << intpart
<< ", fraction part is " << fracpart << endl;
} while( number != 0.0 );
system("pause");
return 0;
}
//--------------------------------------------------------------
// intfrac()
// finds integer and fractional parts of real number
void intfrac(float dumnumber, float& dumintpart, float& dumfracpart)
{
long temp = static_cast<long>(dumnumber);
dumintpart = static_cast<float>(temp);
dumfracpart = dumnumber - dumintpart;
}
//---------------------------------------------------------------
CODE 2
Code:
// ref.cpp
// demonstrates passing by reference
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
void intfrac(float dumnumber, float& dumintpart, float& dumbfracpart);
float number, intpart, fracpart;
do {
cout << "Enter a real number: ";
cin >> number;
intfrac(number, intpart, fracpart);
cout << "Integer part is " << intpart
<< ", fraction part is " << fracpart << endl;
} while( number != 0.0 );
system("pause");
return 0;
}
//--------------------------------------------------------------
// intfrac()
// finds integer and fractional parts of real number
void intfrac(float dumnumber, float& dumintpart, float& dumfracpart)
{
long temp = static_cast<long>(dumnumber);
dumintpart = static_cast<float>(temp);
dumfracpart = dumnumber - dumintpart;
}
//---------------------------------------------------------------