Hi,
I'm learning c++ got assigned a project, and have written the program. Now the programs all written, compiles nicely no errors (using turbo c++ while at home, and use borland c++ 5.02 at school). Now I run the program input my data and all the answers (data) it produces is correct except for 1 value which is always off by 1. The pennies value is ALWAYS off by 1 in its finaly dispay except if its supposed to be 0. Your comments and assistance would be great.
I am including the source code for the actual function that does the computation below:
----------------------------------------------------------------------------------------------------------------------------------------------------------------
void change(double change_break) // change_break is the actual decimal amount like ex. 0.84 or 1.56
{
int dollars, quarters, dimes, nickels, pennies;
double cents, temp1, temp2;
dollars = change_break / 1;
cents = change_break - dollars;
quarters = cents / .25;
temp1 = quarters * .25;
temp2 = cents - temp1;
cents = temp2;
dimes = cents / .10;
temp1 = dimes * .10;
temp2 = cents - temp1;
cents = temp2;
nickels = cents / .05;
temp1 = nickels * .05;
temp2 = cents - temp1;
cents = temp2;
pennies = cents / .01;
temp1 = pennies * .01;
temp2 = cents - temp1;
cents = temp2;
cout << " Change: " << change_break << endl;
cout << " Dollars: " << dollars << endl;
cout << "Quarters: " << quarters << endl;
cout << " Dimes: " << dimes << endl;
cout << " Nickels: " << nickels << endl;
cout << " Pennies: " << pennies << endl;
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
I'm learning c++ got assigned a project, and have written the program. Now the programs all written, compiles nicely no errors (using turbo c++ while at home, and use borland c++ 5.02 at school). Now I run the program input my data and all the answers (data) it produces is correct except for 1 value which is always off by 1. The pennies value is ALWAYS off by 1 in its finaly dispay except if its supposed to be 0. Your comments and assistance would be great.
I am including the source code for the actual function that does the computation below:
----------------------------------------------------------------------------------------------------------------------------------------------------------------
void change(double change_break) // change_break is the actual decimal amount like ex. 0.84 or 1.56
{
int dollars, quarters, dimes, nickels, pennies;
double cents, temp1, temp2;
dollars = change_break / 1;
cents = change_break - dollars;
quarters = cents / .25;
temp1 = quarters * .25;
temp2 = cents - temp1;
cents = temp2;
dimes = cents / .10;
temp1 = dimes * .10;
temp2 = cents - temp1;
cents = temp2;
nickels = cents / .05;
temp1 = nickels * .05;
temp2 = cents - temp1;
cents = temp2;
pennies = cents / .01;
temp1 = pennies * .01;
temp2 = cents - temp1;
cents = temp2;
cout << " Change: " << change_break << endl;
cout << " Dollars: " << dollars << endl;
cout << "Quarters: " << quarters << endl;
cout << " Dimes: " << dimes << endl;
cout << " Nickels: " << nickels << endl;
cout << " Pennies: " << pennies << endl;
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------