Errors with sentinel-controlled loop/switch [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
A mail order house sells five different products whose retail prices
are: product 1 — $2.98, product 2—$4.50, product 3—$9.98, product 4—$4.49 and product 5—
$6.87. Write a program that reads a series of pairs of numbers as follows:
a) product number
b) quantity sold
Your program should use a switch statement to determine the retail price for each product. Your
program should calculate and display the total retail value of all products sold. Use a sentinel-controlled loop to determine when the program should stop looping and display the final results.
I get tons of error when I try to compile my code and I was wondering what I did wrong.
#include <iostream>
using namespace std;
int main()
{
int numberOfProducts = 0;
int costOfProducts = 0;
int productTotal = 0;
double amountP1 = 0;
double amountP2 = 0;
double amountP3 = 0;
double amountP4 = 0;
double amountP5 = 0;
double product1 = 2.98;
double product2 = 4.50;
double product3 = 9.98;
double product4 = 4.49;
double product5 = 6.87;
cout<<"How many products do you want to buy? -1 to finish shopping"<<endl;
cin>>numberOfProducts;
while(numberOfProducts != -1)
cout<<"Which products do you want to buy? -1 to finish shopping"<<endl;
cin>>costOfProducts;
switch(costOfProducts)
{
case product1:
cout<<"Product 1($2.98) has been purchased";
productTotal = productTotal + 2.98;
amountP1 = amountP1 + 1;
break;
case product2:
cout<<"Product 2($4.50) has been purchased";
productTotal = productTotal + 4.50;
amountP2 = amountP2 + 1;
break;
case product3:
cout<<"Product 3($9.98) has been purchased";
productTotal = productTotal + 9.98;
amountP3 = amountP3 + 1;
break;
case product4:
cout<<"Product 4($4.49) has been purchased";
productTotal = productTotal + 4.49;
amountP4 = amountP4 + 1;
break;
case product5:
cout<<"Product 5($6.87) has been purchased";
productTotal = productTotal + 6.87;
amountP5 = amountP5 + 1;
break;
default:
cout<<"Sorry, please select a product"; << endl;
}
cout<<"The total amount of products bought are: " << numberOfProducts;
cout<<"The total amount of product 1's bought is: $" << amountP1 << endl;
cout<<"The total amount of product 2's bought is: $" << amountP2 << endl;
cout<<"The total amount of product 3's bought is: $" << amountP3 << endl;
cout<<"The total amount of product 4's bought is: $" << amountP4 << endl;
cout<<"The total amount of product 5's bought is: $" << amountP5 << endl;
cout<<"The total price of all your products are: $" << productTotal << endl;
return 0;
}

Remove the semicolons at the end of the following lines:
while(numberOfProducts != -1);
switch(costOfProducts);
In addition, the compiler doesn't understand the following symbols (and neither do I):
product1
product2
product3
product4
product5
BTW, I strongly recommend that you add a couple of cin>> there, if you actually want this to work...

You need to learn about statement blocks.
while(numberOfProducts != -1)
cout<<"Which products do you want to buy? -1 to finish shopping"<<endl;
cin>>costOfProducts;
I'll play a game with you. You need to insert { to begin a statement block and } to end one.
Your role is to figure out where they go.
Hint 1: The expression inside a while statement applies to the statement block.
Hint 2: One or more statements can be placed in a statement block.
If you wish to cheat, you can look up the syntax of the while statement, for multiple statements in the loop.

Related

