Can someone please help me figure out what I\'m doing wrong? I\'ve provided the
ID: 3737409 • Letter: C
Question
Can someone please help me figure out what I'm doing wrong? I've provided the code I've been working on, and I'm sure I'm close to finishing it but something is wrong and I can't seem to spot it. It's stopping at table one, and isn't continuing.
#include <iostream>
#include<cmath>
#include<iomanip>
using namespace std;
float MPG(float,float,float,int);
void PrintTable(int , int , int,float,float ,float,int );
void CostTracker(int,int,int,int,double,float,float);
void MaxEconomy(int,int,int,float,float,float,float,int);
void output(float,float,double,int);
float capacity,amt,cost;
double price, dist;
int main()
{
while(1)
{
int choice;
float a,b,c;
int speed,smin,smax,sinc;
cout << " Fuel Economy of a Vehicle " << endl;
cout << " Please choose from the list below to continue the program or exit:"<<endl;
cout << " 1) Start/Continue program " << endl;
cout << " 2) End/exit program " << endl;
cout << "Choice: ";
cin >> choice;
switch(choice)
{
case 1:
{
cout << " Please answer all basic questions about trip below: ";
cout<<" Enter the values for coefficients A, B, and C: ";
cin>>a>>b>>c;
cout<<" Enter the capacity of the fuel tank (in gallons): " ;
cin>>capacity;
if(capacity<0 || capacity>20)
{
cout<<"Fuel tank capacity should be in range :(0 to 20) gallons.. please re-enter the value: ";
cin>>capacity;
}
cout<<"Enter the current amount of fuel in the tank (in gallons): ";
cin>>amt;
if((amt/capacity)*100 < 20 || (amt/capacity)*100 >100)
{
cout<<"Current amount of fuel in tank should be in range :(20% - 100%).. please re-enter the value: ";
cin>>amt;
}
else
amt == amt;
cout<<" Enter the current speed of the vehicle (in mpg): ";
cin>>speed;
if(speed<20 || speed >80)
{
cout<<"Current speed of vehicle should"<< endl;
cout <<" be in range:(20 to 80) mph.. please re-enter the value: ";
cin>>speed;
}
cout<<" Enter the distance to be traveled on the current trip (in miles): ";
cin>>dist;
if(dist<0)
{
cout<<"Distance should be greater than 0..please re-enter the value: ";
cin>>dist;
}
cout<<" Enter the cost per gallon for gasoline: ";
cin>>cost;
if(cost<0)
{
cout<<"Gasoline cost should be greater than 0..please re-enter the value: ";
cin>>cost;
}
cout<<" Enter the minimum speed (Smin): ";
cin>>smin;
if(smin<20 || smin >50)
{
cout<<" Integer value should be (20 < Smin < 50).Re-enter the value:";
cin>>smin;
}
cout<<" Enter the maximum speed( Smax): ";
cin>>smax;
if(smax<(smin + 10) || smax >80)
{
cout<<"Integer value should be((Smin + 10) < Smax < 80).. re-enter the value:";
cin>>smax;
}
cout<<" Enter the speed increment, (Sinc): ";
cin>>sinc;
int dum=(int)(smax-smin)/5;
if(sinc<0 || sinc>dum )
{
cout<<" Integer value should be(0 < Sinc < (Smax – Smin)/5)..re-enter the value:";
cin>>sinc;
}
float y= MPG(a,b,c,speed);
cout<<" Y: "<<y<<endl;
PrintTable(smin,smax,sinc,a,b, c,speed);
break;
}
case 2:
{
cout << "ending program now...";
return 0;
}
}
}
}
float MPG(float a,float b,float c,int x)
{
float y = ((a*(float)x*x) + (b*(float)x) + c);
return y;
}
void PrintTable(int smin, int smax, int sinc,float a,float b,float c,int s)
{
cout<<" Fuel Economy Table"<<endl;
cout<<"|" << setw(18) << "Speed (mph) "<< "|" << setw(18) << "Fuel Economy (mpg)"<< "|" << endl;
float mpg[200];
for(int i=smin;i<=smax;(i=i+sinc))
{
mpg[i]= MPG(a,b,c,i);
cout<< "|" << setw(18)<<i<< "|" << setw(18) << mpg[i] << "|" << endl;
}
int mini=smin;
int maxi=smin;
for(int i=(smin+sinc);i<=smax;(i=i+sinc))
{
if(mpg[mini]>mpg[i])
mini=i;
if(mpg[maxi]<mpg[i])
maxi=i;
}
cout<<"Maximum Fuel Economy: "<<mpg[maxi]<<" mpg at "<<maxi<<" mph"<<endl;
cout<<"Current Fuel Economy: "<<mpg[s]<<" mpg at "<<s<<" mph"<<endl;
cout<<"Minimum Fuel Economy: "<<mpg[mini]<<" mpg at "<<mini<<" mph"<<endl<< endl;
cout<<" Trip Option #1: "<<endl;
MaxEconomy(smin, smax, sinc, a,b,c, mpg[maxi], maxi);
cout<<" Trip Option #2: "<<endl;
MaxEconomy(smin, smax, sinc, a,b,c, mpg[s], s);
}
void MaxEconomy(int smin,int smax,int sinc,float a,float b,float c,float mpg,int mph)
{
int sum,R,k,count;
cout<<"Travelling "<< dist <<" miles at "<<mph<<" mph with "<< amt <<" gallons currently in the tank and gas cost of $"<< cost <<endl;
cout<<"Fuel economy: "<<mpg<<" mpg"<<endl;
cout<<"Speed: "<<mph<<" mph"<<endl;
output(mpg,mph,price,sum);
}
void CostTracker(int R, int sum, int k,int count,float mpg,float mph)
{
while(dist!=0)
{
dist=dist-mpg; // Subtracks how many many miles have been traveled within an hour until the distance equals zero.
amt = amt-1; // Subtracks 1 gallon of fuel from the amount in the tank.
sum = sum++; // keeps track of each gallon expended for the entire trip
if((amt/capacity)*100<10) // Checks the current amount of fuel in the tank after each loop to determine if refuel is nessary.
{
R= capacity-amt; // Determines how many gallons are nesseary to obtain a full tank.
amt = amt + R; // Adds the amount of fuel nessary to obtain a full tank with the current amount in the tank.
k = k+R; // keeps track of how many gallons have been bought over the course of the entire trip.
count = count++;
}
}
price = sum*cost;
}
void output(float mpg,float mph,double price,int sum)
{
int R,k,count;
CostTracker(R,sum,k,count,mpg,mph);
cout<<"Fuel cost for the trip: "<< price <<endl;
cout<<"Number of gallons that will be used for the trip: "<< sum <<endl;
cout<<"Time to reach the destination: "<<(dist/mph)<<endl;
cout<<"Number of times to fill the tank: "<< count <<endl;
cout<<"Number of gallons left in the tank after the trip: "<< amt <<endl;
cout<<"Number of miles until the next time the tank must be filled after the trip: "<<((mpg/amt))<<endl;
}
Explanation / Answer
#include <iostream>
#include<cmath>
#include<iomanip>
using namespace std;
float MPG(float,float,float,int);
void PrintTable(int , int , int,float,float ,float,int );
void CostTracker(int,int,int,int,double,float,float);
void MaxEconomy(int,int,int,float,float,float,float,int);
void output(float,float,double,int);
float capacity,amt,cost;
double price, dist;
int main()
{
while(1)
{
int choice;
float a,b,c, y;
int speed,smin,smax,sinc,dum;
cout << " Fuel Economy of a Vehicle " << endl;
cout << " Please choose from the list below to continue the program or exit:"<<endl;
cout << " 1) Start/Continue program " << endl;
cout << " 2) End/exit program " << endl;
cout << "Choice: ";
cin >> choice;
switch(choice)
{
case 1:
cout << " Please answer all basic questions about trip below: ";
cout<<" Enter the values for coefficients A, B, and C: ";
cin>>a>>b>>c;
cout<<" Enter the capacity of the fuel tank (in gallons): " ;
cin>>capacity;
if(capacity<0 || capacity>20)
{
while(1) {
cout<<"Fuel tank capacity should be in range :(0 to 20) gallons.. please re-enter the value: ";
cin>>capacity;
if(capacity>=0 && capacity<=20)
break;
}
}
cout<<"Enter the current amount of fuel in the tank (in gallons): ";
cin>>amt;
if((amt/capacity)*100 < 20 || (amt/capacity)*100 > 100)
{
while(1) {
cout<<"Current amount of fuel in tank should be in range :(20% - 100%).. please re-enter the value: ";
cin>>amt;
if((amt/capacity)*100 >= 20 && (amt/capacity)*100 <= 100)
break;
}
}
cout<<" Enter the current speed of the vehicle (in mpg): ";
cin>>speed;
if(speed<20 || speed >80)
{
while(1) {
cout<<"Current speed of vehicle should be in range:(20 to 80) mph.. please re-enter the value: ";
cin>>speed;
if(speed>=20 && speed <=80)
break;
}
}
cout<<" Enter the distance to be traveled on the current trip (in miles): ";
cin>>dist;
if(dist<=0)
{
while(1) {
cout<<"Distance should be greater than 0..please re-enter the value: ";
cin>>dist;
if(dist > 0)
break;
}
}
cout<<" Enter the cost per gallon for gasoline: ";
cin>>cost;
if(cost<=0)
{
while(1) {
cout<<"Gasoline cost should be greater than 0..please re-enter the value: ";
cin>>cost;
if(cost > 0)
break;
}
}
cout<<" Enter the minimum speed (Smin): ";
cin>>smin;
if(smin<20 || smin >50)
{
while(1) {
cout<<" Integer value should be (20 < Smin < 50).Re-enter the value:";
cin>>smin;
if(smin>=20 && smin <=50)
break;
}
}
cout<<" Enter the maximum speed( Smax): ";
cin>>smax;
if(smax<(smin + 10) || smax >80)
{
while(1) {
cout<<"Integer value should be((Smin + 10) < Smax < 80).. re-enter the value:";
cin>>smax;
if(smax>=(smin + 10) && smax <=80)
break;
}
}
cout<<" Enter the speed increment, (Sinc): ";
cin>>sinc;
dum =(int)(smax-smin)/5;
if(sinc<=0 || sinc>dum )
{
while(1) {
cout<<" Integer value should be(0 < Sinc < (Smax – Smin)/5)..re-enter the value:";
cin>>sinc;
if(sinc>0 && sinc<dum)
break;
}
}
y = MPG(a,b,c,speed);
cout<<" Y: "<<y<<endl;
PrintTable(smin,smax,sinc,a,b, c,speed);
break;
case 2:
cout << "ending program now...";
return 0;
}
}
}
float MPG(float a,float b,float c,int x)
{
float y = ((a*(float)x*x) + (b*(float)x) + c);
return y;
}
void PrintTable(int smin, int smax, int sinc,float a,float b,float c,int s)
{
cout<<" Fuel Economy Table"<<endl;
cout<<"|" << setw(18) << "Speed (mph) "<< "|" << setw(18) << "Fuel Economy (mpg)"<< "|" << endl;
float mpg[200];
for(int i=smin;i<=smax;(i=i+sinc))
{
mpg[i]= MPG(a,b,c,i);
cout<< "|" << setw(18)<<i<< "|" << setw(18) << mpg[i] << "|" << endl;
}
int mini=smin;
int maxi=smin;
for(int i=(smin+sinc);i<=smax;(i=i+sinc))
{
if(mpg[mini]>mpg[i])
mini=i;
if(mpg[maxi]<mpg[i])
maxi=i;
}
cout<<"Maximum Fuel Economy: "<<mpg[maxi]<<" mpg at "<<maxi<<" mph"<<endl;
cout<<"Current Fuel Economy: "<<mpg[s]<<" mpg at "<<s<<" mph"<<endl;
cout<<"Minimum Fuel Economy: "<<mpg[mini]<<" mpg at "<<mini<<" mph"<<endl<< endl;
cout<<" Trip Option #1: "<<endl;
MaxEconomy(smin, smax, sinc, a,b,c, mpg[maxi], maxi);
cout<<" Trip Option #2: "<<endl;
MaxEconomy(smin, smax, sinc, a,b,c, mpg[s], s);
}
void MaxEconomy(int smin,int smax,int sinc,float a,float b,float c,float mpg,int mph)
{
int sum,R,k,count;
cout<<"Travelling "<< dist <<" miles at "<<mph<<" mph with "<< amt <<" gallons currently in the tank and gas cost of $"<< cost <<endl;
cout<<"Fuel economy: "<<mpg<<" mpg"<<endl;
cout<<"Speed: "<<mph<<" mph"<<endl;
output(mpg,mph,price,sum);
}
void CostTracker(int R, int sum, int k,int count,float mpg,float mph)
{
while(dist!=0)
{
dist=dist-mpg; // Subtracks how many many miles have been traveled within an hour until the distance equals zero.
amt = amt-1; // Subtracks 1 gallon of fuel from the amount in the tank.
sum = sum++; // keeps track of each gallon expended for the entire trip
if((amt/capacity)*100<10) // Checks the current amount of fuel in the tank after each loop to determine if refuel is nessary.
{
R= capacity-amt; // Determines how many gallons are nesseary to obtain a full tank.
amt = amt + R; // Adds the amount of fuel nessary to obtain a full tank with the current amount in the tank.
k = k+R; // keeps track of how many gallons have been bought over the course of the entire trip.
count = count++;
}
}
price = sum*cost;
}
void output(float mpg,float mph,double price,int sum)
{
int R,k,count;
CostTracker(R,sum,k,count,mpg,mph);
cout<<"Fuel cost for the trip: "<< price <<endl;
cout<<"Number of gallons that will be used for the trip: "<< sum <<endl;
cout<<"Time to reach the destination: "<<(dist/mph)<<endl;
cout<<"Number of times to fill the tank: "<< count <<endl;
cout<<"Number of gallons left in the tank after the trip: "<< amt <<endl;
cout<<"Number of miles until the next time the tank must be filled after the trip: "<<((mpg/amt))<<endl;
}
//The issue is with inputs. I have cleared those issues. Lemme know if you have any queries.