User Input Arrays Task

Hi Guys,

Hope you are all well, I have to write a program for an online course, it is for the task below HOWEVER it must be designed so that the age of each family member (6 in total)is entered into
an array. Once the user has entered all the data calculate the fare total using the populated
array. SO my question is how should I go about this, I have basic c++ knowledge but arrays are new to me and watching youtube videos isnt helping as alot are far too advanced for me.

Here is the task-

"A standard class return fare for a journey from London to Paris is normally about £80,
however a new deal gives special rates for children and seniors as follows:

Age Cost
16-64 £80.00
65 or over £52.60
12-15 £55.00
4-11 £40.20
under 4 free
Design and write a program that reads in the ages of family members and calculates the
total cost of the fair. Use this program to calculate the cost for a family of six with the
following ages: 36, 40, 65, 11,12 and 16. You should find this comes to £387.80."
This is how I have started but it looks and feels like im going about it wrong :/

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
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>

using namespace std;

int main(){

   int familyAges[5];

   cout << "please enter first family members age" << endl;
   cin >> familyAges[0];

   cout << "please enter second family members age" << endl;
   cin >> familyAges[1];

   cout << "please enter third family members age" << endl;
   cin >> familyAges[2];

   cout << "please enter fourth family members age" << endl;
   cin >> familyAges[3];

   cout << "please enter fifth family members age" << endl;
   cin >> familyAges[4];

   cout << "please enter sixth family members age" << endl;
   cin >> familyAges[5];
Anybody :/
You can then do for looping on your code.
////this is a pseudocode
for(unsigned int i = 0;i<6;i++) ////I'm assuming it to be static, i<6, e.g: not changing
{
if(familyAges[i]<4) cost+= 0;
else if(familyAges[i]<11) ///that way you dont have to write them <> again and again
////etc
}
Note for the cost to be of double type and initialized to zero. (double cost = 0)
Last edited on
Thanks for the reply, I just don't get how to lay out my code, also I dont understand how to make the code populate each element of the family age array with a number, I'm really sorry to ask but could somebody explain it a little more, above was a great help though thankyou.
This is what I got so far, my mind is going blank and I am setting it up wrong :/
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
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>

using namespace std;

int main(){

   int familyAges[5];
   double cost=0;


   cout << "please enter first family members age" << endl;
   cin >> familyAges[0];

   cout << "please enter second family members age" << endl;
   cin >> familyAges[1];

   cout << "please enter third family members age" << endl;
   cin >> familyAges[2];

   cout << "please enter fourth family members age" << endl;
   cin >> familyAges[3];

   cout << "please enter fifth family members age" << endl;
   cin >> familyAges[4];

   cout << "please enter sixth family members age" << endl;
   cin >> familyAges[5];

   for(unsigned int i = 0;i<6;i++){
   if(familyAges[i]<4) {
        cost += 0; }
      else if(familyAges[i]=>4 && <11) {
        cost += 40.20; }
   else if (familyAges[i]>11 && <=15) {
        cost += 55.00;
   }
   }

else if(familyAges[i]=>4 && <11) {
////you dont do that it should be familyAges[i]=>4&&familyAges[i]<11
cost += 40.20; }
else if (familyAges[i]>11 && <=15) { ////same
cost += 55.00;
}
Last edited on
Topic archived. No new replies allowed.