Super C++ newbie needs help! (converting double to int)

Discussion in 'Programming' started by allah ackbar, Jan 28, 2007.

  1. #1
    Hi all,


    I'm working on a program that will compute difference, and compute the fifty, twenty, ten, five, and one dollar bills, and the number of quarters, dimes, nickels, and pennies that the customer should receive in return.

    I've broken the double difference between the amount of cash received and amount received into two integers (dollars and cents). It usually converts everything well until you throw in an input like 17.01 for amount due and 18.00 for the amount received, when it gives you 98 cents back, where it should be giving you 99 cents.

    The problem, I think, can basically be localized to the void diff function.

    What is going on here?? Any help would be nice.

    
    #include <iostream>
    #include <iomanip>
    #include <math.h>
    
    using std::cout;
    using std::cin;
    using std::setprecision;
    using std::ios;
    using std::string;
    using std::endl;
    
    void welcomeMsg ();
    void getAmtDueAmtRec (double &, double &);
    void diff (double &, double, double, int &, int &);
    void changeCalc (int, int, int [][2], int [][2]);
    void dispChange (double, int [][2], int [][2], string [], string []);
    char rerunDisplay ();
    int main ()
    {
    double amtDue(0.00), amtRec(0.00);
    int dollars(0), cents(0);
    double difference=0.0;
    int bills [5][2]={{50, 0}, {20, 0}, {10,0}, {5, 0}, {1, 0}};
    int coins [4][2]={{25, 0}, {10, 0}, {5, 0}, {1, 0}};
    string labelDol[5]={"fifty dollar bill(s)", "twenty dollar bill(s)", "ten dollar bill(s)", "five dollar bill(s)", "one dollar bill(s)"};
    string labelCoins [4]={"quarter(s)", "dime(s)", "nickel(s)", "penny(ies)"};
    char restartAnswer=' ';
    
    welcomeMsg ();
    do
    {
    getAmtDueAmtRec (amtDue, amtRec);
    diff (difference, amtDue, amtRec, dollars, cents);
    changeCalc (dollars, cents, bills, coins);
    dispChange (difference, bills, coins, labelDol, labelCoins);
    restartAnswer=rerunDisplay();
    amtDue=amtDue*0;
    dollars=dollars*0;
    amtRec=amtRec*0;
    cents=cents*0;
    difference=difference*0;
    for (int u=0;u<5;u++)
    {
    bills[u][1]=bills[u][1]*0;
    }
    
    for (int t=0;t<4;t++)
    {
    coins[t][1]=coins[t][1]*0;
    }
    }while (restartAnswer=='y');
    
    return 0;
    }
    
    
    void welcomeMsg ()
    {
    cout<<"\n***This program will determine the amount of money returned in change based on the amount due***\n";
    return;
    }
    
    
    void getAmtDueAmtRec (double &amtDue, double &amtRec)
    {
    
    while (amtDue<= 0)//if amtDue is less than or equal to zero, the program will force the user to re-enter
    {
    cout<<"\nPlease enter the amount due:  ";
    cin>>amtDue;
    
    if (amtDue<=0)
    cout<<"\nYou did not enter an amount greater than zero!  Please re-enter!\n";
    }
    
    
    while (amtRec<amtDue)//if amtRec is less than amtDue, the program will force the user to re-enter
    {
    cout<<"\nPlease enter the amount received:  ";
    cin>>amtRec;
    
    if (amtRec<amtDue)
    cout<<"\nYou did not enter an amount greater than or equal to the amount due!  Please re-enter!\n";
    }
    
    
    return;
    }
    
    void diff (double &difference, double amtDue, double amtRec, int &dollars, int &cents)
    {
    double doubcents=0;
    double amtRecCents=0;
    double amtDueCents=0;
    double control=0;
    
    
    difference=amtRec-amtDue;
    dollars=int(difference);
    doubcents=difference-floor(difference);
    doubcents=doubcents*100;//converts doubcents into a number that can be converted into an int
    cents=static_cast<int>(doubcents);//cents gets assigned doubcents as a static_cast<int>
    cout<<"\n"<<cents<<"\n"<<doubcents;
    return;
    }
    
    void changeCalc (int dollars, int cents, int bills [][2], int coins [][2])
    {
    int x=0;
    int amtOf=0;
    int remain=0;
    remain=remain+dollars;
    while (remain != 0)
    {
    amtOf=remain/bills[x][0];//divides dollars remaining by bill values stored in bills[x][0] and stores as amtOf
    if (amtOf>= 1)
    {
    remain=remain % bills [x][0];//if amtOf is >1, the remainder of remain/bills[x][0] is stored as remain
    bills [x][1]=bills [x][1]+amtOf;//adds amtOf to bills[x][1] to store it as the amount of bills of that type needed
    }
    x++;
    }
    //loop is continued until remain equals 0
    
    
    x=x*0;
    remain=remain+cents;
    while (remain != 0)
    {
    amtOf=remain/coins[x][0];//divides cents remaining by coin values stored in coins[x][0] and stores as amtOf
    if (amtOf>= 1)
    {
    remain=remain % coins [x][0];//if amtOf is >1, the remainder of remain/coins[x][0] is stored as remain
    coins [x][1]=coins [x][1]+amtOf;//adds amtOf to coins[x][1] to store it as the amount of coins of that type needed
    }
    x++;
    }
    //loop is continued until remain equals 0
    
    
    return;
    
    }
    
    void dispChange (double difference, int bills [][2], int coins [][2], string labelDol [], string labelCoins [])
    {
    
    cout<<"\nThe difference is $"<<difference;
    cout<<"\nPlease give the customer: ";
    for (int q=0;q<5;q++)
    {
    if (bills [q][1]>=1)
    cout<<"\n"<<bills [q][1]<<" "<<labelDol[q]<<".";
    }
    
    for (int s=0;s<4;s++)
    {
    if (coins [s][1]>=1)
    cout<<"\n"<<coins [s][1]<<" "<<labelCoins[s]<<".";
    }
    
    return;
    
    }
    
    char rerunDisplay ()
    {
    char repeatDecision= ' ';
    bool validDecision=false;
    
    	do
    	{
    	cout << "\nWould you like to repeat the program? (y/n): ";
    	
    	cin >> repeatDecision;
    	repeatDecision=tolower(repeatDecision);
    switch (repeatDecision)
    {
    	case 'y':	validDecision=true;
    	break;
    	case 'n':	validDecision=true;
    	break;
    	default:	validDecision=false; cout<<"\nPlease enter either y or n!\n";
    }
    	}while (validDecision==false);
    return repeatDecision;
    }
    
    
    Code (markup):
     
    allah ackbar, Jan 28, 2007 IP
  2. Gyrus

    Gyrus Guest

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I've tried to compile your program with bcc32.

    At first add #include <ctype.h> to avoid error of compilation
    (because "tolower" function is not always declared)

    Besides there is a pack of warnings aka "xxx is assigned a value that is never used"

    To convert to integer try cents=(int)(doubcents+0.5);
    Not sure, but it is normal relacement for "floor"-function...

    BUY THE WAY
    If you enter a pair of ("18.00", "17.00") and accept error,
    the next result which you will enter correctly will give always ZERO.

    Think a little on this....:cool:
     
    Gyrus, Jan 30, 2007 IP