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.



LinkBack URL
About LinkBacks