C++ output printed twice and error exit code: -1 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
I am learning and practicing with C++. I an error when I tried to run the program. I tried different things like changing the sign (<=,>=,<,>) but I don't think they are the problem. I was planning to create different classes for each range of bonus salary but I don't think it is needed to add different classes. I tried to combine 'bonus' and 'total' in one line of code but I need 'bonus' information to be displayed. The instructions are if your old salary is up to $14,999.99 raise 5%, $15,000.00 to $49,999.99 raise 7%, $50,000.00 to $99,999.99 raise 10% and $100,000.00 and higher raise 15%
collect2.exe: error: ld returned 1 exit status
The terminal process terminated with exit code: -1.
This is the code
// Salary Calculator Program
// Intro C++, Lesson 6
// Written by Phong Dau, Jun 2022
#include <iostream>
using namespace std;
int main( )
{
//Declare variables
double oldSalary = 0.00;
double bonus = 0.00;
double total = 0.00;
//Prompt the user for inputs
cout << "Enter your old salary: ";
cin >> oldSalary;
//Decide salary bonus
if (oldSalary < 15000.00)
{
bonus = oldSalary * 0.05;
total = bonus + oldSalary;
cout <<"You will receive a raise of $" <<bonus<<", for a new yearly salary of $"<<total<<endl;
}
else if (oldSalary >= 15000.00 < 50000.00)
{
bonus = oldSalary * 0.7;
total = bonus + oldSalary;
cout <<"You will receive a raise of $" <<bonus<<", for a new yearly salary of $"<<total<<endl;
}
else if (oldSalary >= 50000.00 < 100000.00)
{
bonus = oldSalary * 0.1;
total = bonus + oldSalary;
cout <<"You will receive a raise of $" <<bonus<<", for a new yearly salary of $"<<total<<endl;
}
else
{
bonus = oldSalary * 0.15;
total = bonus + oldSalary;
cout <<"You will receive a raise of $" <<bonus<<", for a new yearly salary of $"<<total<<endl;
}
//end if
system("pause");
return 0;
} //end of main
When I enter few numbers to test the program, the output printed twice except for over $100,000.00. I only want it to be printed once.
Enter your old salary:10000
You will receive a raise of $500,for a new yearly salary of $10500
You will receive a raise of $500,for a new yearly salary of $10500
Press any key to continue . . .
Enter your old salary:20000
You will receive a raise of $1400,for a new yearly salary of $21400
You will receive a raise of $1400,for a new yearly salary of $21400
Press any key to continue . . .
Enter your old salary:80000
You will receive a raise of $8000,for a new yearly salary of $88000
You will receive a raise of $8000,for a new yearly salary of $88000
Press any key to continue . . .
Enter your old salary:150000
You will receive a raise of $22500,for a new yearly salary of $172500
Press any key to continue . . .
As identified already in the comments, you have a few issues. I'm going to spell them out for you as an answer.
The first issue is that your final else is only controlling a single line because it does not enclose the multiple statements in brackets.
else
bonus = oldSalary * 0.05;
total = bonus + oldSalary;
cout <<"You will receive a raise of $" <<bonus<<", for a new yearly salary of $"<<total<<endl;
The above is equivalent to:
else {
bonus = oldSalary * 0.05;
}
total = bonus + oldSalary;
cout <<"You will receive a raise of $" <<bonus<<", for a new yearly salary of $"<<total<<endl;
To fix that, put those statements in a block:
else {
bonus = oldSalary * 0.05;
total = bonus + oldSalary;
cout <<"You will receive a raise of $" <<bonus<<", for a new yearly salary of $"<<total<<endl;
}
The second issue is multiple issus actually. To start, this kind of thing is totally bogus:
if (oldSalary >= 15000.00 < 50000.00)
This does not do what you think. You probably wanted:
if (oldSalary >= 15000.00 && oldSalary < 50000.00)
But that's still incorrect because it's never reached due to your logic being backwards:
if (oldSalary > 14999.99) {
// All salaries above 14999.99
}
else if (oldSalary > 49999.99) {
// Not reachable
}
else if (oldSalary > 99999.99) {
// Not reachable
}
else
{
// All salaries less than or equal to 14999.99
}
One approach is to reverse the tests such that the things being tested by the "else" are logical possibilities.
if (oldSalary > 99999.99) {
// (99999.99, +inf) -> 15%
}
else if (oldSalary > 49999.99) {
// (49999.99, 99999.99] -> 10%
}
else if (oldSalary > 14999.99) {
// (14999.99, 49999.99] -> 7%
}
else
{
// (-inf, 14999.99] -> 5%
}
Also, in this instance you have a very minor boundary issue. You say that it's 5% for anything below 15000.00. However, 14999.99999 is below that but you wrongly assume 14999.99 is the highest possible value below 15000.
So instead of reversing the order of the statements, let's reverse the comparisons. While we're at it, check out in every possible scenario you are performing the same salary calculation and the same output. So those do not have to be repeated. The only thing different is the bonus calculation.
Let's put all that together and roll the bonus multiplier in there too... Look how simple (and readable) it becomes:
if (oldSalary < 15000.0)
bonus = 0.05;
else if (oldSalary < 50000.0)
bonus = 0.07;
else if (oldSalary < 100000.0)
bonus = 0.10;
else
bonus = 0.15;
bonus *= oldSalary;
total = oldSalary + bonus;
cout << "You will receive a raise of $" << bonus
<< ", for a new yearly salary of $" << total
<< endl;

