Hey everyone, I just had a quick question in regards to syntax. I have 6 functions. All of the functions work but I'm having trouble calling the functions. For example, I am having users first select what they will be paying with, cash or debit. Then, i am having them select a gas type. *NOW this is what im having trouble with* I'm calling the function that selects the gas, say Diesel at 3.99. I've been trying to debug this for about an hour but when I pick any value, say Diesel at 3.99 and insert the gallons, 10. It reads $680.00.
I feel like pulling my hair out because I can't find any errors in the syntax and feel like I have it right. Anyways, any help would be greatly appreciated. I'm not asking you to do it for me but just kindly point me in the right direction. Thank you so much for your help in advance. (The syntax is mostly right, i had to chop some out for it to fit).
How many gallons you would? Please enter a positive float value: 10
Your bill is: $680.00
#include <iostream>
#include <iomanip>
usingnamespace std;
constchar CASH = 'C';
constchar CREDIT = 'D';
constchar REGULAR = 'R';
constchar EXTRA = 'X';
constchar PREMIUM = 'P';
constchar DIESEL = 'D';
constfloat REGULARPRICE = 3.42;
constfloat EXTRAPRICE = 3.85;
constfloat PREMIUMPRICE = 4.05;
constfloat DIESELPRICE = 3.99;
//---------------------------------------------------------------
// ConfirmChoice
// Parameters: The type of gas selected
// Purpose: Confirms the users gas purchase before filling up
// Returns: true if the user confirms the selection, false otherwise
//--------------------------------------------------------------
bool ConfirmChoice(constchar GasType) //function #1
{
char Choice;
bool Confirmed;
// Print out their selection
cout << "\nYou have chosen to purchase ";
cout << fixed << setprecision(2);
switch (GasType)
{
case REGULAR:
cout << "Regular gas at a price of $";
cout << REGULARPRICE << ".\n";
break;
case EXTRA:
cout << "Extra gas at a price of $";
cout << EXTRAPRICE << ".\n";
break;
case PREMIUM:
cout << "Premium gas at a price of $";
cout << PREMIUMPRICE << ".\n";
break;
case DIESEL:
cout << "Deisel gas at a price of $";
cout << DIESELPRICE << ".\n";
break;
}
//Confirm ?
cout << "Do you wish to confirm your purchase? Enter Y or N: ";
cin >> Choice;
Choice = toupper(Choice);
while (Choice != 'Y' && Choice != 'N')
{
cout << "Invalid selection. Please enter either Y or N: ";
cin >> Choice;
Choice = toupper(Choice);
}
Confirmed = (Choice == 'Y');
//Check Confirmation
if (Confirmed)
cout << "You have confirmed your choice.\n" << endl;
else
cout << "You have not confirmed your choice.\n" << endl;
return (Confirmed);
}
void CalculateChange(constfloat ChangeDue) // Function#2
{
int Change = 0;
int Dollars = 0;
int Quarters = 0;
int Dimes = 0;
int Nickels = 0;
int Pennies = 0;
//Compute Change
Change = ChangeDue * 100;
Dollars = Change / 100;
Change = Change % 100;
Quarters = Change / 25;
Change = Change % 25;
Dimes = Change / 10;
Change = Change % 10;
Nickels = Change / 5;
Pennies = Change % 5;
//Print out Change
cout << "Your change is \n\t" << Dollars << " Dollars\n\t"
<< Quarters << " Quarters\n\t" << Dimes << " Dimes\n\t"
<< Nickels << " Nickels\n\t" << Pennies << " Pennies\n";
}
float CalculateCost(constfloat PricePerGallon)
{
float Gallons;
float Cost;
cout << "How many gallons you would? Please enter a positive float value: ";
cin >> Gallons;
while (Gallons < 0)
cout << "Invalid entry. Please re-enter: ";
cin >> Gallons;
}
Cost = PricePerGallon * Gallons;
cout << "Your bill is: $" << fixed << setprecision(2) << Cost << endl;
return Cost;
}
//---------------------------------------------------------------------------
// Name: GetPaymentType
// Purpose: Ask the user how they want to pay, cash or credit
// Parameters: none
// Returns: char; value is Credit or Cash (global constants)
//---------------------------------------------------------------------------
char GetPaymentType() //Fuction #3
{
char Choice;
// Print the main menu describing the Gas Prices
cout << "+-------------------------------------------------------+\n";
cout << "+ Welcome to our Gas Station +\n";
cout << "+-------------------------------------------------------+\n";
cout << endl << endl;
// Cash or Credit Card
cout << "How would you like to pay?\n";
cout << "Enter C for cash or D for credit card: ";
cin >> Choice;
Choice = toupper(Choice); // convert to uppercase
while (Choice != CASH && Choice != CREDIT)
{
cout << "Invalid choice. Please re-enter: ";
cin >> Choice;
Choice = toupper(Choice);
}
return Choice;
void GetGasType(char &Choice) //Function#4
{
// Ask the customer what type of gas s/he prefers to buy
cout << "\nWhat type of gas would you like?\n";
cout << "\t" << REGULAR << " for Regular, Price = $" << REGULARPRICE << endl;
cout << "\t" << EXTRA << " for Extra, Price = $" << EXTRAPRICE << endl;
cout << "\t" << PREMIUM << " for Premium, Price = $" << PREMIUMPRICE << endl;
cout << "\t" << DIESEL << " for Diesel, Price = $" << DIESELPRICE << endl;
//Get Gas Choice
cout << "Enter choice: ";
cin >> Choice;
Choice = toupper(Choice); // convert to uppercase
while (Choice != REGULAR && Choice != EXTRA && Choice != PREMIUM
&& Choice != DIESEL)
{
cout << "Invalid choice. Please re-enter: ";
cin >> Choice;
Choice = toupper(Choice);
}
}
//---------------------------------------------------------------------------
// Name: PayWithCash
// Purpose: Handles payment by cash. Asks the user for the money until
// they enter enough, then updates the ChangeDue parameter
// Parameters: const float Cost - the amount due for the purchase
// float &ChangeDue - the amount of change due
// Returns: Nothing
//---------------------------------------------------------------------------
void PayWithCash(constfloat Cost, float &ChangeDue) //Fuction$5
{
float CashOffered;
// Pay in Cash
cout << "Please enter enough cash. Your bill is $" << Cost << ": $ ";
cin >> CashOffered;
//Check Sufficiency
while (CashOffered < Cost)
{
cout << "That is not enough to pay for your purchase!\n"
<< " Please enter at least $" << Cost << ": ";
cin >> CashOffered;
}
//Calculate Change
ChangeDue = CashOffered - Cost;
}
//---------------------------------------------------------------------------
// Name: PayWithCredit
// Purpose: Handles payment by credit. Basically, just prints a statement
// telling them that their card will be charged.
// Parameters: const float Cost - the amount due for the purchase
// Returns: nothing
//---------------------------------------------------------------------------
void PayWithCredit(constfloat Cost) //Fuction#6
{
cout << "Your credit card will be billed for $" << Cost << ".\n";
}
//---------------------------------------------------------------------------
// This is the main program that you need to write
//---------------------------------------------------------------------------
int main()
{
// Declarations
char GChoice; // gas type: Regular, Extra, etc..
char PChoice; // payment choice: cash or credit card
bool Confirmed; // did the user confirm the selection
float Cost; // the cost of the gas puchased
float ChangeDue; // the amount of change owed (for cash purchases)
cout << "-----------------" << endl; //
PChoice=GetPaymentType();
if (PChoice=='C' || PChoice=='D')
{ GetGasType(PChoice); }
ConfirmChoice(PChoice);
CalculateCost(PChoice);
Your function header for CalculateCost has one parameter of type constfloat, yet in your call to that function you pass it a char which represents the chosen gas type. So it's using the ASCII keycode of that char as its 'price per gallon' value.