In this program everything work except the change back, the quarters, dimes, nickels, and pennies wont work, everytihng else works, for my input i used 65.67, which with hst came out to 75.52, the amount tendered was 100. I just need help with getting the quarters, dimes, nickels, and pennies to work.

#include<iostream>
#include<fstream>
#include<conio.h>
#include<iomanip>
#include<cmath>
// Associating program identifiers with external file names
#define in_file "data.txt"//input file name
#define out_file "result.txt"//output file name
using namespace std;

void purchase(float&);
float Change(float);
void reportchange(float);

ifstream ins;// associates ins as an input stream
ofstream outs;// associates outs as an output stream
float total=0.0;
float leftover;
float tendered;
float change;

int main()
{


    ins.open(in_file);// associates ins as an input stream
    outs.open(out_file);// associates outs as an output stream

    purchase(total);
    Change(total);
    reportchange(change);

    ins.close();// closing input file
    outs.close();// closing output file

    return 0;

}

void purchase(float& total)
{
    float amount=0.0;
    ins>>amount;
    while(amount>0)
    {
    total+=amount;
    ins>>amount;
    }
    if(amount==0)
        ins>>tendered;

    total=total+(total*0.15);
}

float Change(float total)
{
    outs<<setprecision(4)<<endl;
    outs<<"Your total is: " << total<<endl;
    outs<<"You payed: "<< tendered <<endl;
    change=tendered-total;
    outs<<"Your total change is: " <<change<<endl;

    return total;
}

void reportchange(float change)
{
    int leftover=change;
    int hundreds, fifties, twenties, tens, fives, toonies, loonies, quarters, dimes, nickels, pennies;
    quarters=0.0;
    dimes=0.0;
    nickels=0.0;
    int q=0.25;
    int d=0.10;
    int n=0.05;

    // Calculate the number of quarters, dimes, and nickels in change
    hundreds=leftover/100;
    leftover=leftover%100;
    fifties=leftover/50;
    leftover=leftover%50;
    twenties=leftover/20;
    leftover=leftover%20;
    tens=leftover/10;
    leftover=leftover%10;
    fives=leftover/5;
    leftover=leftover%5;
    toonies=leftover/2;
    leftover=leftover%2;
    loonies=leftover/1;
    leftover=leftover%1;
    if(q>0)
    {
    quarters=leftover/q;
    leftover=leftover%q;
    }
    if(d>0)
    {
    dimes=leftover/d;
    leftover=leftover%d;
    }
    if(n>0)
    {
    nickels=leftover/n;
    leftover=leftover%n;
    }
    pennies=leftover/(0.01);
    leftover=(100-leftover); //set change again otherwise it put garbage in output.

    outs<<setprecision(4)<<endl;
    outs<<"That is: "<<endl;
    if (hundreds < 1);
    else if (hundreds == 1)
    outs << "\t" << hundreds << " Hundred" << "\n";
    else
    outs << "\t" << hundreds << " Hundreds" << "\n";

    if (fifties < 1);
    else if (fifties == 1)
    outs << "\t" << fifties << " Fifty" << "\n";
    else
    outs << "\t" << fifties << " Fifties" << "\n";

    if (twenties < 1);
    else if (twenties == 1)
    outs << "\t" << twenties << " Twenty" << "\n";
    else
    outs << "\t" << twenties << " Twenties" << "\n";

    if (tens < 1);
    else if (tens == 1)
    outs << "\t" << tens << " Ten" << "\n";
    else
    outs << "\t" << tens << " Tens" << "\n";

    if (fives < 1);
    else if (fives == 1)
    outs << "\t" << fives << " Five" << "\n";
    else
    outs << "\t" << fives << " Fives" << "\n";

    if (toonies < 1);
    else if (toonies == 1)
    outs << "\t" << toonies << " Toonie" << "\n";
    else
    outs << "\t" << toonies << " Toonies" << "\n";

    if (loonies < 1);
    else if (loonies == 1)
    outs << "\t" << loonies << " Loonie" << "\n";
    else
    outs << "\t" << loonies << " Loonies" << "\n";

    if (quarters < 1);
    else if (quarters == 1)
    outs << "\t" << quarters << " Quarter" << "\n";
    else
    outs << "\t" << quarters << " Quarters" << "\n";

    if (dimes < 1);
    else if (dimes == 1)
    outs << "\t" << dimes << " Dime" << "\n";
    else
    outs << "\t" << dimes << " Dimes" << "\n";

    if (nickels < 1);
    else if (nickels == 1)
    outs <<"\t" << nickels << " Nickel" << "\n";
    else
    outs << "\t" << nickels << " Nickels"<< "\n";

    if (pennies <= 0);
    else if (pennies < 2)
    outs << "\t" << pennies << " Penny" << "\n";
    else    
    outs << "\t" << pennies << " Pennies" << "\n";


}

