-
Converting data types
I am trying to convert a float to an int. I'm making a change calculator where the user enters an amount. I didn't want the result to be off because I've had results from floats come back 1 away from the correct answer. So this is what I have so far.
Code:
/*
FILE: Change_Calculator.cpp
PROGRAMMER: Stephen Michael Croy
DATE: 01-07-09
LAB: 1
This program calculates the change from an amount input by the user
*/
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
//the following are the constants for this program
const int HUNDRED = 10000;
const int FIFTY = 5000;
const int TWENTY = 2000;
const int TEN = 1000;
const int FIVE = 500;
const int ONE = 100;
const int QUARTER = 25;
const int DIME = 10;
const int NICKEL = 5;
const int PENNY = 1;
//entered by user
float amount;
float payment;
int intAmount;
int intPayment;
int total;
cout << "Enter the amount of purchase: "; //Prompt for data
cin >> amount; //read information from user into amount
cout << "Enter payment toward purchase: ";
cin >> payment; //read information from user into payment
intAmount = amount * 100;
intPayment = payment * 100;
if (intAmount > intPayment)
total = intAmount - intPayment;
cout << total;
system("PAUSE");
return EXIT_SUCCESS;
}
I know the program isn't finished, I'm just hung up on this conversion.
-
So if you multiplied the amount and payment by 100, then you must divide the total by 100.
Also check out alternatives to system("pause"): http://apps.sourceforge.net/mediawik...=Pause_console
-
In order to avoid issues with truncation, a quick fix would be
Code:
intAmount = amount * 100 + 0.5;
intPayment = payment * 100 + 0.5;
The reason is that a float * 100 may end up just a tiny bit below the actual value due to approximate nature of floating point values, e.g 0.11 * 100 = 10.999996 and if you truncate it to int it will become 10.
However, I think normally you should just avoid floating point values where the approximation errors are unacceptable. E.g take the input as a string, parse it to two integers, multiply the first part by 100 and add the second part as needed (avoid errors where user enters 10.5 or 2.345).
Code:
const int HUNDRED = 10000;
const int FIFTY = 5000;
const int TWENTY = 2000;
const int TEN = 1000;
const int FIVE = 500;
const int ONE = 100;
const int QUARTER = 25;
const int DIME = 10;
const int NICKEL = 5;
const int PENNY = 1;
When you continue you'll probably find that this will be better off as an array (perhaps even a map of numeric values and string names).
-
Thank you for the suggestions. Although they were helpful, they weren't exactly what I was looking for. I just want to know how to convert the float into an int.
-
But you have been given suggestions on how to do it...
Floats are not exact either, so there may be precision errors when converting to/from floats.
-
Elysia is right. There are a few ways to do it but the results may not be exactly what you want. You can use bit the bit-wise operators to get values. There is a lot of info on the net about that. Other than that you can:
float x = 5.2;
static_cast<int>(x);
that will just give you 5 because it is going to drop everything after the decimal. But it will convert the float into an int.
-
There are ways to store money as integers by storing the values as BCDs.