I need help with this program please C++, Thank you! The txt file is prog5.txt:
ID: 3733599 • Letter: I
Question
I need help with this program please C++, Thank you!
The txt file is prog5.txt:
1
California
2376
164.60
69
3
0
1
Alaska
3791.7
307.26
130.75
1
Florida Keys
1363.6
128.18
54.5
2
Explanation / Answer
//prog5.h
#include <string>
using namespace std;
class Trip{
private:
float m_distance;
float m_gasCost;
float m_GallonsOfGas;
std::string m_destination;
public:
//Default constructor
Trip();
//Overloded constructor
Trip(float distance, float gasCost, float gallonsOfGas, std::string destination);
//Setters
void SetDistance(float& distance);
void SetGasCost(float& gasCost);
void SetGallonsOfGas(float& gallonsOfGas);
void SetDestination(std::string& destination);
//Getters
float GetDistance() const;
float GetGasCost() const;
float GetGallonsOfGas() const;
std::string GetDestination() const;
//Extra needed function
double GetMilesPerGallon() const;
double GetCostPerGallon() const;
double GetCostPerMile() const;
};
//prog5.cpp
#include "prog5.h"
#include <iostream>
// Implementaion of class functions
Trip::Trip()
{
this->m_distance = 0;
this->m_gasCost = 0;
this->m_GallonsOfGas = 0;
this->m_destination = "";
}
Trip::Trip(float distance, float gasCost, float gallonsOfGas, std::string destination){
this->m_distance = distance;
this->m_gasCost = gasCost;
this->m_GallonsOfGas = gallonsOfGas;
this->m_destination = destination;
}
void Trip::SetDistance(float& distance)
{
this->m_distance = distance;
}
void Trip::SetGasCost(float& gasCost)
{
this->m_gasCost = gasCost;
}
void Trip::SetGallonsOfGas(float& gallonsOfGas)
{
this->m_GallonsOfGas = gallonsOfGas;
}
void Trip::SetDestination(string& destination)
{
this->m_destination = destination;
}
float Trip::GetDistance() const
{
return this->m_distance;
}
float Trip::GetGasCost() const
{
return this->m_gasCost;
}
float Trip::GetGallonsOfGas() const
{
return this->m_GallonsOfGas;
}
string Trip::GetDestination() const
{
return this->m_destination;
}
double Trip::GetMilesPerGallon() const
{
double milesPerGallon = this->m_distance/this->m_GallonsOfGas;
return milesPerGallon;
}
double Trip::GetCostPerGallon() const
{
double costPerGallon = this->m_gasCost/this->m_GallonsOfGas;
return costPerGallon;
}
double Trip::GetCostPerMile() const
{
double costPerMile = this->m_gasCost/this->m_distance;
return costPerMile;
}
//prog5client.cpp
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <iostream>
#include <fstream>
#include <string.h>
#include "prog5.h"
using namespace std;
void PrintHeading()
{
printf("=========================================== ");
printf("Welcome to the Trip Calculator Application ");
printf("=========================================== ");
}
void PrintMenu()
{
printf(" 1. Add trip ");
printf(" 2. Quit ");
printf("=========================================== ");
}
ifstream m_txtdata_in;
void GetInput(Trip* trip, string (&word)[255], int& counter )
{
string destination;
cout<< "Destination? "<<endl;
destination = word[counter];
cout<<destination<<endl;
counter++;
cout<< "Distance? "<<endl;
float distanceFloat = strtof((word[counter]).c_str(),0); // string to float
cout<<distanceFloat<<endl;
counter++;
cout<< "Cost of gas? "<<endl;
float costGasFloat = strtof((word[counter]).c_str(),0); // string to float
cout<<costGasFloat<<endl;
counter++;
cout<< "Gallons? "<<endl;
float gallonsFloat = strtof((word[counter]).c_str(),0); // string to float
cout<<gallonsFloat<<endl;
counter++;
printf("=========================================== ");
trip->SetDistance(distanceFloat);
trip->SetGasCost(costGasFloat);
trip->SetGallonsOfGas(gallonsFloat);
trip->SetDestination(destination);
}
void ShowOutput(Trip* trip)
{
cout<< "Your trip to "<<trip->GetDestination()<< " (" << trip->GetDistance()<<") miles"<<endl;
cout<< "Miles per gallon:"<<" "<<trip->GetMilesPerGallon()<<endl;
cout<< "Cost per mile:"<<" "<<trip->GetCostPerMile()<<endl;
cout<< "Cost per gallon:"<<" "<<trip->GetCostPerGallon()<<endl;
printf("=========================================== ");
}
int main()
{
//Using this to read the txt file line by line
string testline;
string word[255];
PrintHeading();
//change the file path to your directory path
m_txtdata_in.open ("prog5.txt");
if(!m_txtdata_in)
{
cerr << "File could not be opened" << endl;
return 1;
}
int itemCount = 0;
//store file in array
int i=0;
while(getline( m_txtdata_in, testline))
{
word[i]=testline;
i++;
}
m_txtdata_in.close();
int counter = 0;
while(counter<i)
{
PrintMenu();
// convert 1st value to integer
int enteredChoiceInt = stoi(word[counter]);
// Increasing the counter
counter++;
if(enteredChoiceInt == 1)
{
Trip* trip = new Trip();
GetInput(trip, word, counter);
ShowOutput(trip);
} else if(enteredChoiceInt == 2)
{
cout<<"Goodbye"<<endl;
break;
} else{
cout<<"Invalid Choice"<<endl;
}
}
return 0;
}
//prog5.txt
1
California
2376
164.60
69
3
0
1
Alaska
3791.7
307.26
130.75
1
Florida Keys
1363.6
128.18
54.5
2
//PLEASE PROVIDE FEEDBACK THUMBS UP
//I fixed it to work with your txt file