calculation using fungtion

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
using namespace std;

const float maxPerUnit = 20000.00;
//minPerChild includes a standard fift consisting
//of a bath towel and 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;

//calculate amount allowed per child
    float calcAllowedPerChild(int nrChildernP)
    {
      float AmountPerChild;
  
        AmountPerChild = maxPerUnit / nrChildernP;
          if (AmountPerChild > 180)
            AmountPerChild = 180;
              else if (AmountPerChild < 100)
                AmountPerChild = 100;
         
           return AmountPerChild;
      }
//calculate the actual spent on an ophanage
      float calcActualAmount(float amtGift, int nrChildern)
      {
        float amtToothbrush = 0.0, amtNotebook = 0.0, amtHighlighters = 0.0,
        amtCrayons = 0.0, amtPen = 0.0;
        
          float total, totalForAll;
          total = amtGift;
          total -= TOOTHBRUSH;
  
  if (amtGift > 100)
  {
    total = amtGift - minPerChild;
    while (total > PEN)
    {
      if (total > TOOTHBRUSH)
      {
        total -= TOOTHBRUSH;
        amtHighlighters += total;
      }
      else if (total > HIGHLIGHTERS)
           {
             total -= HIGHLIGHTERS;
             amtHighlighters += total;
           }
      else if (total > CRAYONS)
           {
             total -= CRAYONS;
             amtCrayons += total;
            }
      else if (total > NOTEBOOK)
           {
             total -= NOTEBOOK;
             amtNotebook += total;
           }
      else
      {     
      total -= PEN; 
      amtPen += total;
      }
    }  
  
  }
  totalForAll = amtToothbrush + amtNotebook + amtHighlighters + amtCrayons + amtCrayons + minPerChild;
  return totalForAll;
}

    float calcTotalSpent(int nrChildern, float amtSpentPerChild)
    {
      return nrChildern * amtSpentPerChild;
    }

int main( )
{
  float amtGift;//Allowed amount per child
  float amtSpentPerChild = 0.00;//Actual Amount per child
  float totalSpent = 0.00;//total spent for one ophenage
  float totalAll = 0.00;//total spent for all ophenages
  int nrChildren;        //number of children per ophenage
  
    for (int i = 1; i <= 4; i++)
    {
      cout << "Orphanage " << i << endl;
      cout << "Enter the number of children: ";
      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;
      totalSpent = calcTotalSpent(nrChildren, amtSpentPerChild);
      cout << endl << "The actual amount spent was: R" ;
      cout << totalSpent << endl;
      cout <<endl;
      }
      //calculate the of the money spent on all orphanages      

      totalAll= totalAll + totalSpent;
      cout << endl;
      }
      //display the total spent on all ophanages
      cout <<"The total spent on All the ophanages is: R";
      cout << totalAll <<endl;
  return 0;
}



I need to calculate the total spent on all the orphanages using functions instead of using variables as I did.
totalAll= totalAll + totalSpent;

Please help
Your main has 2 {s and 3 }s. I suppose the wrong } is on line 109 ?
Anyway, just put lines 92-102 into some kind of function (I guess). You know how to do that.
Last edited on
Topic archived. No new replies allowed.