How to define function...passing values?

I am writing a program that executes two operations on a structure distanceType which is compose of the int members: feet, yards, and miles.The function convertYards takes a distance in yards that is greater than or equal to 1760 (because there are 1760 yards in a mile) and converts it to x miles, y yards, and z feet (into distanceType). I'm having trouble with the parameters. It takes in an integer value and returns a distanceType. The problem is I am not sure how to define the function, because it's supposed to accept either distance1 or distance2. In my definition: distanceType convertYards( int ---) what am I supposed to put in place of the "---"?

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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
int main()
{
    char choice;
    char type1, type2;
    int  distance1, distance2;
    int time;
    distanceType var1, var2;

    do
    {
        cout << "Enter A to add two distances, B to calculate miles per hour (or EXIT to exit):" << endl;
        cin >> choice;
        if (choice=='A'||choice=='a')
        {
            cout << "Enter distance type for first distance (M for miles, Y for yards, or F for feet):" << endl;
            cin >> type1;
            cout << "Enter distance type for second distance (M for miles, T for yards, or F for feet):" << endl;
            cin >> type2;
            switch (type1)
            {
                case 'M': case 'm':
                    {
                        cout << "Enter distance in miles:" << endl;
                        cin >> var1.miles;
                        break;
                    }
                case 'Y': case 'y':
                    {
                        cout << "Enter distance in yards:" << endl;
                        cin >> distance1;
                        if (distance1 < 1760)
                        {
                            var1.yards = distance1;
                        }
                        else
                            if (distance1 >= 1760)
                            {
                                convertYards(distance1);
                            }
                        break;
                    }
                case 'F': case 'f':
                    {
                        cout << "Enter distance in feet:" << endl;
                        cin >> distance1;
                        if (distance1 < 3)
                        {
                            var1.feet = distance1;
                        }
                        else
                            if (distance1 >= 3)
                            {
                                convertFeet (distance1);
                            }
                        break;
                    }
                default:
                    cout << "Invalid distance type" << endl;
            }
            switch (type2)
            {
                case 'M': case 'm':
                {
                    cout << "Enter distance in miles:" << endl;
                    cin >> var2.miles;
                    break;
                }
                case 'Y': case 'y':
                {
                    cout << "Enter distance in yards:" << endl;
                    cin >> distance2;
                    if (distance2 < 1760)
                    {
                        var2.yards = distance2;
                    }
                    else
                        if (distance2 >= 1760)
                        {
                            convertYards(distance2);
                        }
                    break;
                }
                case 'F': case 'f':
                {
                    cout << "Enter distance in feet:" << endl;
                    cin >> distance2;
                    if (distance2 < 3)
                    {
                        var2.feet = distance2;
                    }
                    else
                        if (distance2 >= 3)
                        {

                            convertFeet(distance2);
                        }
                    break;
                }
                default:
                    cout << "Invalid distance type" << endl;
            }
            addDistance(var1, var2);
        }
        else
            if(choice=='B'||choice=='b')
            {
                cout << "Enter distance type (M for miles, Y for yards, or F for feet):" << endl;
                cin >> type1;
                cout << "Enter a time (in minutes):" << endl;
                cin >> time;
                switch (type1)
                {
                    case 'M': case 'm':
                    {
                        cout << "Enter distance in miles:" << endl;
                        cin >> var1.miles;
                        break;
                    }
                    case 'Y': case 'y':
                    {
                        cout << "Enter distance in yards:" << endl;
                        cin >> distance1;
                        if (distance1 < 1760)
                        {
                            var1.yards = distance1;
                        }
                        else
                            if (distance1 >= 1760)
                            {
                                convertYards(distance1);
                            }
                        break;
                    }
                    case 'F': case 'f':
                    {
                        cout << "Enter distance in feet:" << endl;
                        cin >> distance1;
                        if (distance1 < 3)
                        {
                            var1.feet = distance1;
                        }
                        else
                            if (distance1 >= 3)
                            {
                                convertFeet(distance1);
                            }
                        break;
                    }
                    default:
                        cout << "Invalid distance type" << endl;
                }
                calcMPH(var1, time);
            }
    } while (choice != "EXIT");
}

struct distanceType
{
    int miles;
    int yards;
    int feet;
}

distanceType convertYards(int
{
In my opinion, most programmers can't write a complete program then put in their calculations and have it work the first time. Do the math portion first, Then do the output section, and you'll know what the user input variables should be. T

In convertYards you want to get the value from another function, so you can pass the value or make a global value.

In this case, when the user enters a distance, I would convert that to feet. Pass feet to convertYards.

Once you have the distance in feet, it's easy to convert to yards or miles.
Otherwise, if you add distance 1 and 2, and the values are 59, 59, 59 you have a hard time converting 118 miles 118 yards and 118 feet into anything.
if A add the distances and then convert
if B 60 miles per hour = 88 feet per second.

To me it looks like you have a lot of things to fix in your code.

If I was doing this, I would start simple, get the math working, get the output looking like I wanted, and then add the user input.

Sorry i can't help but play with examples when I'm thinking about a problem. Hope this is helpful.

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
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
using namespace std;

    int yards=1760;
    int feet=5280;

    int UserInputFeet=5287;
    int TimeinMins=60;
    int x=0;  // Miles
    int y=0;  // Yards
    int r=0;  // Remainder

void convert()
{
x=UserInputFeet/feet;     // Miles traveled
r=(UserInputFeet-(x*feet));
y=r/3;                    // Yards
r=(UserInputFeet-(x*feet));
r=(r-(y*3));              // Feet
}

void Report()
{
cout << " Miles Traveled " << x << endl;
cout << " Yards Traveled " << y << endl;
cout << " Feet Traveled  " << r << endl; // Feet traveled
cout << " User traveled " << x << " Miles " << y << " Yards " << r << " Feet  in " << (TimeinMins/60) << " hour" << endl;
cout << " User traveled " << UserInputFeet << " Feet in " << TimeinMins << " Mins" << endl;
cout << " User traveled " << UserInputFeet << " Feet in " << (TimeinMins*60) << " Seconds" << endl;
}

int main()
{
convert ();
Report();
return 0;
}

Last edited on
Topic archived. No new replies allowed.