Getting a second function to work?

I'm supposed to write a code where one function calculates the fare of an Uber ride, and the second one outputs the costs and name. I'm encountering two problems. One: when I run this code my second function doesn't output anything. Two: Would it even be possible to return the fare and the name from the first function? ***Please disregard that it says cout << "Rider name" << fare. I'm simply trying to debug this.

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
/* Bryan Kobashi
bryan.kobashi@gmail.com
Lab 4 CIS 22A

*/
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>

using namespace std;

int main()
{
   string name;
   double minutes;
   double distance;
   char surge;
   char option;
   double multiplier;
   double baseFare;
   double perMinute;
   double perMile;
   double minimum;
   double fare;

    cout << "Enter first and last name then press enter: ";
    getline (cin, name);
    cout << "Enter the length of your ride in minutes: ";
    cin >> minutes;
    cout << "Enter the distance of your trip in miles: ";
    cin >> distance;
    cout << "Enter your budget option, S for SUV, X for budget, and L for luxury: ";
    cin >> option;
    cout << "Is this a surge trip? Enter Y for yes, and N for no: ";
    cin >> surge;
switch (option)
{
case 'L':
    baseFare = 5.00;
    perMinute = 0.50;
    perMile = 2.75;
    minimum = 10.55;
    break;
case 'S':
   baseFare = 15.00;
    perMinute = 0.90;
    perMile = 3.75;
    minimum = 25.00;
    break;
case 'X':
     baseFare = 2.00;
    perMinute = 0.22;
    perMile = 1.15;
    minimum = 6.55;
    break;
}
        if (surge == 'Y'){
            cout << "Enter the surge multiplier: ";
            cin >> multiplier;

fare = (baseFare * multiplier) + (perMinute * minutes) + (perMile * distance);
if (fare < minimum){
    fare = minimum;}
    else{
        fare = fare;

   return fare;


    }

        }
}
double fare (double fare)
{
    cout << "Rider's name: " << fare;
}

Last edited on
One: when I run this code my second function doesn't output anything.

That's because your second function is never called.


Two: Would it even be possible to return the fare and the name from the first function?

Yes. You can either return a structure containing both bits of information, pass the variables you wish to take on those values via reference (or, if you're taking a c approach, pass the addresses of those variables) or a combination of the two.

However, it is definitely not possible if that function is main since main returning means your program has ended.

Tip: Don't use the same names for functions and other variables. In your code you have a function named fare and two different variables of type double sharing the name.
Topic archived. No new replies allowed.