I was wondering if anyone would be kind enough to review my basic c++ program. I'm a beginner programmer so was hoping to get feedback on how well I've used object oriented programming i.e. using a class in my program. And in general, how can I make my program better? Many thanks in advance.
// Created by George Austin Bradley on 08/12/2019.
// Copyright © 2019 George Austin Bradley. All rights reserved.
//
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
using namespace std;
class User{
private:
string sName;
string sCarName;
string sCarFuelType;
string sChoiceOfLocation;
double dFuelCost;
int iMilesToTravel;
double dTotalToBePaid;
double dVATAmount;
double dEstimatedTime;
public:
User()
:sName(""),sCarName(""), sCarFuelType(""), sChoiceOfLocation(""), dFuelCost(0), iMilesToTravel(0), dTotalToBePaid(0),dVATAmount(0), dEstimatedTime(0){
}
void SetName()
{
cout << "What's your name?: ";
cin >> sName;
}
void SetCarFuelType(){
bool bValid = false;
char cSelection = 0;
do{
cout << "What's your fuel type? (p) petrol or (d) diesel: ";
cin >> cSelection;
cSelection = toupper(cSelection);
if (cSelection == 'P')
{
sCarFuelType = "Petrol";
dFuelCost = 0.33;
cout << "You've chosen " << sCarFuelType << " as your fuel type.\n";
bValid = true;
}
else if (cSelection == 'D')
{
sCarFuelType = "Diesel";
dFuelCost = 0.40;
cout << "You've chosen " << sCarFuelType << " as your fuel type.\n";
bValid = true;
}
else
{
cout << "Invalid input. Please try again!";
}
}while(bValid == false);
}
void SetCarName()
{
bool bValid = false;
do{
cout << "What car do you drive?: ";
cin >> sCarName;
bValid = true;
}while(bValid == false);
}
void DisplayTravelDestinations()
{
cout << "\n";
cout << "Select a location number from the menu...\n";
cout << "1. South West - 141 Miles\n";
cout << "2. Leeds - 195\n";
cout << "3. Birmingham - 220\n";
cout << "4. Glasglow - 230 miles\n";
cout << "\n";
}
void SetLocation(){
bool bValid = false;
char cSelection = 0;
do{
DisplayTravelDestinations();
cout << "Where do you want to travel?: ";
cin >> cSelection;
cSelection = toupper(cSelection);
switch(cSelection)
{
case '1':
sChoiceOfLocation = "South West";
iMilesToTravel = 141;
bValid = true;
break;
case '2':
sChoiceOfLocation = "Leeds";
iMilesToTravel = 195;
bValid = true;
break;
case '3':
sChoiceOfLocation = "Birmingham";
iMilesToTravel = 220;
bValid = true;
break;
case '4':
sChoiceOfLocation = "Glasglow";
iMilesToTravel = 230;
bValid = true;
break;
default:
cout << "Invalid input! Please try again.";
continue;
}
}while(bValid == false);
}
void CalculateTotalToBePaid()
{
if (iMilesToTravel < 100)
{
dTotalToBePaid = iMilesToTravel * dFuelCost;
}
else
{
dTotalToBePaid = 100 * dFuelCost;
dTotalToBePaid += (iMilesToTravel - 100) * (dFuelCost / 2);
}
}
void CalculateVAT()
{
double dVATPercentage = 0.2;
dVATAmount = dTotalToBePaid * dVATPercentage;
}
void CalculateEstimatedTravelTime()
{
const int iAverage = 50;
dEstimatedTime = iMilesToTravel / iAverage;
}
string GetName(){return sName;}
string GetCarName(){return sCarName;}
string GetFuelType(){return sCarFuelType;}
string GetChoiceOfLocation(){return sChoiceOfLocation;}
double GetFuelCost(){return dFuelCost;}
int GetMilesToTravel(){return iMilesToTravel;}
double GetTotalToBePaid(){return dTotalToBePaid;}
double GetVATAmount(){return dVATAmount;}
double GetEstimatedTime(){return dEstimatedTime;}
};
bool ContinueOptions()
{
char cSelection = 0;
cout << "Do you wish to restart? (Y/N) ";
cin >> cSelection;
cSelection = toupper(cSelection);
if(!cin)
{
return false;
}
bool bGoAgain = toupper(static_cast<unsigned char>(cSelection)) == 'Y';
cout << (bGoAgain ? "You've chosen to start again\n" : "Goodbye!\n");
return bGoAgain;
}
void DisplayResults(User George)
{
cout << fixed << setprecision(2);
cout << "Hi " << George.GetName() << ", you're driving in a " << George.GetCarName() << ".\n";
cout << "The car your driving runs on " << George.GetFuelType() << " which costs £" << George.GetFuelCost() << " an hour.\n";
cout << "The destination you have chosen is " << George.GetChoiceOfLocation() << " and is a distance of " << George.GetMilesToTravel() << " miles.\n";
cout << "The journey will cost you £" << George.GetTotalToBePaid() << " with 20% VAT: £" << George.GetVATAmount() << ".\n";
cout << "The estimated amount of time it will take is " << George.GetEstimatedTime() << " hours.\n";
}
void GetUserInformation(User &George)
{
George.SetName();
George.SetCarName();
George.SetCarFuelType();
George.SetLocation();
George.CalculateTotalToBePaid();
George.CalculateVAT();
George.CalculateEstimatedTravelTime();
}
int main() {
do{
User George;
GetUserInformation(George);
DisplayResults(George);
}while(ContinueOptions());
return 0;
}
using namespace std;
in it. You might also want to remove the licensing information since posting the code here effectively makes in public domain and Stack Overflow Inc. Has their own licensing for the questions asked. \$\endgroup\$