Expert Q&A; Done Need help creating this program using pointer in c++ Part l-Bon
ID: 3729647 • Letter: E
Question
Expert Q&A; Done Need help creating this program using pointer in c++ Part l-Bonus £50 points) Design a prog am to calculate the cost of gas for a trip. The user will be prompted for the cost of agaon of gas, the number of miles of the trip, and the number of miles per gallon the car gets. The program should use pointers, the address operator, and the dereferencing operator for ALL input, caloulations and output Enter, compile, link, and execute Lab7GasTripfinstinitiallastNome.app The following is a sample output that might appear after running your program Input by the user is shown in bold This program eaieulates the east of ga for tri.p when th* user entrs tho coat gas, e nuber ot lies, and the urber of nssea per gailen the vehlele gets Enter the nuber of miles in your teip 89 Enter the mpg your car gecs 2 Enter the cost of gasolines 1.7 Your trip coat 568-0 Hint: the trip oostcost of gasoline (number of miles/mpe your car gExplanation / Answer
#include <iostream>
using namespace std;
int main() {
int miles,*milesptr;
double mpg,*mpgptr,cost,*costptr,tripCost,*tripCostPtr;
cout<<"This program calculates the cost of gas for a trip when the user enters";
cout<<" the cost of gas , the number of miles and the number of miles per gallon";
cout<<" the vehicle gets";
//input miles,mpg and cost of gasoline per gallon
cout<<" Enter the number of miles in your trip : ";
cin>>miles;
cout<<" Enter the mpg your car gets : ";
cin>>mpg;
cout<<" Enter the cost of gasoline : ";
cin>>cost;
//assign address of variables to pointers
mpgptr = &mpg;
milesptr = &miles;
costptr = &cost;
tripCostPtr = &tripCost;
//use pointers to compute trip cost
*tripCostPtr = (*milesptr)/(*mpgptr) * *costptr;
cout<<" Your trip will cost : $"<<*tripCostPtr;
return 0;
}
Output:
This program calculates the cost of gas for a trip when the user enters
the cost of gas , the number of miles and the number of miles per gallon
the vehicle gets
Enter the number of miles in your trip :890
Enter the mpg your car gets :23
Enter the cost of gasoline :1.79
Your trip will cost : $69.2652
Do ask if any queries. Please upvote if the answer is helpful.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.