Thanks.

Edited 3 Years Ago by Nick Evan: Fixed formatting

You still have 11 minutes. Please edit and add your [code] ...code tags... [/code] so that we can actually read your code.

#include<iostream>
#include<fstream>
#include<conio.h>
#include<iomanip>
#include<cmath>
// Associating program identifiers with external file names
#define in_file "data.txt"//input file name
#define out_file "result.txt"//output file name
using namespace std;

void purchase(float&);
float Change(float);
void reportchange(float);

ifstream ins;// associates ins as an input stream
ofstream outs;// associates outs as an output stream
float total=0.0;
float leftover;
float tendered;
float change;

int main()
{


ins.open(in_file);// associates ins as an input stream
outs.open(out_file);// associates outs as an output stream

purchase(total);
Change(total);
reportchange(change);

ins.close();// closing input file
outs.close();// closing output file

return 0;

}

void purchase(float& total)
{
float amount=0.0;
ins>>amount;
while(amount>0)
{
total+=amount;
ins>>amount;
}
if(amount==0)
ins>>tendered;

total=total+(total*0.15);
}

float Change(float total)
{
outs<<setprecision(4)<<endl;
outs<<"Your total is: " << total<<endl;
outs<<"You payed: "<< tendered <<endl;
change=tendered-total;
outs<<"Your total change is: " <<change<<endl;

return total;
}

void reportchange(float change)
{
int leftover=change;
int hundreds, fifties, twenties, tens, fives, toonies, loonies, quarters, dimes, nickels, pennies;
quarters=0.0;
dimes=0.0;
nickels=0.0;
int q=0.25;
int d=0.10;
int n=0.05;

// Calculate the number of quarters, dimes, and nickels in change
hundreds=leftover/100;
leftover=leftover%100;
fifties=leftover/50;
leftover=leftover%50;
twenties=leftover/20;
leftover=leftover%20;
tens=leftover/10;
leftover=leftover%10;
fives=leftover/5;
leftover=leftover%5;
toonies=leftover/2;
leftover=leftover%2;
loonies=leftover/1;
leftover=leftover%1;
if(q>0)
{
quarters=leftover/q;
leftover=leftover%q;
}
if(d>0)
{
dimes=leftover/d;
leftover=leftover%d;
}
if(n>0)
{
nickels=leftover/n;
leftover=leftover%n;
}
pennies=leftover/(0.01);
leftover=(100-leftover); //set change again otherwise it put garbage in output.

outs<<setprecision(4)<<endl;
outs<<"That is: "<<endl;
if (hundreds < 1);
else if (hundreds == 1)
outs << "\t" << hundreds << " Hundred" << "\n";
else
outs << "\t" << hundreds << " Hundreds" << "\n";

if (fifties < 1);
else if (fifties == 1)
outs << "\t" << fifties << " Fifty" << "\n";
else
outs << "\t" << fifties << " Fifties" << "\n";

if (twenties < 1);
else if (twenties == 1)
outs << "\t" << twenties << " Twenty" << "\n";
else
outs << "\t" << twenties << " Twenties" << "\n";

if (tens < 1);
else if (tens == 1)
outs << "\t" << tens << " Ten" << "\n";
else
outs << "\t" << tens << " Tens" << "\n";

if (fives < 1);
else if (fives == 1)
outs << "\t" << fives << " Five" << "\n";
else
outs << "\t" << fives << " Fives" << "\n";

if (toonies < 1);
else if (toonies == 1)
outs << "\t" << toonies << " Toonie" << "\n";
else
outs << "\t" << toonies << " Toonies" << "\n";

if (loonies < 1);
else if (loonies == 1)
outs << "\t" << loonies << " Loonie" << "\n";
else
outs << "\t" << loonies << " Loonies" << "\n";

if (quarters < 1);
else if (quarters == 1)
outs << "\t" << quarters << " Quarter" << "\n";
else
outs << "\t" << quarters << " Quarters" << "\n";

if (dimes < 1);
else if (dimes == 1)
outs << "\t" << dimes << " Dime" << "\n";
else
outs << "\t" << dimes << " Dimes" << "\n";

if (nickels < 1);
else if (nickels == 1)
outs <<"\t" << nickels << " Nickel" << "\n";
else
outs << "\t" << nickels << " Nickels"<< "\n";

if (pennies <= 0);
else if (pennies < 2)
outs << "\t" << pennies << " Penny" << "\n";
else	
outs << "\t" << pennies << " Pennies" << "\n";


}