I need to figure out simple loops for a school project [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
We are supposed to make a retail sales cashier but I just can't figure out the loops at all. We have only learned simple selection and repetition statements so far and I know that's all I need but I just can't seem to figure it out.
Project Overview
Starter Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double cashDrawer = 500.00;
int productID = 0;
int quantity = 0;
double price = 0.0;
double subtotal = 0.0;
double salesTax = 0.0;
double totalSale = 0.0;
int anotherSale = 1;
// Loop for repeat sales
// Enter the first Product ID for the first sale (-1 to exit)
// Main loop for each sale
// Switch statement to determine the price, and calculate sales tax, if any, for the item.
// Get next Product ID
// Print properly formatted output for each sale
// Another sale?
// Display how much is in the cash drawer at the end
}
What I have so far:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double cashDrawer = 500.00;
int productID = 0;
int quantity = 0;
double price = 0.0;
double subTotal = 0.0; // for receipt purposes
double salesTax = 0.0; // for receipt purposes
double totalSale = 0.0; // for receipt purposes
int anotherSale = 1;
double taxRate = 0.075; // default tax rate
// Loop for repeat sales
while ()
{
// Enter the first Product ID for the first sale (-1 to exit)
cout << "Enter the first Product ID: ";
cin >> productID;
// Main loop for each sale
while (productID > 0)
{
// Switch statement to determine the price, and calculate sales tax, if any, for the item.
switch (productID)
{
case 101:
price = 65.00;
taxRate = 0.075;
break;
case 102:
price = 12.50;
taxRate = 0;
break;
case 103:
price = 24.50;
taxRate = 0.00;
break;
case 104:
price = 38.75;
taxRate = 0.075;
break;
case 105:
price = 17.80;
taxRate = 0.075;
break;
case 106:
price = 16.50;
taxRate = 0;
break;
case 107:
price = 42.85;
taxRate = 0.075;
break;
case 108:
price = 32.99;
taxRate = 0.075;
break;
case 109:
price = 28.75;
taxRate = 0.075;
break;
case 110:
price = 51.55;
taxRate = 0;
break;
default:
cout << "INVALID PRODUCT ID: Product ID not found." << endl;
}
cout << "Enter the quantity: ";
cin >> quantity;
subTotal += price * quantity;
salesTax += price * quantity * taxRate;
totalSale = subTotal + salesTax;
// Get next Product ID
cout << "Enter the next Product ID: ";
cin >> productID;
}
// Print properly formatted output for each sale
// Another sale?
}
// Display how much is in the cash drawer at the end
}
Any help is appreciated, thank you guys in advance.
The basic aspect of a loop statement is to repeat the same set of instructions until a specified condition is met. You have successfully figured it out for the inner loop of your code where you are checking if productID>0. You need to do the same for the outer loop and impose a similar condition on anotherSale i.e. while(anotherSale!=0). Every time the inner loop finish, just ask the user for the value of anotherSale; if the user enters 0, the loop should break.

