I'm having some trouble converting a value from double to int. After my conversion of the number with value of 1 and 2 in the tenth position, the value being converted is actually deducted by one. Can someone help me fix this problem?
#include <iostream>
#include <cstring>
#include <cmath>
typedef char* BookName;
using namespace std;
class DecimalBookInfo
{
public:
DecimalBookInfo(char inName[], double number);
private:
BookName name;
double code;
bool verifyCode(double number);
};
int main()
{
char name[] = "TestBook";
DecimalBookInfo testBook(name, 230.1);
}
DecimalBookInfo::DecimalBookInfo(char inName[], double number)
{
// initiate new cstring
name = new char[strlen(inName)];
strcpy(name, inName);
if (verifyCode(number))
code = number;
else
{
cout << "Wrong code...\n";
exit(1);
}
}
bool DecimalBookInfo::verifyCode(double number)
{
double areaCode = floor(number);
cout << areaCode << endl;
double subCode = ((number - areaCode) * 100.0);
cout << static_cast<int>(subCode) << endl;
if (areaCode > 999 || areaCode < 100 ||
((subCode - 10) != 0))
{
return false;
}
return true;
}
The result is:
230 9 Wrong code...
so I'm not sure why this is so. The number after 230 is supposed to be 10. Thank you