Hey everyone :) I'm new to c++ and have just joined this site. Looks great. Hope you guys can help.
I'm having trouble with the function calcAllowedPerChild()
What I want the function to do: subtract the gifts TOOTHBRUSH, HIGHLIGHTERS, CRAYONS, NOTEBOOK , AND PEN, from the amount left(leftToSpend) while the leftToSpend doesn't equal 0. My program runs but not with the correct output.
Here's the full program:
#include <iostream>
using namespace std;
const float maxPerUnit = 20000.00;
//minPerChild includes a standard gift consisting of a bath towel and a facecloth
const float minPerChild = 100.00;
const float maxPerChild = 180.00;
//depending on the amount, the child may also get one or more of the following;
const float TOOTHBRUSH = 29.95;
const float HIGHLIGHTERS = 25.95;
const float CRAYONS = 17.95;
const float NOTEBOOK = 12.95;
const float PEN = 9.99;
float calcAllowedPerChild(int nrChildrenP)
{
float amtGiftP = 20000.00 / nrChildrenP;
return amtGiftP;
}
float calcActualAmount(float amountP, int nrChildrenP)
{
float leftToSpend;
if (calcAllowedPerChild(nrChildrenP) > 180)
amountP = 180;
else if (calcAllowedPerChild(nrChildrenP) < 180)
leftToSpend = calcAllowedPerChild(nrChildrenP) - 100;
do
{
for (int i = 1; i <= 5; i++)
{
switch (i)
{
case 1: leftToSpend -= TOOTHBRUSH;
break;
case 2: leftToSpend -= HIGHLIGHTERS;
break;
case 3: leftToSpend -= CRAYONS;
break;
case 4: leftToSpend -= NOTEBOOK;
break;
case 5: leftToSpend -= PEN;
break;
amountP = calcAllowedPerChild(nrChildrenP) - leftToSpend;
}
}
return amountP;
}
while (leftToSpend == 0);
}
int main()
{
float amtGift; // Allowed amount per child
float amtSpentPerChild = 0.00; // actual amount spent per child
float totalSpent = 0.00; // total spent for 1 orphanage
float totalAll = 0.00; // total spent for all 4 orphanages
int nrChildren;
cout << "Enter the amount of children: " << endl;
cin >> nrChildren;
amtGift = calcAllowedPerChild(nrChildren);
cout.setf(ios::fixed);
cout.precision(2);
cout << endl << " Allowable amount per child : R" << amtGift;
cout << endl << endl;
amtSpentPerChild = calcActualAmount(amtGift, nrChildren);
cout << endl << " Actual amount spent per child : R" ;
cout << amtSpentPerChild << endl << endl;
system("pause");
return 0;
}
okay so the output should be (if the user inputs 140 children):
Allowable amt per child: R 142.85
Actual amt per child: R 139.09
The working:
142.85 - 100(standard gift) = 42.85(leftToSpend);
The next most expensive gift is a TOOTHBRUSH so: 42.85 - 29,95 = 12.9(leftToSpend);
The only gift that can be added now with the amount left is a PEN
so: 12.9(lefToSpend) - 9.99 = 2.91 (leftToSpend);
And to get the total amount per child: calcAllowedPerChild(nrChildren) - leftToSpend
= 139.09
I don't know how to get this. perhaps I shouldn't use a switch statement at all/ a do-while loop ?