uninitialized?, tuitionCost, local Variable, [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
int main()
{
double tuitionCalc(int sumCreditHoursTaken);
int numCourses;
double total = 0.0;
double tuitionCost= 0.0;
cout << "\t\t This Program calculates a student's total number of\n";
cout << "\t\tcredit hours and tution for a given semester.\n";
cout << "\nPlease enter the number of Courses you will be taking this semester: ";
cin >> numCourses;
for ( int count = 1; count <= numCourses; count++)
{
double sumCreditHoursTaken;
cout << " please enter the number of credit hours for course" << count << ": ";
cin >> sumCreditHoursTaken;
total += sumCreditHoursTaken;
}
cout << fixed << showpoint << setprecision(2);
cout << "Your Total number of credit hours is: " << total << endl;
cout << "Your total tuition will be: $" << tuitionCalc(tuitionCost) << "\n\n";
return 0;
}
and the function im calling is
double tuitionCalc(int sumCreditHoursTaken)
{
double tuitionCost = 0.0;
double costCreditHour = 147.00;
double maxHoursFullTuition = 12;
double maintFeeAddOn = 29.33;`
if (sumCreditHoursTaken <= maxHoursFullTuition)
cout<< " " << (sumCreditHoursTaken * costCreditHour);
else if (sumCreditHoursTaken > maxHoursFullTuition)
cout << " " << (maxHoursFullTuition * costCreditHour) + ((sumCreditHoursTaken - maxHoursFullTuition) * maintFeeAddOn);
return tuitionCost;
}
input for number of courses is 5
and credit hours is 3,3,3.5,4,2.5
i get the total credit hours but icant seem to display the tuition cost?
thank you s
You are never actually assigning a value to tuitionCost in tuitionCalc() method, so it will always be 0.0.
To elaborate: You are returning tuitionCost from tuitionCalc(). You first initialize tuitionCost = 0.0, but never proceed to assign any calculated value to it. Thus, when you return tuitionCost, it will return the value you initialized it to: 0.0.
I haven't examined your code in detail, but if you inputs contain floating point numbers then change the type for sumCreditHoursTaken from int to double.
Also, change the parameter for the invocation of tuitionCalc from tuitionCost to total.
It seems that OP has fallen afoul of misunderstanding scope and how variables are passed to functions.
In main, OP defines tuitionCost. tuitionCalc defines another tuitionCost. These are different tuitionCosts. They represent different locations in memory and can store different values.
Next, because the tuitionCalc function is defined
double tuitionCalc(int sumCreditHoursTaken)
tuitionCalc(tuitionCost) will take tuitionCost convert it to an integer, and pass a copy into tuitionCalc where it will be used with the name sumCreditHoursTaken. One could say OP has three tuitionCalcs at this point. Not what they want.
Breaking down the tuitionCalc prototype, we see that it takes sumCreditHoursTaken, an integer and based on the name the number of credit hours taken, not a total cost. tuitionCalc also returns a double and inferring the purpose of the function from it;'s name, one would expect that it calculates and returns the tuition.
Like Anatoly states in his answer, the input to tuitionCalc should almost certainly be total, the total number of credit hours computed, and the output should be tuitionCost.
Since this has the smell of a homework assignment, it's not in the OP's best interests to fully answer the question. Instead here are a few recommendations:
Eliminate tuitionCost from main. It only serves to increase confusion. You can reuse variable names, but only do it where there is a clear benefit. If you have a cost and a function that takes and uses cost, then using cost for both makes sense. Just remember that cost inside the function is a different entity unless you pass by reference. In
void function(int & cost)
called with
function(cost);
both costs are the same. But in
void function(int cost)
called with
function(cost);
both function's cost is a copy of the caller's cost and any changes made by function will only effect the copy.
Declare variables close to where you use them. This way people reading your code don't have to scroll up and down and otherwise go hunting. It also helps you because it makes mistakes like, "Why am a calling a function that takes an int with a double?" more obvious.
Do not cout in tuitionCalc. Compute and return tuitionCost. Allow main to output tuitionCost. A function with a name like calc should only calculate. Names should describe function as closely as possible.
First of all you should create the function prototype before declaring it.
And there were some confusions in the code you have used I tried my best to omit error hope this is helpful!
#include <iostream>
#include <iomanip>
using namespace std;
double tuitionCal(double sumCreditHoursTaken);
int main() {
double tuitionCalc(int sumCreditHoursTaken);
int numCourses;
double total = 0.0;
//double tuitionCost= 0.0;
cout << "\t\t This Program calculates a student's total number of\n";
cout << "\t\tcredit hours and tution for a given semester.\n";
cout << "\nPlease enter the number of Courses you will be taking this semester: ";
cin >> numCourses;
double sumCreditHoursTaken; // you should create this variable outside the for loop
for ( int count = 1; count <= numCourses; count++)
{
cout << " please enter the number of credit hours for course" << count << ": ";
cin >> sumCreditHoursTaken;
total += sumCreditHoursTaken;
}
double tuitionCost=tuitionCal(total);
cout << fixed << showpoint << setprecision(2);
cout << "Your Total number of credit hours is: " << total << endl;
cout << "Your total tuition will be: $" <<tuitionCost<< "\n\n";// I assume this is what you want
return 0;
}
double tuitionCal(double sumCreditHoursTaken)//the parameter type is double now
{
double tuitionCost = 0.0;
double costCreditHour = 147.00;
double maxHoursFullTuition = 12;
double maintFeeAddOn = 29.33;
if (sumCreditHoursTaken <= maxHoursFullTuition)
tuitionCost=(sumCreditHoursTaken * costCreditHour);
else if (sumCreditHoursTaken > maxHoursFullTuition)
tuitionCost=(maxHoursFullTuition * costCreditHour) + ((sumCreditHoursTaken - maxHoursFullTuition) * maintFeeAddOn);
return tuitionCost;
//I don't see a point of returning the value and couting both you can
//do only one of the oprations
}

Issue with calling function that takes in three parameters, one being int const [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm posting because I'm having issues figuring out why my "total aid available" is not printing the total of the pell grant, stafford loan, and work-study loan. I've tried fixing my function again and again (I used sources online and in reference book, but I don't know if the issue is that my function won't be called or not, since nothing is printing for the total aid available.
Everything else is fine, except that one thing, and it is really bugging me since no matter what changes I make, I'm in the same state. No errors showing either. I'm using microsoft visual studio for the first time as compiler, so I wonder if that is the issue.
Here is what I have:
#include <iostream>
using namespace std;
double pell_Grant(double); // forward declaration on pell_Grant ( which is a function for calculating pell grant)
double sum(double, int const, double); // declaration for sum function which gives total for the total aid available
int main()
{
double workstudy = 0, pellgrant = 5730, grossincome = 0, Total = 0; // variables
int yes;
int const stafford = 9500; //const declaration
cout << "Lemme Forecast Your FAFSA" << endl;
cout << "Enter your adjusted gross income: " << endl; cin >> grossincome; // input for gross income
if (grossincome >= 30000) // if gross income is higher than 30,000 then print message
{
cout << "Sorry Charlie, your income is too high to run this forecaster!";
return 0;
}
cout << "Can someone claim you as a dependent? [1=yes/0=no]: " << endl; // input to claim dependent or not
cin >> yes;
if (yes == 1) // if 1 for yes is selected then pell grant gets reduced by 750, if 0 or no selected, then program goes by standard procedure
{
pellgrant -= 750;
}
workstudy = 1465; // work study must be nationwide avergae 1,465
if (grossincome >= 19000) // if this condition is met then work study is not met and message is printed as follows...
{
cout << "Your Work-Study Award is not available for your income level" << endl;
workstudy = 0;
}
double foo = pell_Grant(grossincome); // value returned from pell_Grant stored here to give total
Total = sum(workstudy + stafford + pellgrant); // sum function is called and stores result in Total
if (workstudy != 0) // if work study loan isn't more than 19,000 then it will be calculated and printed in next statement
{
cout << "Your Work-Study Award (if available)= " << workstudy << endl;
}
cout << "Your Stafford Loan award (if needed)= " << stafford << endl; // prints stafford loan (const called)
cout << "Your Pell Grant= " << pellgrant << endl; // prints pell grant
cout << "Total Aid Available For You=" << Total << endl; // prints total
return (0);
}
double pell_Grant(double x) // pell_Grant function that calculates pell grant which is assigned 5,730
{
// x is gross income which is assigned 5,730. This is money received that does not need to be repaid.
if ((x > 12000) && (x < 20000)) // statement makes sure adjusted gross is bettween 12000 & 20000
{
double a = x / 1000; // for every 1,000 in adjusted gross income... reduce/subtract 400 from it
a *= 400;
x -= a;
}
if (x > 20000) // check if gross income is more than 20000
{
double a = x / 1000; // for every 1,000 in gross income, subtract 500
a *= 500;
x -= a;
}
return x;
}
double sum(double workstudy , int const stafford, double pellgrant) // function for adding up workstudy loan, stafford loan, and pellgrant loan
{
double Total;
Total = workstudy + stafford + pellgrant;
return (Total); // returns total
}
According to its declaration, the method sum() accepts 3 parameters.
double sum(double, int const, double);
But while calling you are passing only 1 parameter:
Total = sum(workstudy + stafford + pellgrant);
Instead, you need to pass 3 parameters, like this:
Total = sum(workstudy, stafford, pellgrant);
But, I don't understand why you aren't getting any errors! You are trying to call a non-existent function. You must get a compiler error.
You are calling your sum() function incorrectly. This is your code:
Total = sum(workstudy + stafford + pellgrant); // sum function is called and stores result in Total
But your sum() function has three parameters. The correct form to call the function would be:
Total = sum(workstudy, stafford, pellgrant); // sum function is called and stores result in Total

How can I keep track of the coin number available coins in a vending machine? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a school project in c++ for a simple vending machine that accepts one-dollar-bills and give change in quarters, dimes, nickels. All the items cost less than 1 dollar .
But I need to add an extra ability to keep track of the available change in the machine, and I dont how to do it.
And this is the piece of code I wrote:
#include <iostream>
#include <string>
using namespace std;
int main()
{
//set the variables
int Qav=5 ;
int Dav=5 ;
int Nav=5 ;
int purchaseAmount ;
cout << "Simple Vending Program for Adam Ashouri (Regular Version)" <<endl;
cout <<endl;
cout <<endl;
cout << "Enter a purchase amount [5 - 100] -->";
cin >> purchaseAmount;
int chaneBack = 100 - purchaseAmount ;
changecalculator ();
}
void changecalculator ()
{
int purchaseAmount ;
int QBack ,DBack ,NBack ;
int chaneBack = 100 - purchaseAmount ;
if (purchaseAmount %5 == 0)
{
cout << "You entered a purchase amount of " << purchaseAmount << " cents." <<endl;
cout <<endl;
QBack = chaneBack / 25 ;
DBack = (chaneBack % 25) / 10;
NBack = (chaneBack %25 %10) / 5;
cout <<"Your change of " <<chaneBack <<" cents is given as " <<QBack <<" Q, " <<DBack <<" D,and " <<NBack <<" N." <<endl;
int coinsNum = QBack + DBack + NBack;
cout << "The value of your " <<coinsNum <<" coins adds up to " <<chaneBack <<" cents." <<endl;
cout << "Thank you for using my program.";
}
else
{
cout << "Unable to process an invalid purchase amout of " <<purchaseAmount <<" cents." <<endl;
cout << "Thank you for using my program." <<endl;
}
}
First of all, your current program doesn't work, because you don't transfer purchaseAmount inside function changecalculator(). You need to do it this way:
1) Change signature of the function to this: void changecalculator(int iPurchaseAmount)
2) Send purchaseAmount inside the function (i.e. call it this way: changecalculator(purchaseAmount);)
3) Use value iPurchaseAmount inside the body of your function and remove line int purchaseAmount; from it.
And about your main question (saving of number of available coins):
Ok, you've added Qav, Dav and Nav (available coins) in the body of your program. It is right. Now you have to transfer these values inside your changecalculator() function. Try to use this signature of your function: void changecalculator(int iPurchaseAmount, int& ioQav, int& ioDav, int& ioNav) (these values will be available inside body of the function). And call this function this way: changecalculator(purchaseAmount, Qav, Dav, Nav);.
After it you need only to analyze these values and change it inside the function. Let's add changeing of the values (it is necessary in case of many calls of this function). Just add following lines after calculation of QBack, DBack and NBack:
ioQav -= QBack;
ioDav -= DBack;
ioNav -= NBack;
And the most complex part of this task is analysis of values ioQav, ioDav, ioNav before calculation of QBack, DBack and NBack. Let's do it for quarters:
QBack = std::min(chaneBack / 25, ioQav);
It is calculation of the smallest value of two: available number of quarters and optimal number of quarters. So, this line calculates exactly what do you need. After it you can calculate number of dimes:
chaneBack -= QBack * 25; // subtract quarters
DBack = std::min(chaneBack / 10, ioDav);
If you understand this logic, you can complete it for NBack.