-3
using namespace std;

int amount_total(int amount_paid, int& amountleft), quarters(int&
    amountleft), dimes(int& amountleft), pennies(int& amountleft);

int amount_total(int amount_paid, int& amountleft)
{
    const int TOTALVALUE = 99;

    cout << "You have bought a candy bar that's 99 cents. \n"
        << "Please insert the amount paid. " << endl;
    cin >> amount_paid;
    amountleft = TOTALVALUE - amount_paid;

    cout << "You have to pay : " << amountleft << " cents. \n" << endl;

    return(0);
}

int quarters(int& amountleft)
{
    const int QUARTERS = 25;
    int total, count_Quarters;
    count_Quarters = 0;

    while (amountleft > 0 || amountleft >= QUARTERS)
    {
        total = amountleft - QUARTERS;

        count_Quarters++;
    }

    return(count_Quarters);
}

int dimes(int& amountleft)
{
    const int DIMES = 10, QUARTERS = 25;
    int total, count_Dimes;
    count_Dimes = 0;

    while (amountleft > 0 || amountleft <= QUARTERS)
    {
        total = amountleft - DIMES;

        count_Dimes++;
    }

    return(count_Dimes);
}

int pennies(int& amountleft)
{
    const int PENNIES = 1, DIMES = 10;
    int total, count_Pennies;
    count_Pennies = 0;

    while (amountleft >= 0 || amountleft <= DIMES)
    {
        total = amountleft - PENNIES;

        count_Pennies++;
    }

    return(count_Pennies);
}

int main()
{
    int amount_paid, amountleft, Count_Quarters,
        Count_Dimes, Count_Pennies, total_amount;

    total_amount = amount_total(amount_paid, amountleft);
    Count_Quarters = quarters(amountleft);
    Count_Dimes = dimes(amountleft);
    Count_Pennies = pennies(amountleft);

    cout << "You'll get : " << Count_Quarters << " quarters, " <<
        Count_Dimes << " dimes, and " << Count_Pennies << " pennies. \n"
        << endl;
    return 0;
}

//sample run:

You have bought a candy bar that's 99 cents. Please insert the amount paid. 36 You have to pay : 63 cents.

What my original plan was for the program to run and the main would just run through the functions and the functions would return the variable, however it's not doing that and it's only running the first function

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

-1

as rex mentioned in the comment all your while-loops are infinit and so will never stop looping. Your program actualling freezing inside your second function “quarters”. See how things happning there: when you left your function amount_total it returns 0 and assigns it to a “total_amount” variable, before it “amountleft” variable has been assigned the value equal to “TOTALVALUE – amount_paid”. You pass this value ( inside “amountleft” variable ) to the second function “quarters”. In case of your example “amountlef” variable is equal to 63 which is bigger than “0” and also bigger than QUARTERS ( as it is 25 ). So you are starting the loop here and each iteration of the loop your variable “amountleft” keeps having the same value i.e. 63 as it doesn’t change inside your while-loop, to leave the loop you have to change “amountleft” or to set any kind of break. To make it more visible it is possible to set “cout” inside your loops and see what is happeing there. Like this e.g.

 21 int quarters( int& amountleft ) {                                                                                                  
 22         const int QUARTERS = 25;                                                                                                   
 23         int total, count_Quarters;                                                                                                 
 24         count_Quarters = 0;                                                                                                        
 25         cout << "before while-loop" << endl;                                                                                       
 26         while( amountleft > 0 || amountleft >= QUARTERS ) {
 27           cout << "count_Quarters = " << count_Quarters  << endl;
 28           cout << "amountleft  = " << amountleft  << endl;                                                                         
 29                 total = amountleft - QUARTERS;                                                                                     
 30                                                                                                                                    
 31                 count_Quarters++;                                                                                                  
 32         }                                                                                                                                                                                                                                                   
 33         cout << "after while-loop" << endl;                                                                                        
 34         return count_Quarters;                                                                                                     
 35 }

Keep in mind that as you haven’t defined “total“ variable it can contain any garbage but usually don’t. Anyway it can be just good to assign any value ( such as 0 e.g. ) to a variable before using it.

Neyroman
  • 51
  • 6