Struct & Array Functions

Hello, I am having problems using the struct function and arrays. This is the code I have written, it complies successfully and everything but are missing a few parts.

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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <string.h>
#include <cmath>
#include <iostream>
using namespace std;

struct sPrice {bool student; bool disabled; int age; double price;};
struct sPrice SetParameters (bool student, bool disabled, int age, double price)
{
struct sPrice pr;
    pr.student = student;
    pr.disabled = disabled;
    pr.age = age;
    pr.price = price;
    return (pr);
}

struct sPassenger {int age; bool student; bool disabled; int from; int to;};
int main()
{
    int age; bool student; bool disabled; int from; int to; 

    cout << "Please Enter Your Age (0 - 150)" << endl;
    cin >> age;

    do
        {
        cout << "Please Enter Your Age Again (0 - 150)" << endl;
        cin >> age;
        }
    while (age < 0 || age > 150);

        if ( age > 12 && age < 65) age = 0;
        else if ( age >= 65) age = 1;
        else if ( age <= 12) age = 2;

    cout << "Are you Disabled (Y/N)?" << endl;
    char answer1;
    cin >> answer1;
    switch( answer1 )
        {
        case 'Y': case 'y': 
        disabled = true;
        break;

        case 'N': case 'n':
        disabled = false;
        break;
        }

    cout << "Are you a Student (Y/N)?" << endl;
    char answer2;
    cin >> answer2;
    switch( answer2 )
        {
        case 'Y': case 'y': 
        student = true;
        break;

        case 'N': case 'n':
        student = false;
        break;
        }

    cout << "From (1)Taipei, (2)Taichung, (3)Tainan:" << endl;
    cin >> from;

    cout << "To (1)Taipei, (2)Taichung, (3)Tainan:" << endl;
    cin >> to;

struct sPrice pr[11]; 
    pr[0] = SetParameters (true, true, 0, 2.8);
    pr[1] = SetParameters (true, true, 1, 1.7);
    pr[2] = SetParameters (true, true, 2, 1.8);
    pr[3] = SetParameters (true, false, 0, 3.6);
    pr[4] = SetParameters (true, false, 1, 2.9);
    pr[5] = SetParameters (true, false, 2, 1.9);
    pr[6] = SetParameters (false, true, 0, 3.3);
    pr[7] = SetParameters (false, true, 1, 2.0);
    pr[8] = SetParameters (false, true, 2, 2.2);
    pr[9] = SetParameters (false, false, 0, 5.6);
    pr[10] = SetParameters (false, false, 1, 3.7);
    pr[11] = SetParameters (false, false, 2, 3.2);

{
struct sPassenger ps;
    ps.age = age;
    ps.student = student;
    ps.disabled = disabled;
}

bool satisfy (struct sPassenger ps, struct sPrice pr[11]);
{

}

double CheckPrice(struct sPassenger ps, struct sPrice pr[11],int size);
{

} 

double distance;
if (from == 1 && to == 1) distance = 0;
    if (from == 1 && to == 2) distance = 165.0;
    if (from == 1 && to == 3) distance = 324.9;
    if (from == 2 && to == 2) distance = 0;
    if (from == 2 && to == 3) distance = 159.9;
    if (from == 2 && to == 1) distance = 165.0;
    if (from == 3 && to == 3) distance = 0;
    if (from == 3 && to == 2) distance = 159.9;
    if (from == 3 && to == 1) distance = 324.9;


double price;
    if (age == 0 && disabled == true && student == true) price = 2.8;
    else if (age == 1 && disabled == true && student == true) price = 1.7;
    else if (age == 2 && disabled == true && student == true) price = 1.8;
    else if (age == 0 && disabled == false && student == true) price = 3.6;
    else if (age == 1 && disabled == false && student == true) price = 2.9;
    else if (age == 2 && disabled == false && student == true) price = 1.9;
    else if (age == 0 && disabled == true && student == false) price = 3.3;
    else if (age == 1 && disabled == true && student == false) price = 2.0;
    else if (age == 2 && disabled == true && student == false) price = 2.2;
    else if (age == 0 && disabled == false && student == false) price = 5.6;
    else if (age == 1 && disabled == false && student == false) price = 3.7;
    else if (age == 2 && disabled == false && student == false) price = 3.2;

double ticketprice;
    ticketprice = price * distance;

printf ("Your Ticket Price is $ %.2f\n" , ticketprice);

return (0);
}


