Description (It has to work with c++ please and we can\'t use pointers or classe
ID: 3733437 • Letter: D
Question
Description (It has to work with c++ please and we can't use pointers or classes thank you)
The purpose of this challenge is to get familiar with overloaded functions and functions with default parameters. This challenge simulates what an Uber-like service might use behind the scenes.
Requirements
In main(), ask the user to enter their full name. Use code as shown below to accept a string with spaces. getline() is a function available to you from iostream. You do not need–should not try–to create it.
In main(), ask the user to enter their destination. Destination will be entered as the full street name. Since street address will include spaces, use code similar to what was given in step 1
In main(), ask the user if their destination is within city limits. (In a real scenario, the GPS mapping software would be able to determine this based on the destination address. Since we don’t have that ability, we will simply ask the user).
In main(), ask the user to enter the distance of the fare. (In a real scenario, the GPS mapping software would be able to determine this based on the destination address. Since we don’t have that ability, we will simply ask the user).
Create a function calc_fare(double distance). This function will calculate the fare as follows: A fixed amount of $10 for distances up to 2 miles. $2.50 per mile for distances over 2 miles and up to 5 miles plus a fixed amount of $5.00. Anything over 5 miles will be charged at $3.50 per mile. Return the amount of the fare based on the above rate table.
Create a function calc_fare(double distance, double surcharge). This function calls the calc_fare() function in step 5 and returns a value adding the surcharge to the resulting fare.
Create a function calc_fare(double distance, bool local). When local is true, it means that the fare is within city limits. When local is false, it means that the fare goes outside city limits, in which case, an additional $50 surcharge will be added to the fare. Call calc_fare() from Step 5 and calc_fare() from Step 6 within this function, depending on the value of local. Note that the functions given in steps 5-7 are overloaded versions of the calc_fare()function.
Essentially, what we are doing in this exercise is creating the calc_fare()functions and calling them with driver calls. Drivers-no pun intended since this is a driving simulation-are basically the execution of functions manually to test that they are working, or calculating correctly. The input values of the calc_fare() functions are being entered manually by the user to test them. In a real use case of the functions, they will be called with distances determined by GPS mapping library calls.
Create a function called show_fare_info(string name, string destination, double fare, bool local = true). This function will display the information to the user based on the previous user input. This function shows information; what return type is best to use for this function? Call this function from main() to display the final output. This function gives a different message based on the local variable.
DO NOT USE
No real restrictions on what not to use, but you must use functions.
Sample partial main()
Sample Interaction / Output
Run it again:
Run it again:
Explanation / Answer
#include <cstdlib>
#include <iostream>
using namespace std;
void show_fare_info(string fullname,string destination,double fare,bool local = true){
cout<<"OK, ";
cout<<fullname;
cout<<", your fare to ";
cout<<destination;
cout<<" will be $";
cout<<fare;
if(!local){
cout<<". This fare includes a surcharge of $50 for going outside the city limits. Remember to use GUBER on your return trip home. We will offer a 10% discount if you use GUBER on your return.";
}
cout<<endl;
}
double calc_fare(double distance){
if(distance<=2){
return 10;
}else if(distance <=5){
return (distance*2.5) + 5;
}else{
return distance*3.5;
}
}
double calc_fare(double distance, double surcharge){
return calc_fare(distance)+surcharge;
}
double calc_fare(double distance ,bool fare){
if(fare){
return calc_fare(distance);
}else{
return calc_fare(distance,50.00);
}
}
int main(int argc, char** argv) {
string fullname,destination;
bool local;
double fare, distance;
cout<<"What is your full name?"<<endl;
getline(cin, fullname);
cout<<"Enter destination:"<<endl;
getline(cin, destination);
cout<<"What is the distance to "<<destination<<endl;
cin>>distance;
cout<<"Is this within the city?"<<endl;
{
char ch;
cin>>ch;
if( (ch == 'y') || (ch == 'Y') ){
local = true;
}else if( (ch == 'n') || (ch == 'N') ){
local = false;
}else{
while( (ch !='n')&&(ch !='N')&&(ch !='y')&&(ch !='Y') ){
cout<<"Is this within the city? (press n for no and y for yes)"<<endl;
cin>>ch;
}
}
}
if (local)
{
fare = calc_fare(distance, true);
show_fare_info(fullname, destination, fare);
}
else
{
fare = calc_fare(distance, false);
show_fare_info(fullname, destination, fare, false);
}
return 0;
}