Your total is: 75.52
You payed: 100
Your total change is: 24.48

That is:
1 Twenty
2 Toonies


that is the output i get and its missing some of the change

You're truncating the "change" off your change:

void reportchange(float change)
{
int leftover=change;  //<---*ERROR*: assignment from float to int causes data truncation
int hundreds, fifties, twenties, tens, fives, toonies, loonies, quarters, dimes, nickels, pennies;

Unless you've suppressed compiler warnings, your compiler should be warning you about this.

This may not be your entire problem, but it's a big part of it.

Edited 5 Years Ago by Fbody: n/a

void reportchange(float change)
{
	change=change*100;
	int leftover=change;
	int hundreds, fifties, twenties, tens, fives, toonies, loonies, quarters, dimes, nickels, pennies;


	// Calculate the number of quarters, dimes, and nickels in change
	hundreds=leftover/10000;
	leftover=leftover%10000;
	fifties=leftover/5000;
	leftover=leftover%5000;
	twenties=leftover/2000;
	leftover=leftover%2000;
	tens=leftover/1000;
	leftover=leftover%1000;
	fives=leftover/500;
	leftover=leftover%500;
	toonies=leftover/200;
	leftover=leftover%200;
	loonies=leftover/100;
	leftover=leftover%100;
	quarters=leftover/25;
	leftover=leftover%25;
	dimes=leftover/10;
	leftover=leftover%10;
	nickels=leftover/5;
	leftover=leftover%5;
	pennies=leftover/1;
	leftover=leftover%1;

i made these changes and everytihng works except for pennies
this is my output


Your total is: 75.52
You payed: 100
Your total change is: 24.48

That is:
1 Twenty
2 Toonies
1 Quarter
2 Dimes
2 Pennies

Try it with a different value. The reduction looks okay to me, it may be caused by a rounding error inherent to the representation of a float value.

It may also be a good idea to insert some debugging lines to verify that you are actually filtering down the value you think you are.

For example, what happens if you modify Lines 3-5 like so?

cout << "Initial value of change: " << change << endl;
  change=change*100;
  cout << "Updated value of change: " << change << endl;
  int leftover=change;
  cout << "Starting value of leftover: " << leftover << endl;

Edited 5 Years Ago by Fbody: n/a

yes its not being rounded properly, i've used

outs<<fixed<<showpoint;
	outs<<setprecision(2)<<endl;
	outs<<"Your total is: " << total<<endl;
	outs<<"You payed: "<< tendered <<endl;
	change=tendered-total;
	outs<<"Your total change is: " <<change<<endl;

if the change is 12.436 , it will output 12.44, but the program will calculate the change for 12.436, im not sure how to make it round to a number which will STAY that number

nvm i tihnk i have figured it out, i used

change = (change * 100 + 0.5) / 100.0;

that stores the rounded off change permanently

thanks for the help! appreciate it

This question has already been answered. Start a new discussion instead.