My problem is that I'm not sure how I am suppose to use the functions I'm required to, which are these (below). Instead of using these functions I used if statements to carry out the program.

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
struct sPrice pr[11]; 
    pr[0] = SetParameters (true, true, 0, 2.8);
    pr[1] = SetParameters (true, true, 1, 1.7);
    pr[2] = SetParameters (true, true, 2, 1.8);
    pr[3] = SetParameters (true, false, 0, 3.6);
    pr[4] = SetParameters (true, false, 1, 2.9);
    pr[5] = SetParameters (true, false, 2, 1.9);
    pr[6] = SetParameters (false, true, 0, 3.3);
    pr[7] = SetParameters (false, true, 1, 2.0);
    pr[8] = SetParameters (false, true, 2, 2.2);
    pr[9] = SetParameters (false, false, 0, 5.6);
    pr[10] = SetParameters (false, false, 1, 3.7);
    pr[11] = SetParameters (false, false, 2, 3.2);

{
struct sPassenger ps;
    ps.age = age;
    ps.student = student;
    ps.disabled = disabled;
}

bool satisfy (struct sPassenger ps, struct sPrice pr[11]);
{

}

double CheckPrice(struct sPassenger ps, struct sPrice pr[11],int size);
{

}


The satisfy() function in the program returns the conditions of the passenger as a bool type.
The CheckPrice() function uses the Satisfy () function to find the passenger fare and return the price per kilometer.
I am also suppose to use a two-dimensional array to manage the distance between stations. From Taipei to Taichung 165 km, from Taipei to Tainan 324.9 km and from Taichung to Tainan 159.9 km. What I got so far using arrays but I'm not sure what to do after that. How do I incorporate these distances with the price per kilometer.

1
2
3
4
5
6
7
8
9
10
double distance [2][2] = {from, to};
	distance [0][0] = 0;
	distance [0][1] = 165;
	distance [0][2] = 324.9;
	distance [1][0] = 165;
	distance [1][1] = 0;
	distance [1][2] = 159.9;
	distance [2][0] = 324.9;
	distance [2][1] = 159.9;
	distance [2][2] = 0;


Any ideas on how I'm suppose to complete this because I'm totally confused.
Last edited on
Would you mind including your original specifications/problem? I am having trouble seeing how I would attempt this just by looking at your code.
Ok I will try to explain.
This program is suppose to take the information provided by the user; age, are you a student or not, are you disabled or not and your to and from destinations (given as numbers 1,2 or 3). Based on these given information the program should output the price of the train ticket from your current destination to your final destination. This price is based on the distance in km between stations. The unit price vary depending on the information entered by the user, eg if you are between the ages of 12 and 65, you are disabled and you are a student the price would be 2.8. This 2.8 would then be multiplied by the distance between stations, eg if you are going from Taipei to Tainan it would be 2.8 x 324.9 = $909.72. That is basically how the program is suppose to work.
Here is how you can use the given prices.

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
#include <iostream>
#include <iomanip>
using namespace std;


enum destinations {
  placeA,
  placeB,
  placeC,
};

int main() {
  
  double distance[3][3];
  distance[0][0] = 0;
  distance[0][1] = 165;
  distance[0][2] = 324.9;
  distance[1][0] = 165;
  distance[1][1] = 0;
  distance[1][2] = 159.9;
  distance[2][0] = 324.9;
  distance[2][1] = 159.9;
  distance[2][2] = 0;

  std::cout << std::fixed;
  std::cout << std::setprecision(2);

  cout << "$" << distance[placeA][placeB] << endl;
  cout << "$" << distance[placeA][placeA] << endl;
  cout << "$" << distance[placeC][placeA] << endl;

  return 0;
}



I am a bit confused about these functions. It looks like you want to pass a struct to these functions but that is not how to pass structs to functions. usually you would pass by reference or pointer

1
2
3
4
5
6
7
8
9
bool satisfy (struct sPassenger ps, struct sPrice pr[11]);
{

}

double CheckPrice(struct sPassenger ps, struct sPrice pr[11],int size);
{

}




Last edited on
What do you mean by pass by reference or pointer?
Here is an example of what I mean.

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
#include <iostream>
#include <iomanip>
using namespace std;

struct person {

  string first;
  string last;

};
// pass by reference.
void printByRef(const person &aPerson);
// pass by pointer
void printByPointer(const person *aPerson2);
int main() {

  person aPerson1;
  aPerson1.first = "Foo";
  aPerson1.last = "Bar";

  person *aPerson2 = new person();
  aPerson2->first = "Bar";
  aPerson2->last = "Foo";

  printByRef(aPerson1);
  printByPointer(aPerson2);

  delete aPerson2;

  return 0;
}
void printByPointer(const person *aPerson2) { cout << aPerson2->first << " " << aPerson2->last << endl; }
void printByRef(const person &aPerson) { cout << aPerson.first << " " << aPerson.last << endl; }
Topic archived. No new replies allowed.