Need help

It's a money changing program. For the first part, it gives me the best possible way for the change. It works, but not for $1,2,5,10,20,50,100?? It gives me the same amount of number back, where I want 2-$50 for the 100 or, 2-$20 and 1-$10 for $50 change.. how can I fix it?? the 2nd part of the program works where I have to find the number of possible ways for the change!!
the program is like this:

#include <iostream>

using namespace std;
int penNy(int x)
{
return 1;
}

int niCkel(int x)
{
if (x >= 0) return niCkel(x-5) + penNy(x);
return 0;
}

int diMe(int x)
{
if (x >= 0) return diMe(x-10) + niCkel(x);
return 0;
}

int quaRter(int x)
{
if (x >= 0) return quaRter(x-25) + diMe(x);
return 0;
}

int cent_50(int x)
{
if (x >= 0) return cent_50(x-50) + quaRter(x);
return 0;
}

int doLlar(int x)
{
if (x >= 0) return doLlar(x-100) + cent_50(x);
return 0;
}

int dollar_5(int x)
{
if (x >= 0) return dollar_5(x-500) + doLlar(x);
return 0;
}

int dollar_10(int x)
{
if (x >= 0) return dollar_10(x-1000) + dollar_5(x);
return 0;
}



int main ( )
{
float userNUmber;
int change,dollar100,dollar50,dollar20,dollar10,dollar5,dollar2,dollar1,
cent50,quarters, dimes, nickels, pennies; // declare variables
cout <<"Enter the amount of money: ";
cin >> userNUmber; // input the amount of change
change = userNUmber * 100;
dollar100=change/10000;
change=change%10000;
dollar50=change/5000;
change=change%5000;
dollar20=change/2000;
change=change%2000;
dollar10=change/1000;
change=change%1000;
dollar5=change/500;
change=change%500;
dollar2=change/200;
change=change%200;
dollar1=change/100;
change=change%100;
cent50=change/50;
change=change%50;
quarters = change / 25; // calculate the number of quarters
change = change % 25; // calculate remaining change needed
dimes = change / 10; // calculate the number of dimes
change = change % 10; // calculate remaining change needed
nickels = change / 5; // calculate the number of nickels
pennies = change % 5; // calculate pennies

cout<<"\n$100 : "<<dollar100<<endl;
cout<<"$50 : "<<dollar50<<endl;
cout<<"$20 : "<<dollar20<<endl;
cout<<"$10 : "<<dollar10<<endl;
cout<<"$5 : "<<dollar5<<endl;
cout<<"$2 : "<<dollar2<<endl;
cout<<"$1 : "<<dollar1<<endl;
cout<<"50 cents: "<<cent50<<endl;
cout << "Quarters: " << quarters << endl; // display # of quarters
cout << " Dimes: " << dimes << endl; // display # of dimes
cout << " Nickels: " << nickels << endl; // display # of nickels
cout <<" Pennies: " << pennies << endl; // display # of pennies

double userInput;
userInput=userNUmber;
userInput*=100;
cout<<"\n"<<dollar_10(userInput)<<endl;
return 0;


return (0);
}
Last edited on
Okay, let's break it down into a couple of pieces.
In your main function, you've got the variable y being initialized by the user.

The value stored in that variable is then multiplied by 100.
Next, you make a function call to same_thing().

This is where your problem begins -- inside the function same_thing(), you're creating an entirely NEW variable called "y" that isn't being initialized... this means that the variable "y" (only visible in the scope of same_thing()) will hold a completely random value, one that was previously held in that memory location.

See if you can figure out how to fix that first...
Topic archived. No new replies allowed.