Please help with return function

Hi Everyone

I've been struggeling for almost a whole day with one of my assignment questions. Can someone please assist me.
Basicly I had to only write the return function for the program below. It does work with some values but not wita all

Basicly the program must return differnt values depending on the number of children in an orphanage. It is a program that a store use to provide money for the orphanage. The store can not spend more than R180 a child and the minimum it will spend on a child is R100. If for example the amount per child is say R145 then the R45 needs to buy one of the following starting at the most expensive. The remainder R100 stays the same. The items are

1. toothbrush R29.95
2.Highlighters R25.95
3. Crayons R17.95
4. Notebook R12.95
5. Pen R9.99

so if the amount is R145 it will display amount allowable as R145 and actual ammount will be R129.95 per child. If it was say R111 per child the actual amount will be R109.99 because the most expensive item that could be purchased would be the pen. If the amount was say R300 a child only R180 can be spent not anything more. I will copy and paste the program. The function name and parameters are correct cause that was told to use in the Question



#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)
{
if ((maxPerUnit / nrChildrenP) > maxPerChild)
return maxPerChild;
else
if ((maxPerUnit / nrChildrenP) >= minPerChild)
return (maxPerUnit / nrChildrenP);
else
cout << "Budget not big enough" << endl;
return (maxPerUnit / nrChildrenP);
}

float calcActualAmount(float amtGiftP, int nrChildrenP)
{
if (maxPerUnit / nrChildrenP - minPerChild >= PEN)
if (maxPerUnit / nrChildrenP - minPerChild < NOTEBOOK)
return minPerChild + PEN;
else
return minPerChild + NOTEBOOK;

if (maxPerUnit / nrChildrenP - minPerChild >= NOTEBOOK)
if (maxPerUnit / nrChildrenP - minPerChild < CRAYONS)
return minPerChild + NOTEBOOK;
else
return minPerChild + CRAYONS;

if (maxPerUnit / nrChildrenP - minPerChild >= CRAYONS)
if (maxPerUnit / nrChildrenP - minPerChild < HIGHLIGHTERS)
return minPerChild + CRAYONS;
else
return minPerChild + HIGHLIGHTERS;

if (maxPerUnit / nrChildrenP - minPerChild >= HIGHLIGHTERS)
if (maxPerUnit / nrChildrenP - minPerChild < TOOTHBRUSH)
return minPerChild + HIGHLIGHTERS;
else
return minPerChild + TOOTHBRUSH;
}

int main()
{
float amtGift; //Allowed amount per child
float amtSpentPerChild = 0.00; //actual amount spent per child
float totalSpent = 0.00; //total spent for one orphanage
float totalAll = 0.00; //total spent for all 4 orphanages
int nrChildren; //number of children per orphanage

cout << " Enter the number 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;


return 0;
}


The function that the problem start is float calcActualAmount. The function name and parameters are correct. The function calcAllowedPerChild I also had to write and it seems that it is correct. Its only with the second function I am struggeling. Please please help me
Last edited on
closed account (zb0S216C)
Could you please use code-tags as it adds readability to your code. Also, you can't have a method definition within a method definition( nested ).
Please are there no one that can help me with this.
The only problem is for the code to determine "calcActualAmount". The parameters are correct.
Rest of the code is also correct. Please Please Please
closed account (z05DSL3A)
Please are there no one that can help me with this.

Did you bother to read Frameworks reply?

You are missing a '}' at the end of the calcAllowedPerChild() definition.
Last edited on
Sorry for the sytax error } after calcAllowedPerChild, but that was just a typing bug. I still have a problem with calcActualAmount. If you run the code it wil work but try inputting following values.

1. 180 - code works
2. 170 - code works
3. 160 - wrong output


I know the function code is incomplete but still it must give the rite output if 160 is entered. Please just check it out for me and help me
Topic archived. No new replies allowed.