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 ?

Recommended Answers

All 5 Replies

Hello tamyln,
The problem lies with your calcactualamount funciton. You gave return amountp in a while loop. It's wrong. You should give return statement outside the loops. A function can return only one float value.

Use code tags.

You have a number of questionable things in that function.

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;/*why are you calling this agin?*/

	  }
	}

  return amountP;
  }
  while (leftToSpend == 0);/*whats the purpose of this?*/
}

Thanks, I've put the return outside of the loop, but by program still doesn't work. Well at least the compiler doesn't complain as much :) I don't really know how to go about it- given it my best shot. I'll have to give you guys the full(!) question I guess ?

Here it is (sorry it's so long, but all I'm focused on is calculating the actual amount in a value returning function):

The Linen-and-More store donates gifts to children in orphanages every year. This year they want to donate to children between the ages of 7 and 11. The manager is willing to spend R20000 per orphanage, and will donate to 4 orphanages. The minimum amount that will be spent on each child is R100, which includes a bath towel and a facecloth. Then one or more of the following gifts will be added if the amount allows it: toothbrush, highlighter, crayons, notebook and pen. The maximum amount that will be spent on each child is R180. Each child child will get a gift of at least R100, so if there are 300 children between the age of 7 and 11 in the orphanage, the store manager is prepared to spend a total of 100 * 300.

I have to write a program that will calculate the allowable amount for each child(done this function). If there are 60 children between the ages of 7 and 11, an amount of R330 can be spent per child. This is more than the maximum allowed amount of R180, therefore R180 will be spent on each child in this case. If 130 children - 153.84 is allowable amount. I must add the amount of the extra gift with the highest price first and keep on adding while the amount is less than the allowed amount. For example, say the allowable amount is R145,63, an amount of 142.90 will actually be spent (no gift can be added for R2.73(amountleft) so the total spent per child is R100 + R29.95(toothbrush) + R 12.95(notebook) = R142.90.


And you don't need to know the rest of the question to calculate the actual amount per child. So what I've put in bold is bothering me. I'm probably going about it the wrong way with the switch statements and more specifically, I don't know how to solve the problem of including adding the gift with the highest price(extra gift) first.

I'm probably over-complicating it in my mind.

Thanks ;)

Here is a better version which is close to correct but still not quite there. If I input 140 children I get:

allowable amount per child: R142.85
actual amount spent per child: 129.95 /* this should be 139.94 if you do the working
                                         you'll see why.
                                      */ amountP = calcAllowedPerChild()- leftToSpend  

Here is the improved version:

#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 
      leftToSpend = calcAllowedPerChild(nrChildrenP) - 100;


      if (leftToSpend > TOOTHBRUSH)
      leftToSpend -= TOOTHBRUSH;
      else if (leftToSpend > HIGHLIGHTERS)
      leftToSpend -= HIGHLIGHTERS;
      else if (leftToSpend > CRAYONS)
      leftToSpend -= CRAYONS;
      else if (leftToSpend > NOTEBOOK)
      leftToSpend -= NOTEBOOK;
      else if (leftToSpend > PEN)
      leftToSpend -= PEN;


      amountP = calcAllowedPerChild(nrChildrenP) - leftToSpend;



      return amountP;
      }




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;
}

No worries solved it.

float calcActualAmount(float amountP, int nrChildrenP)
{

      float leftToSpend;
      if (calcAllowedPerChild(nrChildrenP) > 180)
      amountP = 180;
      else 
      leftToSpend = calcAllowedPerChild(nrChildrenP) - 100;


      if (leftToSpend > TOOTHBRUSH)
      leftToSpend -= TOOTHBRUSH;
      if (leftToSpend > HIGHLIGHTERS)
      leftToSpend -= HIGHLIGHTERS;
      if (leftToSpend > CRAYONS)
      leftToSpend -= CRAYONS;
      if (leftToSpend > NOTEBOOK)
      leftToSpend -= NOTEBOOK;
      if (leftToSpend > PEN)
      leftToSpend -= PEN;



      amountP = calcAllowedPerChild(nrChildrenP) - leftToSpend;



      return amountP;
      }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.